import db from '../src/database/models/index.js';
import { ParticipantService } from '../src/services/ParticipantService.js';

async function run() {
    console.log('--- GLOBAL F&F PARTICIPANT AUDIT ---');
    
    try {
        const initiatedFnFs = await db.FnF.findAll({
            where: { status: 'Initiated' },
            attributes: ['id', 'settlementId', 'resignationId', 'terminationRequestId']
        });
        
        console.log(`Found ${initiatedFnFs.length} initiated F&F records.`);
        
        let fixesCount = 0;
        for (const fnf of initiatedFnFs) {
            const count = await db.RequestParticipant.count({
                where: { requestId: fnf.id, requestType: 'fnf' }
            });
            
            if (count === 0) {
                console.log(`Fixing missing participants for F&F: ${fnf.settlementId || fnf.id}`);
                await ParticipantService.assignFnFParticipants(fnf.id);
                fixesCount++;
            }
        }
        
        console.log(`Global audit complete. Fixed ${fixesCount} records.`);
    } catch (error) {
        console.error('Audit Error:', error);
    } finally {
        process.exit();
    }
}

run();
