import { FNF_DEPARTMENTS } from '../config/constants.js';
import { SLAService } from '../../services/SLAService.js';

export function fnfSlaStageName(department: string): string {
    return `F&F Clearance: ${department}`;
}

/** Start/stop departmental F&F SLA timer (non-fatal). */
export async function syncFnfClearanceSla(fnfId: string, department: string, status: string) {
    const stageName = fnfSlaStageName(department);
    const normalized = String(status || '').toLowerCase();

    try {
        if (normalized === 'pending') {
            await SLAService.startTrack({
                entityType: 'fnf',
                entityId: fnfId,
                applicationId: null,
                stageName
            });
        } else {
            await SLAService.stopTrack({
                entityType: 'fnf',
                entityId: fnfId,
                applicationId: null,
                stageName
            });
        }
    } catch (err) {
        console.error('[slaFnfSync] clearance SLA sync failed:', err);
    }
}

export async function startAllPendingFnfClearanceSlas(fnfId: string) {
    await Promise.all(FNF_DEPARTMENTS.map((dept) => syncFnfClearanceSla(fnfId, dept, 'Pending')));
}
