import assert from "node:assert/strict";
import test from "node:test";
import { groupTranslationsByPage, filterTranslationPageModules } from "./use-translation-page-groups";
import type { Translation } from "../types/translation-types";

function row(overrides: Partial<Translation>): Translation {
  return {
    id: 7, module: "pswg", page: "progress_report", translationKey: "title",
    parentId: null, englishText: "Progress Report", khmerText: "របាយការណ៍វឌ្ឍនភាព",
    isActive: true, createdAt: "2026-09-01T00:00:00Z", updatedAt: "2026-09-23T00:00:00Z",
    ...overrides,
  };
}

test("module checkboxes filter multiple modules and retain their children", () => {
  const groups = groupTranslationsByPage([
    row({ id: 1, module: "pswg", page: "dashboard" }),
    row({ id: 2, module: "ministry", page: "dashboard" }),
    row({ id: 3, module: "cdc", page: "dashboard" }),
    row({ id: 4, module: "ministry", page: "plenary_dashboard", parentId: 2 }),
  ]);
  assert.deepEqual(filterTranslationPageModules(groups, ["pswg", "ministry"]).map(group => group.id).sort(), [1, 2, 4]);
  assert.deepEqual(filterTranslationPageModules(groups, "cdc").map(group => group.id), [3]);
  assert.equal(filterTranslationPageModules(groups, "all"), groups);
  assert.equal(filterTranslationPageModules(groups, []), groups);
});

test("Ministry Plenary is an expandable root with three independent child pages", () => {
  const parent = row({ id: 859, module: "ministry", page: "sidebar", translationKey: "group.plenary", englishText: "Plenary" });
  const pages = ["plenary_dashboard", "plenary_plenaries", "plenary_rgc_decisions"];
  const children = pages.map((page, index) => row({ id: 90 + index, module: "ministry", page, parentId: parent.id }));
  const groups = groupTranslationsByPage([
    ...children.map((child, index) => row({ ...child, id: 1000 + index, translationKey: "page_title", parentId: child.id })),
    ...children,
    parent,
    row({ id: 83, module: "ministry", page: "dashboard" }),
    row({ id: 1, module: "pswg", page: "dashboard" }),
  ]);
  assert.equal(groups.find(group => group.id === parent.id)?.parentId, null);
  assert.deepEqual(new Set(groups.filter(group => group.parentId === parent.id).map(group => group.page)), new Set(pages));
  assert.equal(groups.find(group => group.id === 83)?.parentId, null);
  assert.equal(groups.find(group => group.id === 1)?.parentId, null);
  assert.equal(groups.filter(group => group.module === "ministry" && group.parentId === null).length, 2);
});
test("CDC G-PSF Admin groups WG and Plenary pages without hiding standalone pages", () => {
  const wg = row({ id: 860, module: "cdcgpsf", page: "sidebar", translationKey: "group.wg", englishText: "WG" });
  const plenary = row({ id: 861, module: "cdcgpsf", page: "sidebar", translationKey: "group.plenary", englishText: "Plenary" });
  const wgPages = ["dashboard", "meeting_requests", "meeting_calendar", "meeting_summary", "issue_matrix"];
  const plenaryPages = ["plenary_dashboard", "plenary_plenaries", "plenary_rgc_decisions"];
  const children = [
    ...wgPages.map((page, index) => row({ id: 900 + index, module: "cdcgpsf", page, parentId: wg.id })),
    ...plenaryPages.map((page, index) => row({ id: 910 + index, module: "cdcgpsf", page, parentId: plenary.id })),
  ];
  const standalone = row({ id: 920, module: "cdcgpsf", page: "progress_reports", parentId: null });
  const groups = groupTranslationsByPage([standalone, ...children, wg, plenary]);
  assert.deepEqual(new Set(groups.filter(group => group.parentId === wg.id).map(group => group.page)), new Set(wgPages));
  assert.deepEqual(new Set(groups.filter(group => group.parentId === plenary.id).map(group => group.page)), new Set(plenaryPages));
  assert.deepEqual(new Set(groups.filter(group => group.parentId === null).map(group => group.id)), new Set([wg.id, plenary.id, standalone.id]));
});
test("root Progress Report remains visible when a child label arrives first", () => {
  const title = row({});
  const label = row({id: 2589, translationKey: "page_title", parentId: title.id});
  for (const items of [[label, title], [title, label]]) {
    const groups = groupTranslationsByPage(items);
    assert.equal(groups.length, 1);
    assert.equal(groups[0].id, title.id);
    assert.equal(groups[0].parentId, null);
    assert.equal(groups.filter(group => group.parentId === null).length, 1);
    assert.equal(groups[0].labelCount, 2);
  }
});
test("real nested dashboard pages retain their title's parent", () => {
  const title = row({id: 3, page: "dashboard_plenary", parentId: 1});
  const label = row({id: 40, page: "dashboard_plenary", translationKey: "page_title", parentId: 3});
  const [group] = groupTranslationsByPage([label, title]);
  assert.equal(group.id, 3);
  assert.equal(group.parentId, 1);
});
test("root grouping stays separate for PSWG and Ministry", () => {
  const groups = groupTranslationsByPage([
    row({id: 90, translationKey: "page_title", parentId: 7}),
    row({}),
    row({id: 91, module: "ministry", page: "progress_reports", translationKey: "page_title", parentId: 8}),
    row({id: 8, module: "ministry", page: "progress_reports"}),
  ]);
  assert.equal(groups.filter(group => group.parentId === null).length, 2);
  assert.deepEqual(new Set(groups.map(group => group.module)), new Set(["pswg", "ministry"]));
});
