/**
 * Full audit (8 Nov 2026) — shared UI primitive regressions.
 *
 *  - F-UI-1: digits typed on an Arabic keyboard («٠١٢…») are kept, not stripped
 *    (phone/number fields, typed dates, typed times).
 *  - F-UI-2: the slot calendar's bounds are calendar days; a bare `YYYY-MM-DD`
 *    must not shift a day for a visitor west of Greenwich.
 *  - F-UI-3: the date calendar floors at the caller's `minDate` (Kuwait's day),
 *    not the browser's today, and does not mutate the caller's date.
 *  - F-UI-4: typing a time starts from the stored 24-hour value.
 *  - F-UI-6: completion-gallery images follow the platform rule (p.42).
 */
import { test, describe } from "node:test";
import assert from "node:assert/strict";
import { keepDigits } from "@/components/ui/digitEntry";
import {
  formatDateInput,
  isCalendarDayDisabled,
  parseTypedDate,
} from "@/components/ui/dateInputValue";
import { typedTimeDraft } from "@/components/ui/timeValue";
import { parseDate } from "@/features/opportunities/components/timeSlotDateHelpers";
import { buildTimeSlotInitialValues } from "@/features/opportunities/components/timeSlotInitialValues";
import {
  isAllowedImageUpload,
  IMAGE_UPLOAD_MAX_BYTES,
} from "@/features/shared/imageUploadRules";

describe("F-UI-1 Arabic-Indic digit entry", () => {
  test("keepDigits converts Arabic-Indic digits and caps the length", () => {
    assert.equal(keepDigits("٩٩٨٨٧٧٦٦"), "99887766");
    assert.equal(keepDigits("۵۵۵ 12-34", 6), "555123");
    assert.equal(keepDigits("abc"), "");
  });

  test("a date typed in Arabic digits formats and parses", () => {
    const typed = formatDateInput("١٠٠٥٢٠٢٦");
    assert.equal(typed, "10-05-2026");
    const parsed = parseTypedDate(typed);
    assert.ok(parsed);
    assert.equal(parsed.getDate(), 10);
    assert.equal(parsed.getMonth(), 4);
  });
});

describe("F-UI-3 calendar floor", () => {
  const kuwaitDay = new Date(2026, 10, 10);
  const browserToday = new Date(2026, 10, 11, 2, 0); // east of Kuwait, after midnight

  test("minDate is the floor, not the browser's today", () => {
    assert.equal(
      isCalendarDayDisabled(new Date(2026, 10, 10), { minDate: kuwaitDay, now: browserToday }),
      false
    );
    assert.equal(
      isCalendarDayDisabled(new Date(2026, 10, 9), { minDate: kuwaitDay, now: browserToday }),
      true
    );
  });

  test("enforceStartDate wins and is not mutated", () => {
    const start = new Date(2026, 10, 20, 15, 30);
    assert.equal(isCalendarDayDisabled(new Date(2026, 10, 19), { enforceStartDate: start }), true);
    assert.equal(isCalendarDayDisabled(new Date(2026, 10, 20), { enforceStartDate: start }), false);
    assert.equal(start.getHours(), 15);
  });

  test("a saved past date stays selectable, other past days do not", () => {
    const selected = new Date(2026, 9, 1);
    const now = new Date(2026, 10, 8);
    assert.equal(isCalendarDayDisabled(new Date(2026, 9, 1), { selected, now }), false);
    assert.equal(isCalendarDayDisabled(new Date(2026, 9, 2), { selected, now }), true);
  });
});

describe("F-UI-4 typed time draft", () => {
  test("seeds 24-hour HH:mm from the stored value", () => {
    assert.equal(typedTimeDraft("14:30:00"), "14:30");
    assert.equal(typedTimeDraft("09:05"), "09:05");
    assert.equal(typedTimeDraft(""), "");
    assert.equal(typedTimeDraft(undefined), "");
  });
});

describe("F-UI-2 slot calendar bounds are calendar days", () => {
  test("YYYY-MM-DD is local midnight in a zone west of UTC", () => {
    const previous = process.env.TZ;
    process.env.TZ = "America/New_York";
    try {
      const end = parseDate("2026-11-20");
      assert.ok(end);
      assert.equal(end.getDate(), 20);
      assert.equal(end.getHours(), 0);
      // A slot stored on the last day is still inside the window when editing.
      const values = buildTimeSlotInitialValues({
        editTimeSlot: { date: "2026-11-20", start_time: "10:00", end_time: "12:00", needed: 3 },
        opportunityStartDate: parseDate("2026-11-10"),
        opportunityEndDate: end,
      } as unknown as Parameters<typeof buildTimeSlotInitialValues>[0]);
      assert.equal(values.date, "2026-11-20");
    } finally {
      process.env.TZ = previous;
    }
  });

  test("an ISO instant still parses as before", () => {
    const parsed = parseDate("2026-11-20T21:00:00.000Z");
    assert.ok(parsed);
    assert.equal(parsed.toISOString(), "2026-11-20T21:00:00.000Z");
  });
});

describe("F-UI-6 completion-gallery image rule", () => {
  const file = (type: string, size: number) =>
    ({ type, size, name: "x" }) as unknown as File;

  test("JPG, PNG and WEBP up to 10 MB pass; others fail", () => {
    assert.equal(isAllowedImageUpload(file("image/webp", 1024)), true);
    assert.equal(isAllowedImageUpload(file("image/png", IMAGE_UPLOAD_MAX_BYTES)), true);
    assert.equal(isAllowedImageUpload(file("image/jpeg", IMAGE_UPLOAD_MAX_BYTES + 1)), false);
    assert.equal(isAllowedImageUpload(file("image/gif", 10)), false);
    assert.equal(isAllowedImageUpload(file("application/pdf", 10)), false);
  });
});
