import 'dotenv/config';
import db from '../src/database/models/index.js';
import { ROLES } from '../src/common/config/constants.js';

const { Role } = db;

const rolesToSeed = [
    { roleCode: ROLES.SUPER_ADMIN, roleName: 'Super Admin', category: 'ADMIN', description: 'Full system access' },
    { roleCode: ROLES.DD_ADMIN, roleName: 'DD Admin', category: 'ADMIN', description: 'Dealer Development Admin' },
    { roleCode: ROLES.DD_HEAD, roleName: 'DD Head', category: 'LEVEL_3', description: 'DD Head' },
    { roleCode: ROLES.DD_LEAD, roleName: 'DD Lead', category: 'LEVEL_2', description: 'DD Lead' },
    { roleCode: ROLES.DD_ZM, roleName: 'DD-ZM', category: 'LEVEL_1', description: 'DD Zone Manager' },
    { roleCode: ROLES.ZBH, roleName: 'ZBH', category: 'ZONAL', description: 'Zonal Business Head' },
    { roleCode: ROLES.RBM, roleName: 'RBM', category: 'REGIONAL', description: 'Regional Business Manager' },
    { roleCode: ROLES.FINANCE, roleName: 'Finance', category: 'DEPARTMENT', description: 'Finance Department' },
    { roleCode: ROLES.LEGAL_ADMIN, roleName: 'Legal Admin', category: 'DEPARTMENT', description: 'Legal Department' },
    { roleCode: ROLES.NBH, roleName: 'NBH', category: 'NATIONAL', description: 'National Business Head' },
    { roleCode: ROLES.ASM, roleName: 'ASM', category: 'SALES', description: 'Area Sales Manager' },
    { roleCode: ROLES.DEALER, roleName: 'Dealer', category: 'EXTERNAL', description: 'Dealer Principal' },
    { roleCode: ROLES.FDD, roleName: 'FDD Team', category: 'EXTERNAL', description: 'Financial Due Diligence Team' },
    { roleCode: ROLES.ARCHITECTURE, roleName: 'Architecture Team', category: 'DEPARTMENT', description: 'Architecture & Design Team' },
    { roleCode: ROLES.CCO, roleName: 'CCO', category: 'LEADERSHIP', description: 'Chief Commercial Officer' },
    { roleCode: ROLES.CEO, roleName: 'CEO', category: 'LEADERSHIP', description: 'Chief Executive Officer' },
    { roleCode: ROLES.SPARES_MANAGER, roleName: 'Spares Manager', category: 'DEPARTMENT', description: 'Spares Department Clearance' },
    { roleCode: ROLES.SERVICE_MANAGER, roleName: 'Service Manager', category: 'DEPARTMENT', description: 'Service Department Clearance' },
    { roleCode: ROLES.ACCOUNTS_MANAGER, roleName: 'Accounts Manager', category: 'DEPARTMENT', description: 'Accounts Department Clearance' },
    { roleCode: ROLES.DD_AM, roleName: 'DD-AM', category: 'SALES', description: 'Dealer Development Area Manager' }
];

async function seedRoles() {
    console.log('🌱 Seeding roles...');
    try {
        await db.sequelize.authenticate();

        for (const r of rolesToSeed) {
            await Role.upsert(r);
        }

        console.log('✅ Roles seeded successfully!');
        process.exit(0);
    } catch (error) {
        console.error('❌ Seeding failed:', error);
        process.exit(1);
    }
}

seedRoles();
