/**
 * Backend delivery `7fc047d` (8 November 2026): the publisher's refund-all is
 * open while the course runs, and every state the server reports is shown as
 * it is — nothing reads as refunded before the server says `completed`.
 */
import { test } from "node:test";
import assert from "node:assert/strict";
import moment from "moment";

import { canOpenRefundAll } from "@/features/opportunities/participantPaymentRecord";
import { existingRequestStateKey } from "@/features/opportunities/services/reviewRequests";
import { publisherRefundAllKey, shouldReadCancelledCourseRefund } from "@/features/opportunities/cancelledCourseRefund";
import { getOpportunityButtonState } from "@/features/shared/opportunityButtonState";

const paid = [{ payment_status: "paid" as const }];
const schedule = { start_at: "2026-11-10T10:00:00+03:00", end_at: "2026-11-12T12:00:00+03:00" };
const at = (iso: string) => moment.parseZone(iso);

test("A9 + 5 Nov confirmation: refund-all opens before the start and while the course runs", () => {
  assert.equal(canOpenRefundAll(paid, schedule, at("2026-11-09T09:00:00+03:00")), true);
  assert.equal(canOpenRefundAll(paid, schedule, at("2026-11-11T09:00:00+03:00")), true);
});

test("course_ended boundary: closed from the end instant, and on a cancelled course", () => {
  assert.equal(canOpenRefundAll(paid, schedule, at("2026-11-12T11:59:59+03:00")), true);
  assert.equal(canOpenRefundAll(paid, schedule, at("2026-11-12T12:00:00+03:00")), false);
  assert.equal(canOpenRefundAll(paid, { ...schedule, opportunity_status: "cancelled" }, at("2026-11-11T09:00:00+03:00")), false);
});

test("Kuwait wall time: an offset-free end is read as +03:00, not the browser zone", () => {
  const naive = { start_at: "2026-11-10 10:00:00", end_at: "2026-11-12 12:00:00" };
  assert.equal(canOpenRefundAll(paid, naive, moment.utc("2026-11-12T08:59:00Z")), true);
  assert.equal(canOpenRefundAll(paid, naive, moment.utc("2026-11-12T09:00:00Z")), false);
});

test("refund-all request states: pending is review, processing is in progress, only completed is done", () => {
  assert.equal(existingRequestStateKey("publisher", "pending"), "COMMON.REFUND_ALL_PENDING");
  assert.equal(existingRequestStateKey("publisher", "processing"), "COMMON.REFUND_IN_PROGRESS");
  assert.equal(existingRequestStateKey("publisher", "completed"), "COMMON.REFUND_ALL_COMPLETED");
  assert.equal(existingRequestStateKey("publisher", "available"), null);
  assert.equal(existingRequestStateKey("publisher", undefined), null);
});

test("participant cancellation states: approved-and-refunding (or failed) is never shown as refunded", () => {
  assert.equal(existingRequestStateKey("participant", "pending"), "COMMON.CANCELLATION_PENDING");
  assert.equal(existingRequestStateKey("participant", "awaiting_trainer"), "COMMON.CANCELLATION_PENDING");
  assert.equal(existingRequestStateKey("participant", "refunding"), "COMMON.REFUND_IN_PROGRESS");
  assert.equal(existingRequestStateKey("participant", "refund_failed"), "COMMON.REFUND_IN_PROGRESS");
  assert.equal(existingRequestStateKey("participant", "available"), null);
  assert.equal(existingRequestStateKey("participant", "rejected"), null);
});

test("publisher banner follows refund_all_status; nothing for others or a rejected request", () => {
  assert.equal(publisherRefundAllKey({ refund_all_status: "pending" }, true), "COMMON.REFUND_ALL_PENDING");
  assert.equal(publisherRefundAllKey({ refund_all_status: "processing" }, true), "COMMON.REFUND_IN_PROGRESS");
  assert.equal(publisherRefundAllKey({ refund_all_status: "completed" }, true), "COMMON.REFUND_ALL_COMPLETED");
  assert.equal(publisherRefundAllKey({ refund_all_status: "rejected" }, true), null);
  assert.equal(publisherRefundAllKey({ refund_all_status: "pending" }, false), null);
  assert.equal(publisherRefundAllKey(null, true), null);
});

test("a running course hidden by refund-all (status cancelled, action_state ended) offers no booking", () => {
  const hidden = {
    opportunity_type: "learn_serve_opportunity", is_paid: true, is_registered: false,
    opportunity_status: "cancelled", action_state: "ended", is_registration_open: false, ...schedule,
  };
  assert.equal(getOpportunityButtonState(hidden, at("2026-11-11T09:00:00+03:00")), "cancelled");
  // The participant of that course reads its refund state (A9 «الاسترجاع قيد المعالجة»).
  assert.equal(shouldReadCancelledCourseRefund({ ...hidden, is_registered: true, payment_status: "paid" }, false), true);
});
