import assert from "node:assert/strict";
import { describe, test } from "node:test";
import moment from "moment";
import { opportunityStartsAt, opportunityEndsAt, hasOpportunityEnded, formatHoursMinutes, parseHoursInput } from "@/features/shared/clientReviewPolicy";
import { getOpportunityButtonState } from "@/features/shared/opportunityButtonState";
import { reviewedChoices } from "@/features/shared/reviewChoices";
import { getCheckInWindow } from "@/features/opportunities/checkInWindow";
import { deriveVolunteerEventView } from "@/features/opportunities/components/volunteer-event/volunteerEventView";
import type { VolunteerOpportunityDetail } from "@/features/opportunities/components/volunteer-event/types";
const now = moment.parseZone("2026-11-01T12:00:00+03:00");
describe("reviewed schedules, units and eligibility", () => {
  test("scattered slots use first actual start and final overnight end", () => {
    const item = { start_date: "2026-11-01", end_date: "2026-11-20", time_slots: [{ date: "2026-11-04", start_time: "22:00", end_time: "01:00" }, { date: "2026-11-02", start_time: "10:00", end_time: "12:00" }] };
    assert.equal(opportunityStartsAt(item)?.format(), "2026-11-02T10:00:00+03:00");
    assert.equal(opportunityEndsAt(item)?.format(), "2026-11-05T01:00:00+03:00");
    assert.equal(hasOpportunityEnded(item, moment.parseZone("2026-11-05T00:30:00+03:00")), false);
  });
  test("registered volunteering has the cutoff state even on a closed list", () => {
    const item = { opportunity_type: "volunteer_opportunity", start_date: "2026-11-02", start_time: "11:00", is_registered: true, is_registration_open: false };
    assert.equal(getOpportunityButtonState(item, now), "registered");
    assert.equal(getOpportunityButtonState({ ...item, start_date: "2026-11-03" }, now), "unregister");
  });
  test("explicitly reopened volunteering may register during execution, development may not", () => {
    const item = { start_date: "2026-10-31", end_date: "2026-11-03", is_registration_open: true, can_register_while_running: true, is_paid: false };
    assert.equal(getOpportunityButtonState({ ...item, opportunity_type: "volunteer_opportunity" }, now), "register");
    assert.equal(getOpportunityButtonState({ ...item, opportunity_type: "learn_serve_opportunity" }, now), "started");
  });
  test("manual preparation fallback ends 72h after the actual end, not end of day", () => {
    const item = { start_date: "2026-10-27", start_time: "08:00", end_date: "2026-10-29", end_time: "09:00" };
    assert.equal(getCheckInWindow(item, now).endsAt?.format(), "2026-11-01T09:00:00+03:00");
    assert.equal(getCheckInWindow(item, now).isClosed, true);
  });
  test("an explicit later admin extension wins over the natural deadline", () => {
    const item = { start_date: "2026-10-27", end_date: "2026-10-29", end_time: "09:00", preparation_reopened_until: "2026-11-02T09:00:00+03:00", is_preparation_window_closed: true };
    assert.equal(getCheckInWindow(item, now).isOpen, true);
  });
  test("actual hours display as H:MM, including Arabic digits and zero", () => {
    assert.equal(formatHoursMinutes(7 + 35 / 60), "7:35");
    assert.equal(formatHoursMinutes("١.٥"), "1:30");
    assert.equal(formatHoursMinutes(null), "0:00");
  });
  test("manual hour input preserves minute precision and enforces 24h ceiling", () => {
    assert.equal(parseHoursInput("1:17"), 77 / 60);
    assert.equal(parseHoursInput("٢:٣٠"), 2.5);
    assert.equal(parseHoursInput("24:00"), 24);
    for (const raw of ["24:01", "1:60", "-1", "", "nonsense"]) assert.equal(parseHoursInput(raw), null);
  });
  test("retired taxonomy is hidden without inventing IDs or mutating API cache", () => {
    const choices = [{ id: 3, value_ar: "تدريب", value_en: "Training" }, { id: 9, value_ar: "ندوات", value_en: "Seminars" }];
    assert.deepEqual(reviewedChoices("event_type", choices).map(x => x.id), [9]);
    assert.equal(choices.length, 2);
    assert.equal(reviewedChoices("user_interest", [{ id: 4, value_ar: "تطوع", value_en: "Volunteering" }])[0].id, 4);
  });
});

describe("reviewed volunteer organizer lifecycle", () => {
  const viewer = { id: 8, signedIn: true, hasAuthToken: true, isVerified: true, userType: "organization" };
  const item: VolunteerOpportunityDetail = { id: 5, title_ar: "فرصة", title_en: "Opportunity", start_date: "2026-11-02", end_date: "2026-11-02", start_time: "15:00:00", end_time: "18:00:00", total_roles: 0, opportunity_images: [], created_by: { id: 8 } };
  test("closed-registration badge disappears at its deadline, before the opportunity ends", () => {
    const closed = { ...item, is_registration_closed: true, registration_closes_at: "2026-11-01T12:00:00+03:00" };
    const visitor = { ...viewer, id: 9 };
    const before = deriveVolunteerEventView(closed, visitor, moment.parseZone("2026-11-01T11:59:59+03:00"));
    const after = deriveVolunteerEventView(closed, visitor, moment.parseZone("2026-11-01T12:00:00+03:00"));
    assert.equal(before.showRegistrationClosedBadge, true);
    assert.equal(after.showRegistrationClosedBadge, false);
    assert.equal(after.isCompleted, false);
    assert.equal(after.closedByCreator, true);
    const legacy = { ...closed, registration_closes_at: undefined, due_date: "2026-11-01" };
    assert.equal(deriveVolunteerEventView(legacy, visitor, moment.parseZone("2026-11-01T20:00:00+03:00")).showRegistrationClosedBadge, true);
    assert.equal(deriveVolunteerEventView(legacy, visitor, moment.parseZone("2026-11-02T00:00:00+03:00")).showRegistrationClosedBadge, false);
  });
  test("edit and preparation controls use the caller's same clock", () => {
    const before = deriveVolunteerEventView(item, viewer, moment.parseZone("2026-11-01T15:00:00+03:00"));
    const after = deriveVolunteerEventView(item, viewer, moment.parseZone("2026-11-01T15:00:01+03:00"));
    assert.equal(before.canEdit, true);
    assert.equal(after.canEdit, false);
    assert.equal(after.isRepostState, false);
  });
  test("a full or deadline-closed volunteer opportunity can reopen with capacity until end", () => {
    const full = { ...item, participants_needed: 10, registered_volunteers_count: 10 };
    assert.equal(deriveVolunteerEventView(full, viewer, now).canReopenRegistration, true);
    assert.equal(deriveVolunteerEventView({ ...item, registration_closes_at: "2026-11-01T10:00:00+03:00" }, viewer, now).canReopenRegistration, true);
    assert.equal(deriveVolunteerEventView(full, viewer, moment.parseZone("2026-11-02T18:00:00+03:00")).canReopenRegistration, false);
  });
  test("after completion QR and repost stay available while messaging and certificates expire independently", () => {
    const completed = deriveVolunteerEventView(item, viewer, moment.parseZone("2026-11-05T18:00:01+03:00"));
    assert.equal(completed.canGenerateAttendanceQr, true);
    assert.equal(completed.isRepostState, true);
    assert.equal(completed.canMessage, false);
    assert.equal(completed.canSendCertificates, false);
    assert.equal(completed.checkInWindow.isClosed, true);
  });
});
