/**
 * Regression tests for the achievements report's activity list.
 *
 * The rule worth protecting is the de-duplication key. The source endpoints
 * overlap — a registered opportunity is also an *attended* one once it ends —
 * and each endpoint numbers its rows separately, so the same activity arrives
 * twice with different ids. Identity therefore has to be the localized titles
 * plus the year. Keying on `id` would show every finished activity twice.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  dedupeRows,
  toActivityRows,
  yearOf,
  type ActivityRow,
} from "@/features/achievements/components/activityRows";

const row = (over: Partial<ActivityRow> = {}): ActivityRow => ({
  key: "k",
  title_en: "Beach cleanup",
  title_ar: "تنظيف الشاطئ",
  year: 2026,
  kind: "opportunity",
  ...over,
});

describe("yearOf", () => {
  test("takes the first date that parses", () => {
    assert.equal(yearOf("2026-08-13"), 2026);
    assert.equal(yearOf(null, "2027-01-01"), 2027);
    assert.equal(yearOf(undefined, "", "2025-12-31"), 2025);
  });

  test("skips unparseable values rather than returning NaN", () => {
    assert.equal(yearOf("not-a-date", "2026-08-13"), 2026);
  });

  test("is undefined when nothing parses", () => {
    assert.equal(yearOf(), undefined);
    assert.equal(yearOf(null, undefined, ""), undefined);
  });
});

describe("toActivityRows", () => {
  test("maps an array, deriving the year from the first usable date", () => {
    const rows = toActivityRows(
      [{ id: 7, title_en: "A", title_ar: "أ", start_date: "2026-03-01" }],
      "opportunity",
      "opp"
    );
    assert.equal(rows.length, 1);
    assert.deepEqual(rows[0], {
      key: "opp-7",
      title_en: "A",
      title_ar: "أ",
      year: 2026,
      kind: "opportunity",
    });
  });

  test("falls back to the index when an item has no id", () => {
    const rows = toActivityRows([{ title_en: "A" }, { title_en: "B" }], "event", "ev");
    assert.deepEqual(rows.map((r) => r.key), ["ev-0", "ev-1"]);
  });

  test("missing titles become empty strings, not undefined", () => {
    const rows = toActivityRows([{ id: 1 }], "event", "ev");
    assert.equal(rows[0].title_en, "");
    assert.equal(rows[0].title_ar, "");
  });

  test("anything that is not an array yields no rows", () => {
    assert.deepEqual(toActivityRows(null, "event", "ev"), []);
    assert.deepEqual(toActivityRows(undefined, "event", "ev"), []);
    assert.deepEqual(toActivityRows({}, "event", "ev"), []);
    assert.deepEqual(toActivityRows("nope", "event", "ev"), []);
  });

  test("uses the due date when there is no start or end", () => {
    const rows = toActivityRows([{ id: 1, due_date: "2028-05-05" }], "event", "ev");
    assert.equal(rows[0].year, 2028);
  });
});

describe("dedupeRows", () => {
  test("collapses the same activity arriving from two endpoints", () => {
    // Same titles and year, different ids — which is exactly the overlap case.
    const rows = [row({ key: "registered-1" }), row({ key: "attended-99" })];
    const deduped = dedupeRows(rows);
    assert.equal(deduped.length, 1);
  });

  test("keeps the last occurrence, so the later source wins", () => {
    const deduped = dedupeRows([row({ key: "first" }), row({ key: "second" })]);
    assert.equal(deduped[0].key, "second");
  });

  test("keeps two activities that differ only by year", () => {
    const deduped = dedupeRows([row({ year: 2025 }), row({ year: 2026 })]);
    assert.equal(deduped.length, 2);
  });

  test("keeps two activities that differ only by title", () => {
    const deduped = dedupeRows([row(), row({ title_en: "Tree planting" })]);
    assert.equal(deduped.length, 2);
  });

  test("distinguishes by the Arabic title too, not just the English one", () => {
    const deduped = dedupeRows([row(), row({ title_ar: "زراعة الأشجار" })]);
    assert.equal(deduped.length, 2);
  });

  test("an undefined year is its own identity, not a wildcard", () => {
    const deduped = dedupeRows([row({ year: undefined }), row({ year: 2026 })]);
    assert.equal(deduped.length, 2);
  });

  test("does NOT collapse on kind — an opportunity and event can share a title", () => {
    /*
     * The key is title + year only, so a same-named opportunity and event in
     * the same year collapse into one row. Recorded as the shipped behaviour:
     * whether that is right is a product question, and the de-duplication
     * exists precisely because the sources overlap.
     */
    const deduped = dedupeRows([row({ kind: "opportunity" }), row({ kind: "event" })]);
    assert.equal(deduped.length, 1);
  });

  test("an empty list stays empty", () => {
    assert.deepEqual(dedupeRows([]), []);
  });
});
