/** Regression tests for ordered, flat opportunity type options. */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  buildOpportunityTypeOptions,
  type ChoiceItem,
  type TypeOption,
  type TypeOptionGroup,
} from "@/features/opportunities/components/opportunityTypeOptions";

const choice = (id: number, en: string, ar: string): ChoiceItem => ({
  id,
  value_en: en,
  value_ar: ar,
});

/** Deliberately supplied out of display order. */
const allChoices: ChoiceItem[] = [
  choice(5, "Internship", "تدريب ميداني"),
  choice(3, "Course", "دورة"),
  choice(1, "Volunteer", "تطوع"),
  choice(4, "Consultation", "استشارة"),
  choice(2, "Class/Workshop", "ورشة"),
];

const build = (
  over: {
    choices?: ChoiceItem[];
    selectedLanguage?: string;
    showVolunteerFields?: boolean;
    showLearnServeFields?: boolean;
  } = {}
) =>
  buildOpportunityTypeOptions({
    // `in` rather than `??`, so an explicit `undefined` reaches the builder.
    choices: "choices" in over ? over.choices : allChoices,
    selectedLanguage: over.selectedLanguage ?? "en",
    showVolunteerFields: over.showVolunteerFields ?? true,
    showLearnServeFields: over.showLearnServeFields ?? true,
  });

const isGroup = (o: TypeOption | TypeOptionGroup): o is TypeOptionGroup =>
  "options" in o;
const flatLabels = (result: Array<TypeOption | TypeOptionGroup>) =>
  result.filter((o): o is TypeOption => !isGroup(o)).map((o) => o.value_en);

describe("display order", () => {
  test("follows the client's fixed order, not the input order or the alphabet", () => {
    // Input is Internship, Course, Volunteer, Consultation, Class/Workshop.
    const result = build();
    assert.deepEqual(flatLabels(result), [
      "Volunteer",
      "Class/Workshop",
      "Course",
      "Consultation",
      "Internship",
    ]);
    assert.ok(!result.some(isGroup));
  });
});

describe("flat type options", () => {
  test("Internship remains selectable without an Events heading", () => {
    const result = build({ showVolunteerFields: false, showLearnServeFields: true });
    assert.ok(!result.some(isGroup));
    const last = result.at(-1);
    assert.ok(last && !isGroup(last));
    assert.equal(last.value, "5");
    assert.ok(flatLabels(result).includes("Internship"));
  });

  test("NO heading appears when there is nothing to group", () => {
    // A lone heading over a flat list reads as noise.
    const result = build({
      choices: [choice(3, "Course", "دورة"), choice(1, "Volunteer", "تطوع")],
    });
    assert.ok(!result.some(isGroup));
    assert.deepEqual(flatLabels(result), ["Volunteer", "Course"]);
  });

  test("an empty choice list gives an empty dropdown, not a bare heading", () => {
    assert.deepEqual(build({ choices: [] }), []);
    assert.deepEqual(build({ choices: undefined }), []);
  });
});

describe("hiding Volunteer on the development-only list", () => {
  test("hidden when the list shows development fields and not volunteering", () => {
    // That list cannot show volunteering opportunities at all, so offering the
    // filter would guarantee an empty result.
    const result = build({ showVolunteerFields: false, showLearnServeFields: true });
    assert.ok(!flatLabels(result).includes("Volunteer"));
  });

  test("kept on the combined list", () => {
    const result = build({ showVolunteerFields: true, showLearnServeFields: true });
    assert.ok(flatLabels(result).includes("Volunteer"));
  });

  test("kept on the volunteering-only list", () => {
    const result = build({ showVolunteerFields: true, showLearnServeFields: false });
    assert.ok(flatLabels(result).includes("Volunteer"));
  });
});

describe("localization and value shape", () => {
  test("labels follow the selected language", () => {
    const en = build({ selectedLanguage: "en" });
    const ar = build({ selectedLanguage: "ar" });
    assert.equal((en[0] as TypeOption).label, "Volunteer");
    assert.equal((ar[0] as TypeOption).label, "تطوع");
  });

  test("values are strings, because SelectInput matches option values as strings", () => {
    // A numeric id here is what leaves a dropdown visibly unset; the same class
    // of bug was found in `learnServeInitialValues`.
    const result = build();
    for (const option of result) {
      if (isGroup(option)) {
        for (const nested of option.options) assert.equal(typeof nested.value, "string");
      } else {
        assert.equal(typeof option.value, "string");
      }
    }
  });

  test("value_en is preserved for the grouping rules to key off", () => {
    const result = build();
    assert.ok((result[0] as TypeOption).value_en === "Volunteer");
  });
});
