import { SLAService } from '../../services/SLAService.js';

export type SlaEntityType = 'application' | 'termination' | 'resignation' | 'relocation' | 'constitutional' | 'fnf';

/** Sync SLA timers when a workflow stage changes (non-fatal). */
export async function syncSlaOnStageTransition(opts: {
    entityType: SlaEntityType;
    entityId: string;
    applicationId?: string | null;
    fromStage?: string | null;
    toStage?: string | null;
}) {
    const { entityType, entityId, applicationId = null, fromStage, toStage } = opts;

    try {
        if (fromStage) {
            await SLAService.stopTrack({
                entityType,
                entityId,
                applicationId,
                stageName: fromStage
            });
        }
        if (toStage) {
            await SLAService.startTrack({
                entityType,
                entityId,
                applicationId,
                stageName: toStage
            });
        }
    } catch (err) {
        console.error('[slaWorkflowSync] SLA sync failed:', err);
    }
}
