import db from './src/database/models/index.js';
import fs from 'fs';

async function dump() {
    try {
        const roles = await (db as any).Role.findAll({
            include: [{
                model: (db as any).Permission,
                as: 'permissions',
                through: { attributes: [] }
            }]
        });
        const data = roles.map((r: any) => ({
            id: r.id,
            name: r.roleName,
            permissions: r.permissions.map((p: any) => p.permissionCode)
        }));
        fs.writeFileSync('db_audit.json', JSON.stringify(data, null, 2));
        
        const perms = await (db as any).Permission.findAll();
        fs.writeFileSync('all_perms.json', JSON.stringify(perms.map((p: any) => p.permissionCode), null, 2));
        
        console.log('Audit complete');
    } catch (e: any) {
        fs.writeFileSync('db_error.txt', e.stack || e.toString());
        console.error(e);
    } finally {
        process.exit();
    }
}

dump();
