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

async function diagnoseZM() {
    try {
        console.log('--- ZM Diagnosis Start ---');
        
        const zones = await db.Zone.findAll();
        const roles = await db.Role.findAll({
            where: { roleCode: { [db.Sequelize.Op.in]: ['ZBH', 'DD-ZM'] } }
        });
        
        const zmRoleIds = roles.filter((r: any) => r.roleCode === 'DD-ZM').map((r: any) => r.id);
        console.log(`ZM Role IDs: ${zmRoleIds.join(', ')}`);

        for (const zone of zones) {
            console.log(`\nZone: ${zone.name} (ID: ${zone.id})`);
            
            const zms = await db.UserRole.findAll({
                where: { roleId: { [db.Sequelize.Op.in]: zmRoleIds }, zoneId: zone.id, isActive: true },
                include: [{ model: db.User, as: 'user', attributes: ['id', 'fullName', 'email', 'mobileNumber', 'employeeId'] }]
            });
            
            console.log(`Active ZM Count: ${zms.length}`);
            zms.forEach(z => {
                console.log(`- ZM: ${z.user.fullName} (ID: ${z.user.id})`);
            });
        }

        console.log('\n--- ZM Diagnosis End ---');
        process.exit(0);
    } catch (error) {
        console.error('ZM Diagnosis Failed:', error);
        process.exit(1);
    }
}

diagnoseZM();
