"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
const crypto_1 = require("crypto");
const promise_1 = __importDefault(require("mysql2/promise"));
const permission_catalog_1 = require("../src/modules/access-control/permission-catalog");
async function persistRolePermissions(connection, roleId, permissionMap) {
    await connection.query('DELETE FROM role_permissions WHERE role_id = ?', [
        roleId,
    ]);
    const rows = Object.entries(permissionMap).filter(([permission_key]) => permission_catalog_1.ALL_PERMISSION_KEYS.includes(permission_key));
    if (rows.length === 0) {
        return;
    }
    const values = [];
    const placeholders = rows
        .map(([permission_key, permission_value]) => {
        values.push((0, crypto_1.randomUUID)(), roleId, permission_key, permission_value);
        return '(?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))';
    })
        .join(', ');
    await connection.query(`INSERT INTO role_permissions (id, role_id, permission_key, permission_value, created_at, updated_at)
     VALUES ${placeholders}`, values);
}
async function ensureOwnerPermissionKeysExist(connection, ownerRoleId) {
    const [existingRows] = await connection.query('SELECT permission_key FROM role_permissions WHERE role_id = ?', [ownerRoleId]);
    const existingKeys = new Set(existingRows.map((row) => String(row.permission_key)));
    const defaults = (0, permission_catalog_1.getDefaultPermissionMap)('owner');
    const missing = Object.entries(defaults).filter(([permission_key]) => permission_catalog_1.ALL_PERMISSION_KEYS.includes(permission_key) &&
        !existingKeys.has(permission_key));
    if (missing.length === 0) {
        return;
    }
    const values = [];
    const placeholders = missing
        .map(([permission_key, permission_value]) => {
        values.push((0, crypto_1.randomUUID)(), ownerRoleId, permission_key, permission_value);
        return '(?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))';
    })
        .join(', ');
    await connection.query(`INSERT INTO role_permissions (id, role_id, permission_key, permission_value, created_at, updated_at)
     VALUES ${placeholders}`, values);
}
async function ensureDefaultRolesForOwner(connection, ownerId) {
    const [existingRows] = await connection.query('SELECT id, slug FROM access_roles WHERE owner_id = ?', [ownerId]);
    if (existingRows.length > 0) {
        const ownerRole = existingRows.find((row) => row.slug === 'owner');
        if (ownerRole) {
            await ensureOwnerPermissionKeysExist(connection, ownerRole.id);
        }
        console.log(`Owner ${ownerId}: roles already exist (${existingRows.length}), ensured missing owner permission keys`);
        return;
    }
    for (const template of permission_catalog_1.SYSTEM_ROLE_TEMPLATES) {
        const roleId = (0, crypto_1.randomUUID)();
        await connection.query(`INSERT INTO access_roles
        (id, owner_id, name, slug, description, is_system, hierarchy, created_at, updated_at)
       VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, [
            roleId,
            ownerId,
            template.name,
            template.slug,
            template.description,
            template.is_system ? 1 : 0,
            template.hierarchy,
        ]);
        await persistRolePermissions(connection, roleId, (0, permission_catalog_1.getDefaultPermissionMap)(template.slug));
        console.log(`Owner ${ownerId}: created role ${template.slug}`);
    }
}
async function main() {
    const databaseUrl = process.env.DATABASE_URL;
    let connection;
    if (databaseUrl) {
        connection = await promise_1.default.createConnection(databaseUrl);
    }
    else {
        connection = await promise_1.default.createConnection({
            host: process.env.DB_HOST || 'localhost',
            port: parseInt(process.env.DB_PORT || '') || 3306,
            user: process.env.DB_USERNAME || 'root',
            password: process.env.DB_PASSWORD || '',
            database: process.env.DB_DATABASE || 'tracker',
        });
    }
    try {
        const [owners] = await connection.query('SELECT id FROM owner');
        if (!owners.length) {
            throw new Error('No rows found in owner table');
        }
        for (const owner of owners) {
            await ensureDefaultRolesForOwner(connection, owner.id);
        }
        const [roleCount] = await connection.query('SELECT COUNT(*) AS cnt FROM access_roles');
        const [permCount] = await connection.query('SELECT COUNT(*) AS cnt FROM role_permissions');
        console.log(`Done. access_roles=${roleCount[0]?.cnt ?? 0}, role_permissions=${permCount[0]?.cnt ?? 0}`);
    }
    finally {
        await connection.end();
    }
}
main().catch((error) => {
    console.error(error);
    process.exit(1);
});
//# sourceMappingURL=seed-access-roles.js.map