/**
 * Regression tests for the opportunity list query string.
 *
 * This is the filter contract with the API, and the two list screens had it
 * written out twice — so a filter added to one did nothing on the other. Three of
 * its rules are easy to "simplify" into a different result set, and none of them
 * fails to compile when broken:
 *
 *  - a boolean filter must be **omitted** when false, not sent as `false`;
 *  - `0` is a legitimate age bound and must survive;
 *  - dates must be reduced to `YYYY-MM-DD`.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  buildOpportunityListParams,
  opportunityListEndpoint,
} from "@/features/opportunities/components/opportunityListQueryParams";
import {
  EMPTY_OPPORTUNITY_FILTERS,
  type AllOpportunitiesFiltersData,
} from "@/features/opportunities/components/opportunityFilters";

const filters = (
  over: Partial<AllOpportunitiesFiltersData> = {}
): AllOpportunitiesFiltersData => ({ ...EMPTY_OPPORTUNITY_FILTERS(), ...over });

const build = (
  over: Partial<AllOpportunitiesFiltersData> = {},
  isLearnServe = false
) =>
  buildOpportunityListParams({
    page: 1,
    limit: 9,
    searchQuery: "",
    filters: filters(over),
    isLearnServe,
  });

describe("opportunityListEndpoint", () => {
  test("each kind has its own list route", () => {
    assert.equal(opportunityListEndpoint(false), "/list-volunteer-opportunities/");
    assert.equal(opportunityListEndpoint(true), "/learn-serve-opportunities/");
    assert.equal(opportunityListEndpoint(undefined), "/list-volunteer-opportunities/");
  });
});

describe("boolean filters are omitted when false, never sent as false", () => {
  test("volunteering flags", () => {
    // Sending `is_urgent=false` asks the API for *non-urgent* opportunities,
    // which is a filtered subset — not "do not filter".
    const off = build({ isRelief: false, isUrgent: false, isSpecialNeed: false });
    assert.equal(off.is_relief, undefined);
    assert.equal(off.is_emergency, undefined);
    assert.equal(off.is_supports_disabled, undefined);

    const on = build({ isRelief: true, isUrgent: true, isSpecialNeed: true });
    assert.equal(on.is_relief, true);
    assert.equal(on.is_emergency, true);
    assert.equal(on.is_supports_disabled, true);
  });

  test("development format flags", () => {
    const off = build({ isInPerson: false, isOnline: false }, true);
    assert.equal(off.in_person, undefined);
    assert.equal(off.online, undefined);

    const on = build({ isInPerson: true, isOnline: true }, true);
    assert.equal(on.in_person, true);
    assert.equal(on.online, true);
  });

  test("matchMyInterest and sortBy are omitted when unset", () => {
    const none = build();
    assert.equal(none.match_my_interest, undefined);
    assert.equal(none.sort_by, undefined);
    const set = build({ matchMyInterest: true, sortBy: "newest" });
    assert.equal(set.match_my_interest, true);
    assert.equal(set.sort_by, "newest");
  });
});

describe("each kind sends only its own flags", () => {
  test("volunteering never sends the format flags", () => {
    const p = build({ isInPerson: true, isOnline: true }, false);
    assert.ok(!("in_person" in p));
    assert.ok(!("online" in p));
  });

  test("development never sends the volunteering flags", () => {
    const p = build({ isRelief: true, isUrgent: true, isSpecialNeed: true }, true);
    assert.ok(!("is_relief" in p));
    assert.ok(!("is_urgent" in p));
    assert.ok(!("is_supports_disabled" in p));
  });

  test("both send the shared filters", () => {
    for (const isLearnServe of [false, true]) {
      const p = build({ location: "Kuwait", gender: "2", type: "x", status: "open" }, isLearnServe);
      assert.equal(p.location, "Kuwait");
      assert.equal(p.gender, "2");
      assert.equal(p.status, "open");
    }
  });

  /* The development endpoint honours only `learning_type_id` / `learning_type`;
     `type` there is a `filter-type` id that only ever empties the list. */
  test("development sends the type as learning_type_id, volunteering as type", () => {
    const dev = build({ type: "3" }, true);
    assert.equal(dev.learning_type_id, "3");
    assert.ok(!("type" in dev));
    const vol = build({ type: "1" }, false);
    assert.equal(vol.type, "1");
    assert.ok(!("learning_type_id" in vol));
  });

  test("no type selected sends no learning_type_id", () => {
    assert.equal(build({ type: "" }, true).learning_type_id, undefined);
  });
});

describe("dates and ages", () => {
  test("dates are reduced to YYYY-MM-DD", () => {
    const p = build({
      startDate: new Date(2027, 2, 5).toISOString(),
      endDate: new Date(2027, 2, 9).toISOString(),
    });
    assert.equal(p.start_date, "2027-03-05");
    assert.equal(p.end_date, "2027-03-09");
  });

  test("absent dates are omitted, not sent as empty strings", () => {
    const p = build({ startDate: "", endDate: "" });
    assert.equal(p.start_date, undefined);
    assert.equal(p.end_date, undefined);
  });

  test("age bounds pass through, and zero survives", () => {
    // A truthiness check would drop 0, which is a real lower bound.
    const p = build({ age: [0, 30] as [number | null, number | null] });
    assert.equal(p.min_age, 0);
    assert.equal(p.max_age, 30);
  });

  test("an unset age bound is omitted", () => {
    const p = build({ age: [null, null] as [number | null, number | null] });
    assert.equal(p.min_age, undefined);
    assert.equal(p.max_age, undefined);
  });

  test("one bound set, the other not", () => {
    const p = build({ age: [18, null] as [number | null, number | null] });
    assert.equal(p.min_age, 18);
    assert.equal(p.max_age, undefined);
  });
});

describe("paging and search always ride along", () => {
  test("page and limit are sent verbatim", () => {
    const p = buildOpportunityListParams({
      page: 3,
      limit: 8,
      searchQuery: "tree",
      filters: filters(),
      isLearnServe: false,
    });
    assert.equal(p.page, 3);
    assert.equal(p.limit, 8);
    assert.equal(p.search, "tree");
  });

  test("tags go as the array the API expects", () => {
    const p = build({ tags: ["health", "environment"] });
    assert.deepEqual(p.tags, ["health", "environment"]);
  });
});
