import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { describe, test } from "node:test";
import { certificateKindLabelKey, profileTabs } from "@/features/profile/components/individual/individualProfile";
import { reportRowKey, reportTableColumns } from "@/features/achievements/reportEntryPolicy";

const ar = JSON.parse(readFileSync(new URL("../locales/ar/translation.json", import.meta.url), "utf8")) as Record<string, string>;
const en = JSON.parse(readFileSync(new URL("../locales/en/translation.json", import.meta.url), "utf8")) as Record<string, string>;

describe("F-PRO-1 — X2: a volunteer team profile has no Events tab", () => {
  const keys = (kind: "individual" | "team" | "organization", own: boolean) => profileTabs(kind, own).map(([tab]) => tab);
  test("team: opportunities only, own or public", () => {
    assert.deepEqual(keys("team", true), ["opportunities"]);
    assert.deepEqual(keys("team", false), ["opportunities"]);
  });
  test("organization keeps Events; individual gets Certificates on their own profile only", () => {
    assert.deepEqual(keys("organization", false), ["opportunities", "events"]);
    assert.deepEqual(keys("individual", true), ["opportunities", "certificates"]);
    assert.deepEqual(keys("individual", false), ["opportunities"]);
  });
});

describe("F-PRO-5 — PDF p.47 report table headers", () => {
  test("development table: «الدورة أو التدريب» … «الأتعاب», none on a team report", () => {
    assert.deepEqual(reportTableColumns("organization", "development"), ["REPORT_COL_COURSE", "REPORT_COL_ROLE", "TYPE", "REPORT_LEARNERS", "DATE"]);
    assert.deepEqual(reportTableColumns("team", "development"), ["REPORT_COL_COURSE", "REPORT_COL_ROLE", "TYPE", "REPORT_LEARNERS", "DATE"]);
    assert.deepEqual(reportTableColumns("individual", "development"), ["REPORT_COL_COURSE", "REPORT_COL_ROLE", "TYPE", "DATE"]);
  });
  test("sponsorship and volunteering tables", () => {
    assert.deepEqual(reportTableColumns("organization", "sponsorship"), ["REPORT_COL_INITIATIVE", "REPORT_COL_EXECUTOR", "REPORT_COL_INITIATIVE_TYPE", "REPORT_BENEFICIARIES", "REPORT_LEARNERS", "DATE"]);
    assert.deepEqual(reportTableColumns("individual", "volunteer"), ["REPORT_COL_TITLE", "REPORT_COL_ROLE", "TYPE", "REPORT_COL_PUBLISHER", "VOLUNTEER_HOURS", "DATE"]);
    assert.deepEqual(reportTableColumns("team", "volunteer"), ["REPORT_COL_TITLE", "REPORT_COL_ROLE", "TYPE", "REPORT_BENEFICIARIES", "REPORT_COL_ATTENDEES", "REPORT_COL_TOTAL_HOURS", "REPORT_ECONOMIC_IMPACT", "DATE"]);
  });
  test("every header key is translated in both languages", () => {
    const all = new Set((["individual", "team", "organization"] as const).flatMap(kind => (["volunteer", "development", "sponsorship"] as const).flatMap(section => reportTableColumns(kind, section))));
    for (const key of all) {
      assert.ok(ar[`COMMON.${key}`], `ar COMMON.${key}`);
      assert.ok(en[`COMMON.${key}`], `en COMMON.${key}`);
    }
    assert.equal(ar["COMMON.REPORT_COL_COURSE"], "الدورة أو التدريب");
    assert.equal(ar["COMMON.REPORT_COL_FEES"], "الأتعاب");
  });
});

describe("F-PRO-6 — sponsorship rows with the same id stay distinct", () => {
  test("a volunteering and a development row sharing id 7 get different keys", () => {
    const volunteer = { id: 7 };
    const development = { id: 7, learning_type: null };
    assert.notEqual(reportRowKey(volunteer), reportRowKey(development));
  });
});

describe("F-PRO-8 — certificate side label is localized", () => {
  test("kinds map to translated keys", () => {
    for (const kind of ["volunteering", "course", "internship"]) {
      const key = certificateKindLabelKey(kind);
      assert.ok(ar[key] && en[key], key);
    }
    assert.equal(ar[certificateKindLabelKey("internship")], "تدريب عملي");
  });
});

describe("F-PRO-9 — PDF p.30 leaderboard title separator «·»", () => {
  test("titles read «أبطال التطوع · …»", () => {
    for (const scope of ["MONTHLY", "QUARTERLY", "SEMI_ANNUAL", "ANNUAL"]) {
      assert.match(ar[`ACHIEVEMENTS.VOLUNTEER_HEROES_${scope}`], /^أبطال التطوع · /);
      assert.match(en[`ACHIEVEMENTS.VOLUNTEER_HEROES_${scope}`], /^Volunteer Heroes · /);
    }
  });
});
