"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SeedRecruitmentPermissions1781070000000 = void 0;
const crypto_1 = require("crypto");
class SeedRecruitmentPermissions1781070000000 {
    constructor() {
        this.name = 'SeedRecruitmentPermissions1781070000000';
    }
    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 seedRolePermissions(queryRunner, roleSlug, permissions) {
        for (const [key, value] of permissions) {
            await this.insertPermissionIfMissing(queryRunner, roleSlug, key, value);
        }
    }
    async up(queryRunner) {
        const ownerPermissions = [
            ['recruitment.access', 'true'],
            ['recruitment.view', 'all'],
            ['recruitment.create', 'all'],
            ['recruitment.update', 'all'],
            ['recruitment.delete', 'all'],
            ['recruitment.manage', 'true'],
            ['recruitment.api_keys', 'true'],
            ['onboarding.access', 'true'],
            ['onboarding.view', 'all'],
            ['onboarding.manage', 'true'],
            ['onboarding.convert_member', 'true'],
        ];
        const adminPermissions = [
            ['recruitment.access', 'true'],
            ['recruitment.view', 'all'],
            ['recruitment.create', 'all'],
            ['recruitment.update', 'all'],
            ['recruitment.delete', 'all'],
            ['recruitment.manage', 'true'],
            ['recruitment.api_keys', 'false'],
            ['onboarding.access', 'true'],
            ['onboarding.view', 'all'],
            ['onboarding.manage', 'true'],
            ['onboarding.convert_member', 'true'],
        ];
        const memberPermissions = [
            ['recruitment.access', 'false'],
            ['recruitment.view', 'none'],
            ['recruitment.create', 'none'],
            ['recruitment.update', 'none'],
            ['recruitment.delete', 'none'],
            ['recruitment.manage', 'false'],
            ['recruitment.api_keys', 'false'],
            ['onboarding.access', 'true'],
            ['onboarding.view', 'own'],
            ['onboarding.manage', 'false'],
            ['onboarding.convert_member', 'false'],
        ];
        await this.seedRolePermissions(queryRunner, 'owner', ownerPermissions);
        await this.seedRolePermissions(queryRunner, 'admin', adminPermissions);
        await this.seedRolePermissions(queryRunner, 'member', memberPermissions);
        await queryRunner.query(`INSERT INTO hiring_sources (id, name, description, status, owner_id, created_at, updated_at)
       SELECT UUID(), 'Recruitment', 'Hired through recruitment module', 'active', o.id, NOW(3), NOW(3)
       FROM owner o
       WHERE NOT EXISTS (
         SELECT 1 FROM hiring_sources hs
         WHERE hs.owner_id = o.id AND hs.name = 'Recruitment'
       )`);
    }
    async down(queryRunner) {
        await queryRunner.query(`DELETE FROM role_permissions
       WHERE permission_key IN (
         'recruitment.access',
         'recruitment.view',
         'recruitment.create',
         'recruitment.update',
         'recruitment.delete',
         'recruitment.manage',
         'recruitment.api_keys',
         'onboarding.access',
         'onboarding.view',
         'onboarding.manage',
         'onboarding.convert_member'
       )`);
        await queryRunner.query(`DELETE FROM hiring_sources WHERE name = 'Recruitment'`);
    }
}
exports.SeedRecruitmentPermissions1781070000000 = SeedRecruitmentPermissions1781070000000;
//# sourceMappingURL=1781070000000-SeedRecruitmentPermissions.js.map