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

async function run() {
    console.log('Starting Global Participant Synchronization...');

    try {
        // 1. Sync F&F Settlements
        console.log('\n--- Syncing F&F Settlement Participants ---');
        const fnfs = await db.FnF.findAll({ attributes: ['id', 'settlementId'] });
        for (const fnf of fnfs) {
            console.log(`Syncing F&F: ${fnf.settlementId} (${fnf.id})`);
            await ParticipantService.assignFnFParticipants(fnf.id);
        }

        // 2. Sync Resignations
        console.log('\n--- Syncing Resignation Participants ---');
        const resignations = await db.Resignation.findAll({ attributes: ['id', 'resignationId'] });
        for (const res of resignations) {
            console.log(`Syncing Resignation: ${res.resignationId} (${res.id})`);
            await ParticipantService.assignResignationParticipants(res.id);
        }

        // 3. Sync Terminations
        console.log('\n--- Syncing Termination Participants ---');
        const terminations = await db.TerminationRequest.findAll({ attributes: ['id', 'requestId'] });
        for (const term of terminations) {
            console.log(`Syncing Termination: ${term.requestId} (${term.id})`);
            await ParticipantService.assignTerminationParticipants(term.id);
        }

        console.log('\nGlobal Synchronization Complete!');
    } catch (error) {
        console.error('Error during global sync:', error);
    } finally {
        process.exit();
    }
}

run();
