import { test, describe } from "node:test";
import assert from "node:assert/strict";
import moment from "moment";
import { computeLearnServeViewState } from "@/features/opportunities/components/learnServeDetailsViewState";
import type { LearnServeOpportunityData } from "@/features/opportunities/components/learnServeDetailsTypes";
const now = moment.parseZone("2026-11-01T12:00:00+03:00");
const record = (over: Partial<LearnServeOpportunityData> = {}) => ({
  id: 77, primary_language: "ar", title_ar: "دورة", title_en: "Course", description_ar: "<p>وصف</p>", description_en: "<p>Description</p>",
  start_date: "2026-11-03", end_date: "2026-11-05", start_time: "10:00", end_time: "12:00",
  opportunity_status: "upcoming", participants_needed: 20, registered_volunteers_count: 0, created_by: { id: 1 }, ...over,
}) as LearnServeOpportunityData;
const view = (over: Partial<LearnServeOpportunityData> = {}, owner = false, type = "volunteer") => computeLearnServeViewState({
  opportunityData: record(over), isCreator: owner, user: { id: owner ? 1 : 2, user_type: type, is_verified: true, auth_token: "token" }, t: key => key, now,
});
describe("development detail: reviewed lifecycle and controls", () => {
  test("stale started/completed flags do not beat a future actual schedule", () => {
    assert.equal(view({ action_state: "started", opportunity_status: "completed" }, true).isRepostState, false);
  });
  test("repost starts at the actual first session and remains available", () => {
    assert.equal(view({ start_date: "2026-11-01", start_time: "12:01" }, true).isRepostState, false);
    assert.equal(view({ start_date: "2026-11-01", start_time: "12:00" }, true).isRepostState, true);
    assert.equal(view({ start_date: "2026-10-01", end_date: "2026-10-02" }, true).actionButtonLabel, "COMMON.REPOST");
  });
  test("creator can edit only before the exact 24-hour cutoff", () => {
    assert.equal(view({}, true).canEdit, true);
    assert.equal(view({ start_date: "2026-11-02", start_time: "11:59" }, true).canEdit, false);
  });
  test("free participant can unregister before cutoff, then reads Registered", () => {
    assert.equal(view({ is_registered: true }).actionButtonLabel, "COMMON.UNREGISTER");
    assert.equal(view({ is_registered: true, start_date: "2026-11-02", start_time: "11:59" }).actionButtonLabel, "COMMON.REGISTERED");
    assert.equal(view({ is_registered: true, start_date: "2026-11-02", start_time: "11:59" }).isPaidBooking, true);
  });
  test("paid Book and Booked are distinct; unpaid attempt returns Book", () => {
    assert.equal(view({ is_paid: true }).actionButtonLabel, "COMMON.BOOK");
    assert.equal(view({ is_paid: true, is_registered: true, payment_status: "paid" }).actionButtonLabel, "COMMON.BOOKED");
    assert.equal(view({ is_paid: true, is_registered: true, payment_status: "pending" }).actionButtonLabel, "COMMON.BOOK");
  });
  test("pending cancellation is disabled and not reported as refunded", () => {
    assert.equal(view({ cancellation_status: "pending", is_paid: true, is_registered: true }).actionButtonLabel, "COMMON.CANCELLATION_PENDING");
    assert.equal(view({ cancellation_status: "pending" }).isPaidBooking, true);
  });
  test("full, closed-window, started and ended controls remain visible and disabled", () => {
    for (const over of [{ participants_needed: 1, registered_volunteers_count: 1 }, { is_registration_open: false }, { start_date: "2026-10-31" }, { start_date: "2026-10-01", end_date: "2026-10-02" }]) {
      assert.equal(view(over).showActionButton, true); assert.equal(view(over).isPaidBooking, true);
    }
    assert.equal(view({ is_registration_open: false }).actionButtonLabel, "COMMON.REGISTRATION_UNAVAILABLE");
  });
  test("organization browsing has no participant action", () => assert.equal(view({}, false, "organization").showActionButton, false));
  test("QR stays printable after completion, messages stop at actual end", () => {
    assert.equal(view({}, true).canIssueAttendanceCode, true);
    assert.equal(view({ start_date: "2026-11-01", start_time: "23:00" }, true).canIssueAttendanceCode, true);
    const ended = view({ start_date: "2026-10-01", end_date: "2026-10-02" }, true);
    assert.equal(ended.canIssueAttendanceCode, true); assert.equal(ended.canMessage, false);
    assert.equal(view({}, true).canMessage, true);
  });
  test("deletion glyph is visible but a historical successful payment locks the action", () => {
    const paid = view({ has_successful_payments: true }, true);
    assert.equal(paid.canRequestDeletion, true); assert.equal(paid.canDelete, false);
    assert.equal(view({}, true).canDelete, true);
  });
  test("single content title falls back safely; description remains sanitized", () => {
    assert.equal(view({ primary_language: "en" }).opportunityTitle, "دورة");
    assert.equal(view({ title_ar: "" }).opportunityTitle, "Course");
    assert.equal(view({ title_ar: "", title_en: "" }).opportunityTitle, "");
    assert.equal(view({ description_ar: '<p>x<script>alert(1)</script></p>' }).description.includes("<script"), false);
  });
});
