import type { IssueMatrixRow } from "./issue-matrix-data";

export type IssueMatrixLanguage = "en" | "kh";

export type IssueMatrixTranslation = {
    title: string;
    subtitle: string;
    search: string;
    exportAsXlsx: string;
    previous: string;
    next: string;
    viewDetail: string;
    download: string;
    noData: string;
    statusChangeDialog: {
        title: string;
        description: (status: string) => string;
        confirm: string;
        cancel: string;
    };
    ministryChangeDialog: {
        title: string;
        description: (ministry: string) => string;
        confirm: string;
        cancel: string;
    };
    rowUpdateToast: {
        ministryUpdated: string;
        statusUpdated: string;
    };
    plenaryEscalationDialog: {
        escalateTitle: string;
        escalateDescription: string;
        escalateConfirm: string;
        removeTitle: string;
        removeDescription: string;
        removeConfirm: string;
        cancel: string;
    };
    plenaryEscalationToast: {
        escalated: string;
        removed: string;
        close: string;
    };
    plenaryEscalationError: string;
    list: string;
    grid: string;
    gridCard: {
        no: string;
        ministry: string;
        status: string;
        categoryOfIssue: string;
        issue: string;
        escalation: string;
        issueDescription: string;
        recommendation: string;
        rgcDecision: string;
        plenary: string;
        notPlenary: string;
    };
    totalIssues: string;
    solved: string;
    inProgress: string;
    notAddressed: string;
    totalPrimaryAgencies: string;
    columns: Partial<Record<keyof IssueMatrixRow, string>>;
};

export function normalizeIssueMatrixLanguage(
    language?: string
): IssueMatrixLanguage {
    if (language === "kh" || language === "km") return "kh";
    return "en";
}

export const issueMatrixText: Record<
    IssueMatrixLanguage,
    IssueMatrixTranslation
> = {
    en: {
        title: "Issue Matrix",
        subtitle: "Issue Matrix",
        search: "Search",
        exportAsXlsx: "Export as xlsx",
        previous: "Previous",
        next: "Next",
        viewDetail: "View Detail",
        download: "Download",
        noData: "No data found",
        statusChangeDialog: {
            title: "Change issue status?",
            description: (status) =>
                `Are you sure you want to change the status to "${status}"?`,
            confirm: "Change status",
            cancel: "Cancel",
        },
        ministryChangeDialog: {
            title: "Change primary ministry?",
            description: (ministry) =>
                `Are you sure you want to change the primary ministry to "${ministry}"?`,
            confirm: "Change ministry",
            cancel: "Cancel",
        },
        rowUpdateToast: {
            ministryUpdated: "Ministry updated.",
            statusUpdated: "Status updated.",
        },
        plenaryEscalationDialog: {
            escalateTitle: "Escalate this issue to Plenary?",
            escalateDescription:
                "The issue will be flagged for the plenary so it can be prepared and tracked there.",
            escalateConfirm: "Escalate",
            removeTitle: "Remove this issue from Plenary?",
            removeDescription:
                "The issue will no longer be flagged for the plenary.",
            removeConfirm: "Remove",
            cancel: "Cancel",
        },
        plenaryEscalationToast: {
            escalated: "Issue escalated to Plenary.",
            removed: "Issue removed from Plenary.",
            close: "Close notification",
        },
        plenaryEscalationError: "Unable to update the plenary escalation.",

        list: "List",
        grid: "Grid",
        gridCard: {
            no: "No.",
            ministry: "Ministry",
            status: "Status",
            categoryOfIssue: "Category of Issue",
            issue: "Issue",
            escalation: "Escalation",
            issueDescription: "Issue Description",
            recommendation: "Recommendation",
            rgcDecision: "RGC Decision",
            plenary: "Plenary",
            notPlenary: "-",
        },

        totalIssues: "Total Issues",
        solved: "Solved",
        inProgress: "In Progress",
        notAddressed: "Not Addressed",
        totalPrimaryAgencies: "Total Primary Agencies",

        columns: {
            id: "No.",
            workingGroup: "Working Group",
            issue: "Issue",
            category: "Issue Category",
            issueDescription: "Issue Description",
            recommendation: "Recommendation",
            governmentDecision: "Government Decision",
            status: "Status",
            plenaryEscalation: "Plenary Escalation",
            dateOfSolution: "Date of Solution",
            primaryAgency: "Primary Agency",
            meetingType: "Meeting Type",
            wgMeetingDate: "WG Meeting Date",
            secondAgency: "Second Agency",
            thirdAgency: "Third Agency",
            fourthAgency: "Fourth Agency",
            fifthAgency: "Fifth Agency",
            indicator: "Indicator",
            sourceOfVerification: "Source of Verification",
            verificationLink: "Link to Verification Source",
            action: "Action",
        },
    },

    kh: {
        title: "តារាងបញ្ហា",
        subtitle: "តារាងបញ្ហា",
        search: "ស្វែងរក",
        exportAsXlsx: "នាំចេញជា xlsx",
        previous: "មុន",
        next: "បន្ទាប់",
        viewDetail: "មើលលម្អិត",
        download: "ទាញយក",
        noData: "មិនមានទិន្នន័យ",
        statusChangeDialog: {
            title: "ប្តូរស្ថានភាពបញ្ហា?",
            description: (status) =>
                `តើអ្នកប្រាកដថាចង់ប្តូរស្ថានភាពទៅជា «${status}» ឬទេ?`,
            confirm: "ប្តូរស្ថានភាព",
            cancel: "បោះបង់",
        },
        ministryChangeDialog: {
            title: "ប្តូរក្រសួងទទួលបន្ទុក?",
            description: (ministry) =>
                `តើអ្នកប្រាកដថាចង់ប្តូរក្រសួងទទួលបន្ទុកទៅជា «${ministry}» ឬទេ?`,
            confirm: "ប្តូរក្រសួង",
            cancel: "បោះបង់",
        },
        rowUpdateToast: {
            ministryUpdated: "បានប្តូរក្រសួងទទួលបន្ទុក។",
            statusUpdated: "បានប្តូរស្ថានភាព។",
        },
        plenaryEscalationDialog: {
            escalateTitle: "លើកឡើងបញ្ហានេះទៅអង្គប្រជុំពេញអង្គ?",
            escalateDescription:
                "បញ្ហានេះនឹងត្រូវបានសម្គាល់សម្រាប់អង្គប្រជុំពេញអង្គ ដើម្បីរៀបចំ និងតាមដាន។",
            escalateConfirm: "លើកឡើង",
            removeTitle: "ដកបញ្ហានេះចេញពីអង្គប្រជុំពេញអង្គ?",
            removeDescription:
                "បញ្ហានេះនឹងលែងត្រូវបានសម្គាល់សម្រាប់អង្គប្រជុំពេញអង្គទៀតហើយ។",
            removeConfirm: "ដកចេញ",
            cancel: "បោះបង់",
        },
        plenaryEscalationToast: {
            escalated: "បានលើកឡើងបញ្ហាទៅអង្គប្រជុំពេញអង្គ។",
            removed: "បានដកបញ្ហាចេញពីអង្គប្រជុំពេញអង្គ។",
            close: "បិទការជូនដំណឹង",
        },
        plenaryEscalationError: "មិនអាចធ្វើបច្ចុប្បន្នភាពការលើកឡើងបានទេ។",

        list: "បញ្ជី",
        grid: "ក្រឡា",
        gridCard: {
            no: "ល.រ",
            ministry: "ក្រសួង",
            status: "ស្ថានភាព",
            categoryOfIssue: "ប្រភេទបញ្ហា",
            issue: "បញ្ហា",
            escalation: "ការលើកឡើង",
            issueDescription: "ការពិពណ៌នាបញ្ហា",
            recommendation: "អនុសាសន៍",
            rgcDecision: "សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល",
            plenary: "អង្គប្រជុំពេញអង្គ",
            notPlenary: "-",
        },

        totalIssues: "បញ្ហាសរុប",
        solved: "បានដោះស្រាយ",
        inProgress: "កំពុងដំណើរការ",
        notAddressed: "មិនទាន់ដោះស្រាយ",
        totalPrimaryAgencies: "ស្ថាប័នទទួលបន្ទុកសរុប",

        columns: {
            id: "ល.រ",
            workingGroup: "ក្រុមការងារ",
            issue: "បញ្ហា",
            category: "ប្រភេទបញ្ហា",
            issueDescription: "ការពិពណ៌នាបញ្ហា",
            recommendation: "អនុសាសន៍",
            governmentDecision: "សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល",
            status: "ស្ថានភាព",
            plenaryEscalation: "ការលើកឡើងក្នុងអង្គប្រជុំពេញអង្គ",
            dateOfSolution: "កាលបរិច្ឆេទដោះស្រាយ",
            primaryAgency: "ស្ថាប័នទទួលបន្ទុក",
            meetingType: "ប្រភេទកិច្ចប្រជុំ",
            wgMeetingDate: "កាលបរិច្ឆេទកិច្ចប្រជុំក្រុមការងារ",
            secondAgency: "ស្ថាប័នទី២",
            thirdAgency: "ស្ថាប័នទី៣",
            fourthAgency: "ស្ថាប័នទី៤",
            fifthAgency: "ស្ថាប័នទី៥",
            indicator: "សូចនាករ",
            sourceOfVerification: "ប្រភពផ្ទៀងផ្ទាត់",
            verificationLink: "តំណភ្ជាប់ប្រភពផ្ទៀងផ្ទាត់",
            action: "សកម្មភាព",
        },
    },
};

const khmerValueLabels: Record<string, string> = {
    Draft: "ព្រាង",
    Saved: "បានរក្សាទុក",
    "New Submission": "បានដាក់ស្នើថ្មី",
    "In Progress": "កំពុងដំណើរការ",
    Solved: "បានដោះស្រាយ",
    "Not Addressed": "មិនទាន់ដោះស្រាយ",

    Download: "ទាញយក",

    "Working Group": "ក្រុមការងារ",
    "All Status": "ស្ថានភាពទាំងអស់",
    Category: "ប្រភេទ",
    "Primary Agency": "ស្ថាប័នទទួលបន្ទុក",
    Year: "ឆ្នាំ",
    "Progress Report": "របាយការណ៍វឌ្ឍនភាព",
    Attachment: "ឯកសារភ្ជាប់",
    "Has Attachment": "មានឯកសារភ្ជាប់",
    "No Attachment": "មិនមានឯកសារភ្ជាប់",
    Yes: "បាទ/ចាស",
    No: "ទេ",

    Policy: "គោលនយោបាយ",
    Procedure: "នីតិវិធី",
    Legislation: "ច្បាប់",
    Market: "ទីផ្សារ",
    Strategy: "យុទ្ធសាស្ត្រ",
    Governance: "អភិបាលកិច្ច",
    Taxation: "ពន្ធដារ",
    Trade: "ពាណិជ្ជកម្ម",
    Government: "រាជរដ្ឋាភិបាល",
    "Human Resource": "ធនធានមនុស្ស",

    "Not available": "មិនមាន",
    "Not applicable": "មិនពាក់ព័ន្ធ",
    "To be provided": "នឹងផ្តល់ជូន",
};

export function translateIssueMatrixValue(
    value: string | number | boolean | null | undefined,
    language: IssueMatrixLanguage
) {
    if (value === null || value === undefined) return "";

    const textValue = String(value);

    if (language === "en") return textValue;

    return khmerValueLabels[textValue] ?? textValue;
}
