import type { IssueRow } from "../../progress-report-view-details.types";
import type { ProgressReportIssuesPrintModel } from "./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 printValue(value: unknown): string {
  if (value === null || value === undefined) {
    return "-";
  }

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

  return text || "-";
}

export function buildPswgProgressReportIssuesPrintModel(
  rows: IssueRow[],
): ProgressReportIssuesPrintModel {
  return {
    ministryName: "Private Sector Working Group",
    ministryLogo: null,
    reportTitle: "របាយការណ៍វឌ្ឍនភាព",
    year: "២០២៥",
    description:
      "របាយការណ៍វឌ្ឍនភាពនេះត្រូវបានរៀបចំឡើងដោយក្រុមការងារផ្នែកឯកជន ដើម្បីបង្ហាញបញ្ហា អនុសាសន៍ វឌ្ឍនភាព និងសកម្មភាពពាក់ព័ន្ធ។",
    issues: rows.map((row, index) => ({
      number: toKhmerDigits(index + 1),
      title: printValue(row.issue),
      rows: [
        {
          labelKh: "ក្រុមការងារ",
          value: printValue(row.pswg),
        },
        {
          labelKh: "ប្រភេទបញ្ហា",
          value: printValue(row.category),
        },
        {
          labelKh: "សេចក្តីពិពណ៌នាអំពីបញ្ហាផ្នែកឯកជន",
          value: printValue(row.issueDescription),
        },
        {
          labelKh: "អនុសាសន៍/សំណើរបស់វិស័យឯកជន",
          value: printValue(row.recommendation),
        },
        {
          labelKh: "សេចក្តីសម្រេចរបស់រាជរដ្ឋាភិបាល",
          value: printValue(row.rgcDecision),
        },
        {
          labelKh: "ស្ថានភាព",
          value: printValue(row.status),
        },
        {
          labelKh: "វឌ្ឍនភាពនៃដំណោះស្រាយ",
          value: printValue(row.progressSolution),
        },
        {
          labelKh: "សូចនាករ",
          value: printValue(row.indicators),
        },
        {
          labelKh: "កាលបរិច្ឆេទដោះស្រាយបញ្ហា",
          value: printValue(row.dateOfIssueSolution),
        },
        {
          labelKh: "ប្រភពផ្ទៀងផ្ទាត់",
          value: printValue(row.sourceOfVerification),
        },
        {
          labelKh: "តំណភ្ជាប់ប្រភពផ្ទៀងផ្ទាត់",
          value: printValue(row.linkToVerificationSource),
        },
        {
          labelKh: "បញ្ហាប្រឈមក្នុងការអនុវត្ត",
          value: printValue(row.implementationChallenges),
        },
        {
          labelKh: "សំណើ/សំណូមពរ",
          value: printValue(row.request),
        },
      ],
    })),
  };
}