/**
 * Regression tests for the opportunity schedule's date handling.
 *
 * The rule these exist for is stated on `toFormDateValue` and is invisible until
 * the app runs west of Greenwich: `new Date("2027-03-05")` is parsed as **UTC**
 * midnight, while `.getDate()` and friends read **local** time — so a bare
 * `yyyy-MM-dd` lands on the previous day, and "today" reads as being in the past.
 * Every assertion below is written to hold in any timezone.
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import {
  deriveScheduleFromDays,
  parseIsoDay,
  toFormDateValue,
  toIsoDay,
} from "@/features/opportunities/components/scheduleDateValues";

describe("toIsoDay", () => {
  test("formats a local Date as yyyy-MM-dd", () => {
    assert.equal(toIsoDay(new Date(2027, 2, 5)), "2027-03-05");
  });

  test("pads single-digit months and days", () => {
    assert.equal(toIsoDay(new Date(2027, 0, 9)), "2027-01-09");
  });

  test("reads local calendar parts, not UTC ones", () => {
    // Late-evening local time is the *next* day in UTC east of Greenwich and the
    // same day west of it; either way the local calendar day is what is stored.
    const evening = new Date(2027, 5, 30, 23, 30);
    assert.equal(toIsoDay(evening), "2027-06-30");
  });
});

describe("parseIsoDay", () => {
  test("parses to local midnight of that calendar day", () => {
    const parsed = parseIsoDay("2027-03-05");
    assert.ok(parsed);
    assert.equal(parsed.getFullYear(), 2027);
    assert.equal(parsed.getMonth(), 2);
    assert.equal(parsed.getDate(), 5);
    assert.equal(parsed.getHours(), 0);
  });

  test("accepts a full timestamp and keeps only the day", () => {
    const parsed = parseIsoDay("2027-03-05T18:45:00.000Z");
    assert.equal(parsed?.getDate(), 5);
    assert.equal(parsed?.getHours(), 0);
  });

  test("returns null for anything that is not a date", () => {
    assert.equal(parseIsoDay(""), null);
    assert.equal(parseIsoDay("not-a-date"), null);
    assert.equal(parseIsoDay("05/03/2027"), null);
  });
});

describe("toFormDateValue — the local/UTC round trip", () => {
  test("the stored value reads back as the SAME calendar day locally", () => {
    // This is the whole point. `new Date("2027-03-05").getDate()` is 4 in any
    // negative-offset timezone; going through local midnight keeps it 5.
    const stored = toFormDateValue("2027-03-05");
    assert.equal(new Date(stored).getDate(), 5);
    assert.equal(new Date(stored).getMonth(), 2);
    assert.equal(new Date(stored).getFullYear(), 2027);
  });

  test("round-trips through toIsoDay unchanged", () => {
    for (const day of ["2027-01-01", "2027-06-30", "2027-12-31"]) {
      assert.equal(toIsoDay(new Date(toFormDateValue(day))), day);
    }
  });

  test("an absent or unparseable day gives an empty string, never 'Invalid Date'", () => {
    assert.equal(toFormDateValue(undefined), "");
    assert.equal(toFormDateValue(""), "");
    assert.equal(toFormDateValue("nonsense"), "");
  });
});

describe("deriveScheduleFromDays", () => {
  test("sorts, because the picker returns click order", () => {
    // Picking the 9th then the 2nd would otherwise store the 9th as startDate,
    // and every date rule, sort and card downstream reads those two.
    const result = deriveScheduleFromDays(["2027-03-09", "2027-03-02"]);
    assert.deepEqual(result.scheduleDates, ["2027-03-02", "2027-03-09"]);
    assert.equal(new Date(result.startDate).getDate(), 2);
    assert.equal(new Date(result.endDate).getDate(), 9);
  });

  test("de-duplicates repeated days", () => {
    const result = deriveScheduleFromDays([
      "2027-03-02",
      "2027-03-02",
      "2027-03-05",
    ]);
    assert.deepEqual(result.scheduleDates, ["2027-03-02", "2027-03-05"]);
  });

  test("a single day is both the start and the end", () => {
    const result = deriveScheduleFromDays(["2027-03-05"]);
    assert.deepEqual(result.scheduleDates, ["2027-03-05"]);
    assert.equal(result.startDate, result.endDate);
    assert.equal(new Date(result.startDate).getDate(), 5);
  });

  test("clearing the selection empties all three, rather than leaving stale dates", () => {
    // Switching mode calls this with [] on purpose; a leftover startDate would
    // keep validating against a schedule the organiser has abandoned.
    const result = deriveScheduleFromDays([]);
    assert.deepEqual(result.scheduleDates, []);
    assert.equal(result.startDate, "");
    assert.equal(result.endDate, "");
  });

  test("sorting is lexicographic, which is chronological for yyyy-MM-dd", () => {
    const result = deriveScheduleFromDays([
      "2027-12-31",
      "2027-01-09",
      "2027-01-10",
    ]);
    assert.deepEqual(result.scheduleDates, [
      "2027-01-09",
      "2027-01-10",
      "2027-12-31",
    ]);
  });
});
