import { FNF_STATUS, RESIGNATION_STAGES, TERMINATION_STAGES } from '../config/constants.js';

const FNF_STATUS_ALIASES: Record<string, string> = {
    initiated: FNF_STATUS.INITIATED,
    new: FNF_STATUS.INITIATED,
    draft: FNF_STATUS.INITIATED,
    dd_clearance: FNF_STATUS.DD_CLEARANCE,
    'dd clearance': FNF_STATUS.DD_CLEARANCE,
    legal_clearance: FNF_STATUS.LEGAL_CLEARANCE,
    'legal clearance': FNF_STATUS.LEGAL_CLEARANCE,
    finance_approval: FNF_STATUS.FINANCE_APPROVAL,
    'finance approval': FNF_STATUS.FINANCE_APPROVAL,
    completed: FNF_STATUS.COMPLETED,
    settled: FNF_STATUS.COMPLETED
};

export const normalizeFnFStatus = (status: string | null | undefined): string => {
    if (!status) return FNF_STATUS.INITIATED;
    const key = status.trim().toLowerCase();
    return FNF_STATUS_ALIASES[key] || status;
};

export const getResignationStatusForStage = (stage: string): string => {
    switch (stage) {
        case RESIGNATION_STAGES.RBM:
            return RESIGNATION_STAGES.RBM; // It already contains "Review"
        case RESIGNATION_STAGES.ASM:
        case RESIGNATION_STAGES.ZBH:
        case RESIGNATION_STAGES.DD_LEAD:
        case RESIGNATION_STAGES.DD_HEAD:
        case RESIGNATION_STAGES.NBH:
        case RESIGNATION_STAGES.DD_ADMIN:
            return `${stage} Review`;
        case RESIGNATION_STAGES.AWAITING_FNF:
            return 'Awaiting F&F — manual initiation';
        case RESIGNATION_STAGES.LEGAL:
            return 'Legal - Resignation Letter';
        case RESIGNATION_STAGES.FNF_INITIATED:
            return RESIGNATION_STAGES.FNF_INITIATED;
        case RESIGNATION_STAGES.COMPLETED:
            return 'Completed';
        case RESIGNATION_STAGES.REJECTED:
            return 'Rejected';
        default:
            return stage;
    }
};

export const getTerminationStatusForStage = (stage: string): string => {
    switch (stage) {
        case TERMINATION_STAGES.SUBMITTED:
            return 'Submitted';
        case TERMINATION_STAGES.TERMINATED:
            return 'Terminated';
        case TERMINATION_STAGES.REJECTED:
            return 'Rejected';
        default:
            return stage;
    }
};

/** Legacy DB rows may still use SRS label "Personal Hearing" while workflow code keys the canonical stage constant. */
const LEGACY_TERMINATION_STAGE_TO_CANONICAL: Record<string, string> = {
    'Personal Hearing': TERMINATION_STAGES.PERSONAL_HEARING
};

export const normalizeTerminationCurrentStage = (stage: string | null | undefined): string => {
    if (stage == null) return '';
    const trimmed = String(stage).trim();
    return LEGACY_TERMINATION_STAGE_TO_CANONICAL[trimmed] || trimmed;
};

/** Returns column updates to align legacy termination rows with current stage/status strings (no-op if already canonical). */
export const getLegacyTerminationRowFixes = (termination: {
    currentStage?: string | null;
    status?: string | null;
}): Record<string, string> | null => {
    const updates: Record<string, string> = {};
    const rawStage = termination.currentStage;
    if (rawStage) {
        const canonical = normalizeTerminationCurrentStage(rawStage);
        if (canonical !== rawStage) updates.currentStage = canonical;
    }
    const st = termination.status;
    if (st && /personal hearing/i.test(st)) {
        updates.status = st.replace(/personal hearing/gi, 'SCN Response Evaluation');
    }
    return Object.keys(updates).length ? updates : null;
};

export const normalizeClearanceStatus = (status: string | null | undefined, amount: number): string => {
    const normalizedAmount = Math.abs(Number(amount) || 0);
    const value = (status || '').toLowerCase();

    if (value === 'cleared' || value === 'noc submitted') {
        return normalizedAmount > 0 ? 'Dues Pending' : 'NOC Submitted';
    }
    if (value === 'dues' || value === 'dues pending') return 'Dues Pending';
    if (value === 'n/a') return 'N/A';
    if (value === 'pending') return 'Pending';

    return normalizedAmount > 0 ? 'Dues Pending' : 'NOC Submitted';
};
