/**
 * Canonical SLA activity catalog — `activityName` MUST match workflow `currentStage`
 * (or onboarding pipeline label) for timers to start/stop correctly.
 *
 * SRS: Re_New_Dealer_Onboard_TWO.md §9.4
 */
import {
    TERMINATION_STAGES,
    RESIGNATION_STAGES,
    RELOCATION_STAGES,
    CONSTITUTIONAL_STAGES,
    FNF_DEPARTMENTS
} from './constants.js';

export type SlaCatalogEntry = {
    module: 'ONBOARDING' | 'RESIGNATION' | 'TERMINATION' | 'RELOCATION' | 'CONSTITUTIONAL' | 'FNF';
    activityName: string;
    ownerRole: string;
    tatHours: number;
    tatUnit: 'hours' | 'days';
};

/**
 * Onboarding pipeline steps that must NOT use internal SLA (§9.4).
 * - Submitted: applicant action already complete — no internal approver TAT.
 * - Questionnaire: prospect/dealer fills form — use QUESTIONNAIRE_REMINDER (email/WhatsApp), not SLA to ASM/RBM.
 */
export const ONBOARDING_SLA_EXCLUDED_PIPELINE_STAGES = ['Submitted', 'Questionnaire'] as const;

/** Legacy rows to deactivate when re-seeding after catalog corrections */
export const ONBOARDING_SLA_DEPRECATED_ACTIVITIES = [
    ...ONBOARDING_SLA_EXCLUDED_PIPELINE_STAGES,
    'ASM Review',
    'ZM Review',
    'Level 1 Interview',
    'Level 2 Interview',
    'Level 3 Interview',
    'FDD Verification',
    'Finance Verification',
    'LOA Approval'
] as const;

export function shouldTrackOnboardingSla(pipelineStage: string | null | undefined): boolean {
    if (!pipelineStage) return false;
    return !ONBOARDING_SLA_EXCLUDED_PIPELINE_STAGES.includes(
        pipelineStage as (typeof ONBOARDING_SLA_EXCLUDED_PIPELINE_STAGES)[number]
    );
}

/** Pre-rename `sla_configurations.activityName` values still present until re-seed */
export const SLA_ACTIVITY_LEGACY_ALIASES: Record<string, string[]> = {
    '1st Level Interview': ['Level 1 Interview'],
    '2nd Level Interview': ['Level 2 Interview'],
    '3rd Level Interview': ['Level 3 Interview']
};

/** Names to match when resolving SLA config / stopping a timer for a pipeline stage */
export function slaConfigLookupNames(pipelineStage: string): string[] {
    return [pipelineStage, ...(SLA_ACTIVITY_LEGACY_ALIASES[pipelineStage] || [])];
}

const d = (module: SlaCatalogEntry['module'], activityName: string, ownerRole: string, tatHours: number, tatUnit: 'hours' | 'days' = 'days'): SlaCatalogEntry => ({
    module,
    activityName,
    ownerRole,
    tatHours,
    tatUnit
});

/**
 * Onboarding — internal approver TAT only (`PIPELINE_STAGE_LABEL_BY_OVERALL_STATUS`).
 * Excluded: Submitted, Questionnaire (see ONBOARDING_SLA_EXCLUDED_PIPELINE_STAGES).
 */
const ONBOARDING_SLA: SlaCatalogEntry[] = [
    d('ONBOARDING', 'Shortlist', 'DD Admin', 3),
    d('ONBOARDING', '1st Level Interview', 'RBM, DD-ZM', 2),
    d('ONBOARDING', '2nd Level Interview', 'DD Lead, ZBH', 3),
    d('ONBOARDING', '3rd Level Interview', 'NBH, DD Head', 5),
    d('ONBOARDING', 'FDD', 'FDD', 10),
    d('ONBOARDING', 'LOI Approval', 'NBH', 5),
    d('ONBOARDING', 'Security Deposit', 'Finance', 7),
    d('ONBOARDING', 'LOI Issue', 'DD Admin', 3),
    d('ONBOARDING', 'Dealer Code Generation', 'DD Admin', 2),
    d('ONBOARDING', 'Architecture Work', 'Architecture Team', 14),
    d('ONBOARDING', 'Statutory Work', 'DD Admin, Legal', 14),
    d('ONBOARDING', 'LOA', 'NBH', 5),
    d('ONBOARDING', 'EOR Complete', 'DD Admin', 7),
    d('ONBOARDING', 'Inauguration', 'ASM', 3)
];

const TERMINATION_SLA: SlaCatalogEntry[] = [
    d('TERMINATION', TERMINATION_STAGES.RBM_REVIEW, 'RBM, DD-ZM', 3),
    d('TERMINATION', TERMINATION_STAGES.ZBH_REVIEW, 'ZBH', 3),
    d('TERMINATION', TERMINATION_STAGES.DD_LEAD_REVIEW, 'DD Lead', 5),
    d('TERMINATION', TERMINATION_STAGES.LEGAL_VERIFICATION, 'Legal Admin', 7),
    d('TERMINATION', TERMINATION_STAGES.DD_HEAD_REVIEW, 'DD Head', 5),
    d('TERMINATION', TERMINATION_STAGES.NBH_EVALUATION, 'NBH', 5),
    d('TERMINATION', TERMINATION_STAGES.SCN_ISSUED, 'Legal Admin, DD Admin', 5),
    d('TERMINATION', TERMINATION_STAGES.PERSONAL_HEARING, 'DD Lead, ZBH, RBM, DD Head', 5),
    d('TERMINATION', TERMINATION_STAGES.NBH_FINAL_APPROVAL, 'NBH', 3),
    d('TERMINATION', TERMINATION_STAGES.CCO_APPROVAL, 'CCO', 3),
    d('TERMINATION', TERMINATION_STAGES.CEO_APPROVAL, 'CEO', 5),
    d('TERMINATION', TERMINATION_STAGES.LEGAL_LETTER, 'Legal Admin', 5)
];

const RESIGNATION_SLA: SlaCatalogEntry[] = [
    d('RESIGNATION', RESIGNATION_STAGES.ASM, 'ASM', 2),
    d('RESIGNATION', RESIGNATION_STAGES.RBM, 'RBM, DD-ZM', 3),
    d('RESIGNATION', RESIGNATION_STAGES.ZBH, 'ZBH', 3),
    d('RESIGNATION', RESIGNATION_STAGES.DD_LEAD, 'DD Lead', 5),
    d('RESIGNATION', RESIGNATION_STAGES.DD_HEAD, 'DD Head', 5),
    d('RESIGNATION', RESIGNATION_STAGES.NBH, 'NBH', 5),
    d('RESIGNATION', RESIGNATION_STAGES.LEGAL, 'Legal Admin', 7),
    d('RESIGNATION', RESIGNATION_STAGES.DD_ADMIN, 'DD Admin', 3),
    d('RESIGNATION', RESIGNATION_STAGES.AWAITING_FNF, 'DD Lead, DD Admin', 7)
];

const RELOCATION_SLA: SlaCatalogEntry[] = [
    d('RELOCATION', RELOCATION_STAGES.ASM_REVIEW, 'ASM', 2),
    d('RELOCATION', RELOCATION_STAGES.RBM_REVIEW, 'RBM', 3),
    d('RELOCATION', RELOCATION_STAGES.DD_ZM_REVIEW, 'DD-ZM', 3),
    d('RELOCATION', RELOCATION_STAGES.ZBH_REVIEW, 'ZBH', 3),
    d('RELOCATION', RELOCATION_STAGES.DD_LEAD_REVIEW, 'DD Lead', 5),
    d('RELOCATION', RELOCATION_STAGES.NBH_APPROVAL, 'NBH', 5),
    d('RELOCATION', RELOCATION_STAGES.LEGAL_CLEARANCE, 'Legal Admin', 7)
];

const CONSTITUTIONAL_SLA: SlaCatalogEntry[] = [
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.ASM_REVIEW, 'ASM', 2),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.ZM_RBM_REVIEW, 'RBM, DD-ZM', 3),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.ZBH_REVIEW, 'ZBH', 3),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.LEAD_REVIEW, 'DD Lead', 5),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.HEAD_REVIEW, 'DD Head', 5),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.NBH_APPROVAL, 'NBH', 5),
    d('CONSTITUTIONAL', CONSTITUTIONAL_STAGES.LEGAL_REVIEW, 'Legal Admin', 7)
];

/** F&F departmental NOC — SRS §4 / modular_wise/04_FF_Settlement.md */
const FNF_SLA: SlaCatalogEntry[] = FNF_DEPARTMENTS.map((dept) =>
    d('FNF', `F&F Clearance: ${dept}`, dept.includes('Finance') ? 'Finance' : 'DD Admin', 5)
);

export const SLA_STAGE_CATALOG: SlaCatalogEntry[] = [
    ...ONBOARDING_SLA,
    ...TERMINATION_SLA,
    ...RESIGNATION_SLA,
    ...RELOCATION_SLA,
    ...CONSTITUTIONAL_SLA,
    ...FNF_SLA
];

export const SLA_CATALOG_BY_MODULE = {
    ONBOARDING: ONBOARDING_SLA,
    RESIGNATION: RESIGNATION_SLA,
    TERMINATION: TERMINATION_SLA,
    RELOCATION: RELOCATION_SLA,
    CONSTITUTIONAL: CONSTITUTIONAL_SLA,
    FNF: FNF_SLA
} as const;
