/**
 * Frontend closure (8 Nov 2026, backend 3fe127d) — coordinator fixes C-COORD-n.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { toHeroBanners } from "@/features/home/homeHeroBanners";
import { skippedFromSelection } from "@/features/opportunities/services/learnServe";

describe("C-COORD-1: the home hero shows only home-placement banners (BE-290 via /banner-images/)", () => {
  test("rows without a file are dropped, newest first, and every slide gets a stable key", () => {
    // `/banner-images/` answers in insertion order; the hero has always shown the newest first.
    assert.deepEqual(
      toHeroBanners([
        { image: "https://x/a.png", banner_url: "https://fursa/a" },
        { image: null, banner_url: "https://fursa/b" },
        { image: "https://x/c.png" },
      ]),
      [
        { id: 1, image: "https://x/c.png", banner_url: null },
        { id: 2, image: "https://x/a.png", banner_url: "https://fursa/a" },
      ]
    );
    assert.deepEqual(toHeroBanners(null), []);
  });

  test("neither hero entry point reads `hero.banners` from GET /home/ any more", () => {
    for (const file of [
      "src/features/home/components/Banner.tsx",
      "src/features/home/components/HomepageBannerClient.tsx",
    ]) {
      const source = readFileSync(file, "utf8");
      assert.doesNotMatch(source, /hero\?*\.banners/, file);
      assert.match(source, /toHeroBanners\(/, file);
    }
  });

  test("the client shares PlacementBanner's cache key for the home placement", () => {
    const source = readFileSync("src/features/home/components/HomepageBannerClient.tsx", "utf8");
    assert.match(source, /queryKey:\s*\["banner-images",\s*"home"\]/);
  });
});

describe("C-COORD-2: a failed review update has a translated fallback, not a raw key", () => {
  test("the key used by useOpportunityFeedback exists in both languages", () => {
    const source = readFileSync("src/features/opportunities/components/useOpportunityFeedback.ts", "utf8");
    assert.doesNotMatch(source, /"COMMON\.TOAST\.UPDATE_FAILED"/);
    for (const lang of ["ar", "en"]) {
      const messages = JSON.parse(readFileSync(`locales/${lang}/translation.json`, "utf8")) as Record<string, string>;
      assert.ok(messages["COMMON.TOAST.UPDATE_FEEDBACK_FAILED"], lang);
    }
  });
});

describe("C-COORD-3: «skipped unpaid» counts only the organizer's selection", () => {
  test("selection minus the server's updated_count, never the course-wide skipped_unpaid_count", () => {
    // Two paid people selected on a course with five unpaid registrants elsewhere.
    assert.equal(skippedFromSelection(2, 2), 0);
    assert.equal(skippedFromSelection(3, 1), 2);
    assert.equal(skippedFromSelection(2, 5), 0);
    assert.equal(skippedFromSelection(2, undefined), 0);
  });

  test("the register list no longer reads skipped_unpaid_count", () => {
    const source = readFileSync("src/features/opportunities/components/useLearnServeRegisterListActions.ts", "utf8");
    assert.doesNotMatch(source, /skipped_unpaid_count/);
    assert.match(source, /skippedFromSelection\(registrationIds\.length, response\.data\?\.updated_count\)/);
  });
});

describe("C-COORD-4: the volunteering roles table counts «متطوع», not «مشارك» (p.37 vocabulary)", () => {
  test("every Arabic plural form of PARTICIPANTS_STILL_NEEDED says متطوع", () => {
    const ar = JSON.parse(readFileSync("locales/ar/translation.json", "utf8")) as Record<string, string>;
    const forms = Object.keys(ar).filter((key) => key.startsWith("COMMON.PARTICIPANTS_STILL_NEEDED"));
    assert.ok(forms.length >= 6);
    for (const key of forms) {
      assert.match(ar[key], /متطوع/, key);
      assert.doesNotMatch(ar[key], /مشارك/, key);
    }
  });
});
