"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AttendanceRequestPermissions1781020000000 = void 0;
const crypto_1 = require("crypto");
class AttendanceRequestPermissions1781020000000 {
    constructor() {
        this.name = 'AttendanceRequestPermissions1781020000000';
    }
    async insertPermissionIfMissing(queryRunner, roleSlug, permissionKey, permissionValue) {
        await queryRunner.query(`INSERT INTO role_permissions (id, role_id, permission_key, permission_value)
       SELECT ?, ar.id, ?, ?
       FROM access_roles ar
       WHERE ar.slug = ?
         AND NOT EXISTS (
           SELECT 1
           FROM role_permissions rp
           WHERE rp.role_id = ar.id
             AND rp.permission_key = ?
         )`, [(0, crypto_1.randomUUID)(), permissionKey, permissionValue, roleSlug, permissionKey]);
    }
    async up(queryRunner) {
        const memberPermissions = [
            ['attendance.request.create', 'own'],
            ['attendance.request.view', 'own'],
            ['attendance.request.approve', 'false'],
        ];
        for (const [key, value] of memberPermissions) {
            await this.insertPermissionIfMissing(queryRunner, 'member', key, value);
        }
        const adminPermissions = [
            ['attendance.request.create', 'all'],
            ['attendance.request.view', 'all'],
            ['attendance.request.approve', 'true'],
        ];
        for (const [key, value] of adminPermissions) {
            await this.insertPermissionIfMissing(queryRunner, 'admin', key, value);
            await this.insertPermissionIfMissing(queryRunner, 'owner', key, value);
        }
    }
    async down(queryRunner) {
        await queryRunner.query(`DELETE FROM role_permissions
       WHERE permission_key IN (
         'attendance.request.create',
         'attendance.request.view',
         'attendance.request.approve'
       )`);
    }
}
exports.AttendanceRequestPermissions1781020000000 = AttendanceRequestPermissions1781020000000;
//# sourceMappingURL=1781020000000-AttendanceRequestPermissions.js.map