/**
 * BE-125 — the organizer's participants-and-payments row.
 *
 * The rules worth pinning are the ones where a wrong default misreports money
 * or offers an action the server will refuse.
 */
import assert from "node:assert/strict";
import { describe, test } from "node:test";

import moment from "moment";
import {
  canOpenRefundAll,
  withRowId,
  type ParticipantPaymentRecord,
} from "@/features/opportunities/participantPaymentRecord";
import {
  paymentStatusClass,
  paymentStatusKey,
} from "@/features/opportunities/paymentStatusLabel";
import { formatAmount } from "@/features/opportunities/payment";

const record = (
  overrides: Partial<ParticipantPaymentRecord> = {}
): ParticipantPaymentRecord => ({
  registration_id: 42,
  user_id: 7,
  user_name: "Noor Al-Sabah",
  user_email: "noor@example.com",
  status: "pending",
  payment_status: "paid",
  gross_amount: "25.00",
  platform_fee_amount: "1.75",
  gateway_fee_amount: null,
  net_amount: null,
  paid_at: "2026-10-04T09:10:00+03:00",
  refunded_at: null,
  ...overrides,
});

describe("withRowId", () => {
  /* The paging accumulator keys rows by `id`; this resource names its key
     `registration_id`, and merging without the alias would drop every row. */
  test("the registration id becomes the row id, and nothing else changes", () => {
    const row = withRowId(record());
    assert.equal(row.id, 42);
    assert.equal(row.registration_id, 42);
    assert.equal(row.user_name, "Noor Al-Sabah");
  });
});

describe("paymentStatusKey", () => {
  test("each status has its own label", () => {
    assert.equal(paymentStatusKey("paid"), "COMMON.PAYMENT_STATUS_PAID");
    assert.equal(paymentStatusKey("pending"), "COMMON.PAYMENT_STATUS_PENDING");
    assert.equal(paymentStatusKey("refunded"), "COMMON.PAYMENT_STATUS_REFUNDED");
    assert.equal(paymentStatusKey("failed"), "COMMON.PAYMENT_STATUS_FAILED");
    assert.equal(paymentStatusKey("canceled"), "COMMON.PAYMENT_STATUS_CANCELED");
  });

  /*
   * The case that matters: a registration with no transaction behind it — a
   * free opportunity's row, or a seat added outside the paid flow — arrives
   * with `payment_status: null`. Reading that as "pending" would tell the
   * organizer a free participant owes money.
   */
  test("no payment is its own state, not pending", () => {
    assert.equal(paymentStatusKey(null), "COMMON.PAYMENT_STATUS_NONE");
    assert.equal(paymentStatusKey(undefined), "COMMON.PAYMENT_STATUS_NONE");
    assert.notEqual(paymentStatusKey(null), paymentStatusKey("pending"));
  });

  test("every status has a style, including the absent one", () => {
    for (const status of ["paid", "pending", "refunded", "failed", "canceled"] as const) {
      assert.ok(paymentStatusClass(status).length > 0);
    }
    assert.ok(paymentStatusClass(null).length > 0);
  });
});

describe("the money columns", () => {
  /*
   * `gateway_fee_amount` and `net_amount` are null until the gateway reports
   * them — which, with the stub driver, is never. A dash says "not known";
   * `0.00` would say "no fee was charged".
   */
  test("an unreported amount has no figure to show", () => {
    const row = record();
    assert.equal(formatAmount(row.gateway_fee_amount, "KWD"), null);
    assert.equal(formatAmount(row.net_amount, "KWD"), null);
    assert.equal(formatAmount(row.gross_amount, "KWD"), "25.000 KWD");
  });
});

describe("canOpenRefundAll", () => {
  /*
   * The server sends `can_refund: false` on every row (a publisher never
   * refunds one participant), so the refund-all button cannot hang off it.
   */
  const schedule = { start_date: "2026-11-10", start_time: "10:00", end_date: "2026-11-12", end_time: "12:00" };
  const before = moment.parseZone("2026-11-05T12:00:00+03:00");
  test("opens once someone has paid, before the start", () => {
    assert.equal(canOpenRefundAll([record({ payment_status: "pending" }), record()], schedule, before), true);
  });
  test("stays closed with no paid row", () => {
    assert.equal(canOpenRefundAll([record({ payment_status: "refunded" }), record({ payment_status: "pending" })], schedule, before), false);
    assert.equal(canOpenRefundAll([], schedule, before), false);
  });
  test("stays open while the course runs (A9, backend 7fc047d) and closes at the end", () => {
    assert.equal(canOpenRefundAll([record()], schedule, moment.parseZone("2026-11-10T10:00:00+03:00")), true);
    assert.equal(canOpenRefundAll([record()], schedule, moment.parseZone("2026-11-12T11:59:00+03:00")), true);
    assert.equal(canOpenRefundAll([record()], schedule, moment.parseZone("2026-11-12T12:00:00+03:00")), false);
  });
  test("stays closed after a cancellation", () => {
    assert.equal(canOpenRefundAll([record()], { ...schedule, opportunity_status: "cancelled" }, before), false);
  });
  test("an unknown schedule leaves the decision to the quote endpoint", () => {
    assert.equal(canOpenRefundAll([record()], undefined, before), true);
  });
});
