import type { IssueRow } from
    "../progress-report-view-details.types";

import type { ProgressReportIssuesPrintModel } from
    "@/features/ministry/progress-report/components/view-detail/print/progress-report-issues-print-model";

const KHMER_DIGITS: Record<string, string> = {
    "0": "០",
    "1": "១",
    "2": "២",
    "3": "៣",
    "4": "៤",
    "5": "៥",
    "6": "៦",
    "7": "៧",
    "8": "៨",
    "9": "៩",
};

function toKhmerDigits(
    value: string | number,
): string {
    return String(value).replace(
        /[0-9]/g,
        (digit) =>
            KHMER_DIGITS[digit] ?? digit,
    );
}

function printableValue(
    value: unknown,
): string {
    if (
        value === null ||
        value === undefined
    ) {
        return "-";
    }

    const text = String(value).trim();

    return text || "-";
}

export function buildPswgIssuesForMinistryPrint(
    rows: IssueRow[],
): ProgressReportIssuesPrintModel {
    return {
        ministryName:
            "Private Sector Working Group",

        ministryLogo: null,

        privateSectorName:
            "Private Sector Working Group",

        privateSectorLogo:
            "/images/print/pswg-logo.png",

        reportTitle:
            "របាយការណ៍វឌ្ឍនភាព",

        year: "២០២៥",

        description:
            "របាយការណ៍វឌ្ឍនភាពនេះត្រូវបានរៀបចំឡើងដោយក្រុមការងារផ្នែកឯកជន ដើម្បីបង្ហាញបញ្ហា អនុសាសន៍ វឌ្ឍនភាព និងសកម្មភាពពាក់ព័ន្ធ។",

        issues: rows.map(
            (row, index) => ({
                number:
                    toKhmerDigits(index + 1),

                title:
                    printableValue(row.issue),

                rows: [
                    {
                        labelKh: "ក្រុមការងារ",
                        value:
                            printableValue(row.pswg),
                    },
                    {
                        labelKh: "ប្រភេទបញ្ហា",
                        value:
                            printableValue(row.category),
                    },
                    {
                        labelKh:
                            "សេចក្តីពិពណ៌នាអំពីបញ្ហាផ្នែកឯកជន",
                        value:
                            printableValue(
                                row.issueDescription,
                            ),
                    },
                    {
                        labelKh:
                            "អនុសាសន៍/សំណើរបស់វិស័យឯកជន",
                        value:
                            printableValue(
                                row.recommendation,
                            ),
                    },
                    {
                        labelKh:
                            "សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល",
                        value:
                            printableValue(
                                row.rgcDecision,
                            ),
                    },
                    {
                        labelKh: "ស្ថានភាព",
                        value:
                            printableValue(row.status),
                    },
                    {
                        labelKh:
                            "វឌ្ឍនភាពនៃដំណោះស្រាយ",
                        value:
                            printableValue(
                                row.progressSolution,
                            ),
                    },
                    {
                        labelKh: "សូចនាករ",
                        value:
                            printableValue(
                                row.indicators,
                            ),
                    },
                    {
                        labelKh:
                            "កាលបរិច្ឆេទដោះស្រាយបញ្ហា",
                        value:
                            printableValue(
                                row.dateOfIssueSolution,
                            ),
                    },
                    {
                        labelKh:
                            "ប្រភពផ្ទៀងផ្ទាត់",
                        value:
                            printableValue(
                                row.sourceOfVerification,
                            ),
                    },
                    {
                        labelKh:
                            "តំណភ្ជាប់ប្រភពផ្ទៀងផ្ទាត់",
                        value:
                            printableValue(
                                row.linkToVerificationSource,
                            ),
                    },
                    {
                        labelKh:
                            "បញ្ហាប្រឈមក្នុងការអនុវត្ត",
                        value:
                            printableValue(
                                row.implementationChallenges,
                            ),
                    },
                    {
                        labelKh:
                            "សំណើ/សំណូមពរ",
                        value:
                            printableValue(row.request),
                    },
                ],
            }),
        ),
    };
}