import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";

import { MeetingSummaryKhmerPrintDocument } from "./meeting-summary-khmer-print-document";
import {
  buildMeetingSummaryPrintModel,
  MEETING_SUMMARY_PRINT_MOCK,
} from "./meeting-summary-print-mock-data";

const testDirectory = dirname(fileURLToPath(import.meta.url));

test("meeting summary detail print button uses the generated HTML layout", () => {
  const screenFile = join(testDirectory, "../view-meeting-summary-screen.tsx");
  const source = readFileSync(screenFile, "utf8");

  assert.match(source, /printMeetingSummaryLayout/);
  assert.match(source, /useMockIssues: true/);
  assert.doesNotMatch(source, /printPdfFromUrl/);
});

test("meeting summary print layout uses a landscape page", () => {
  const stylesFile = join(testDirectory, "meeting-summary-print-styles.tsx");
  const source = readFileSync(stylesFile, "utf8");

  assert.match(source, /A4 landscape/);
});

test("meeting summary print document renders signature section below the issue tables", () => {
  const markup = renderToStaticMarkup(
    createElement(MeetingSummaryKhmerPrintDocument, {
      model: MEETING_SUMMARY_PRINT_MOCK,
    }),
  );

  assert.match(markup, /ms-khmer-print-signatures/);
  assert.match(markup, /អ្នកធ្វើរបាយការណ៍/);
  assert.match(markup, /អនុប្រធាន/);
  assert.match(markup, /សហប្រធាន/);
  assert.match(markup, /ថ្ងៃសុក្រ ៥រោច ខែភទ្របទ/);
  assert.match(markup, /ត្រូវនឹងថ្ងៃទី១២ ខែកញ្ញា ឆ្នាំ២០២៥/);
});

test("meeting summary print mock issue table includes the static prototype rows", () => {
  const firstIssue = MEETING_SUMMARY_PRINT_MOCK.issues[0];
  const rowLabels = firstIssue.rows.map((row) => row.labelKh);

  assert.deepEqual(rowLabels, [
    "ប្រភេទបញ្ហា",
    "សេចក្តីសង្ខេបបញ្ហារបស់ផ្នែកឯកជន",
    "សំណើសុំរបស់ផ្នែកឯកជន",
    "ដំណោះស្រាយក្នុងកិច្ចប្រជុំ",
    "សកម្មភាពបន្ទាប់",
    "កំណត់ចំណាំផ្សេងៗ",
    "ឯកសារភ្ជាប់",
    "ស្ថានភាពចុងក្រោយ",
  ]);
  assert.match(firstIssue.rows[2].value, /Capacity Charge/);
  assert.match(firstIssue.rows[7].value, /ពិនិត្យនិងធ្វើអន្តរាគមន៍/);
});

test("meeting summary print model can keep detail header while using static issue rows", () => {
  const model = buildMeetingSummaryPrintModel(
    {
      id: 2,
      share: false,
      privateSectorWg: "ទេសចរណ៍",
      meetingRequestDocument: "",
      meetingRequestDocumentSize: "",
      wgMeeting: "កិច្ចប្រជុំក្រុមការងារ",
      meetingDocument: "",
      meetingDocumentSize: "",
      governmentAgency: "",
      sentBy: "",
      time: "",
      date: "2026-07-07",
      location: "សៀមរាប",
      summaryReference: {
        name: "",
        size: "",
        uploadStatus: "Completed",
        url: null,
      },
      issues: [
        {
          id: 1,
          wgName: "",
          issue: "Dynamic Issue",
          category: "Dynamic Category",
          issueDescription: "Dynamic description",
          recommendation: "Dynamic recommendation",
          rgcDecision: "",
          nextStep: "",
          remark: "",
          issueReference: "",
          status: "Dynamic status",
          primaryAgency: "Dynamic agency",
          secondAgency: "",
          thirdAgency: "",
          fourthAgency: "",
          fifthAgency: "",
          issueEscalation: "",
        },
      ],
    },
    { useMockIssues: true },
  );

  assert.equal(model.sectorKh, "ទេសចរណ៍");
  assert.equal(model.locationKh, "សៀមរាប");
  assert.equal(model.issues[0].titleEn, "Import of frozen meat and offal");
});

test("meeting summary print model uses Khmer lunar calendar package for date lines", () => {
  const model = buildMeetingSummaryPrintModel({
    id: 2,
    share: false,
    privateSectorWg: "ទេសចរណ៍",
    meetingRequestDocument: "",
    meetingRequestDocumentSize: "",
    wgMeeting: "កិច្ចប្រជុំក្រុមការងារ",
    meetingDocument: "",
    meetingDocumentSize: "",
    governmentAgency: "",
    sentBy: "",
    time: "",
    date: "2026-07-07",
    location: "សៀមរាប",
    summaryReference: {
      name: "",
      size: "",
      uploadStatus: "Completed",
      url: null,
    },
    issues: [],
  });

  assert.equal(
    model.meetingLunarDateText,
    "ថ្ងៃអង្គារ ៨រោច ខែបឋមាសាឍ ឆ្នាំមមី អដ្ឋស័ក ពុទ្ធសករាជ ២៥៧០",
  );
  assert.equal(model.meetingGregorianDateText, "ថ្ងៃទី៧ ខែកក្កដា ឆ្នាំ២០២៦");
});
