import assert from "node:assert/strict";
import { describe, test } from "node:test";
import { ageFromDateOfBirth, latestEligibleBirthDate } from "@/features/shared/dateOfBirth";
import { calculateAgeFromDob, YupDateOfBirth } from "@/features/shared/schemas/fields";
import { calculateAge, isUnderage } from "@/features/profile/components/volunteerAccountRules";
import { ageAt, ageToday, isUnderageToday } from "@/lib/age";
import { buildVolunteerAccountValidation } from "@/features/profile/components/volunteerAccountValidation";

for (const threshold of [7, 18, 24]) test(`PDF pages 7/8/10/42: age ${threshold} changes at Kuwait birthday midnight`, () => {
  const dob = `${2026 - threshold}-10-27`;
  assert.equal(ageFromDateOfBirth(dob, new Date("2026-10-26T20:59:59Z")), threshold - 1);
  assert.equal(ageFromDateOfBirth(dob, new Date("2026-10-26T21:00:00Z")), threshold);
  const latest = latestEligibleBirthDate(threshold, new Date("2026-10-26T21:00:00Z"));
  assert.deepEqual([latest.getFullYear(), latest.getMonth() + 1, latest.getDate()], [2026 - threshold, 10, 27]);
});

describe("calendar-date integrity", () => {
  test("impossible dates, appended garbage and future birthdays cannot pass", () => {
    for (const dob of ["2020-02-31", "2019-02-29", "2000-13-01", "2000-00-10", "2000-01-00", "2000-01-32", "0000-01-01", "2000-01-01junk", "2000-01-01T00:00:00Z", "2027-01-01", "", null]) assert.equal(ageFromDateOfBirth(dob, new Date("2026-10-27T12:00:00Z")), null, String(dob));
    assert.equal(ageFromDateOfBirth("2000-02-29", new Date("2026-02-28T12:00:00Z")), 25);
    assert.equal(ageFromDateOfBirth("2000-02-29", new Date("2026-03-01T00:00:00Z")), 26);
  });
  test("the leap-day picker clamps to February instead of admitting March births early", () => {
    const latest = latestEligibleBirthDate(7, new Date("2024-02-29T12:00:00Z"));
    assert.deepEqual([latest.getFullYear(), latest.getMonth() + 1, latest.getDate()], [2017, 2, 28]);
    assert.equal(ageFromDateOfBirth("2017-03-01", new Date("2024-02-29T12:00:00Z")), 6);
  });
  test("registration and account guardian validation use the same birthday policy", async context => {
    context.mock.timers.enable({ apis: ["Date"], now: Date.parse("2026-10-26T21:00:00Z") });
    assert.equal(calculateAgeFromDob("2008-10-27"), 18);
    assert.equal(ageToday("2008-10-27"), 18);
    assert.equal(isUnderageToday("2008-10-27"), false);
    assert.equal(isUnderageToday("2008-10-28"), true);
    assert.equal(ageAt("2008-10-27", "2026-10-26T20:59:59Z"), 17);
    assert.equal(ageAt("2008-10-27", "2026-10-26T21:00:00Z"), 18);
    assert.equal(ageAt("2008-10-27", "2026-10-27"), 18);
    assert.equal(calculateAge("2008-10-27"), 18);
    assert.equal(isUnderage("2008-10-27"), false);
    assert.equal(isUnderage("2008-10-28"), true);
    assert.equal(await YupDateOfBirth(7).isValid("2019-10-27"), true);
    assert.equal(await YupDateOfBirth(7).isValid("2019-10-28"), false);
    assert.equal(await YupDateOfBirth(24).isValid("2002-10-27"), true);
    assert.equal(await YupDateOfBirth(24).isValid("2002-10-28"), false);
    const schema = buildVolunteerAccountValidation({ t: key => key });
    for (const dob of [null, "", "2019-10-27"]) await schema.validateAt("dob", { dob });
    for (const dob of ["2019-10-28", "2018-02-31"]) await assert.rejects(schema.validateAt("dob", { dob }));
    await schema.validateAt("emergency_contact_name", { dob: "2008-10-27", emergency_contact_name: "" });
    await assert.rejects(schema.validateAt("emergency_contact_name", { dob: "2008-10-28", emergency_contact_name: "" }));
  });
});
