import assert from "node:assert/strict";
import { describe, test } from "node:test";
import moment from "moment";
import { developmentScanOpensAt } from "@/features/opportunities/developmentAttendanceWindow";
import { computeLearnServeViewState } from "@/features/opportunities/components/learnServeDetailsViewState";
import type { LearnServeOpportunityData } from "@/features/opportunities/components/learnServeDetailsTypes";

const singleDay = { start_date: "2026-11-03", end_date: "2026-11-03", start_time: "10:00", end_time: "14:00" };
const opens = (item: Parameters<typeof developmentScanOpensAt>[0]) => developmentScanOpensAt(item)?.toISOString();

describe("latest client development QR clarification", () => {
  test("one-day opportunity activates at the actual session start, not one hour early", () => {
    assert.equal(opens(singleDay), "2026-11-03T07:00:00.000Z");
    const activation = developmentScanOpensAt(singleDay)!;
    assert.equal(moment.parseZone("2026-11-03T09:59:59+03:00").isBefore(activation), true);
    assert.equal(moment.parseZone("2026-11-03T10:00:00+03:00").isSameOrAfter(activation), true);
  });
  test("two days activate at the final day's start, rather than the first day or final end", () => {
    assert.equal(opens({ ...singleDay, end_date: "2026-11-04" }), "2026-11-04T07:00:00.000Z");
  });
  test("scattered schedules select the final scheduled day regardless of row order", () => {
    assert.equal(opens({ ...singleDay, end_date: "2026-11-10", timeslots_display: [
      { date: "2026-11-10", start_time: "16:30", end_time: "18:30" },
      { date: "2026-11-03", start_time: "10:00", end_time: "14:00" },
      { date: "2026-11-07", start_time: "11:00", end_time: "13:00" },
    ] }), "2026-11-10T13:30:00.000Z");
  });
  test("the first session on the final day activates a day with multiple sessions", () => {
    assert.equal(opens({ time_slots: [
      { date: "2026-11-04", start_time: "16:00" }, { date: "2026-11-03", start_time: "09:00" },
      { date: "2026-11-04", start_time: "10:00" },
    ] }), "2026-11-04T07:00:00.000Z");
  });
  test("an empty card schedule does not mask the actual detail schedule", () => {
    assert.equal(opens({ time_slots: [], timeslots_display: [{ date: "2026-11-04", start_time: "10:00" }] }), "2026-11-04T07:00:00.000Z");
  });
  test("missing timing does not invent an activation at midnight", () => {
    assert.equal(developmentScanOpensAt({ start_date: "2026-11-03", end_date: "2026-11-04" }), null);
  });
  test("one-day canonical timestamps preserve their Kuwait session instant", () => {
    assert.equal(opens({ start_at: "2026-11-03T07:00:00Z", end_at: "2026-11-03T11:00:00Z" }), "2026-11-03T07:00:00.000Z");
  });
  test("the organizer can prepare the QR well before start and keep it after completion", () => {
    const item = { id: 9, ...singleDay } as LearnServeOpportunityData;
    for (const when of ["2026-10-01T10:00:00+03:00", "2026-11-03T09:59:59+03:00", "2026-11-05T10:00:00+03:00"]) {
      const input = { opportunityData: item, user: { id: 1 }, t: (key: string) => key, now: moment.parseZone(when) };
      assert.equal(computeLearnServeViewState({ ...input, isCreator: true }).canIssueAttendanceCode, true);
      assert.equal(computeLearnServeViewState({ ...input, isCreator: false }).canIssueAttendanceCode, false);
    }
  });
});
