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

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

dump();
