/**
 * Backend delivery `bf3140b`: the new status values and counts the website
 * reads (complements `backendDeliveryBf3140b.test.ts`).
 */
import { test } from "node:test";
import assert from "node:assert/strict";

import { paymentReturnState } from "@/features/opportunities/paymentReturn";
import { refundStateFromDetail } from "@/features/opportunities/cancelledCourseRefund";
import { isCancellationRequestOpen, getOpportunityButtonState } from "@/features/shared/opportunityButtonState";
import { missedCheckOutAlert } from "@/features/opportunities/components/volunteerListHelpers";
import type { LearnServePaymentStatusRecord } from "@/features/opportunities/services/learnServe";

const record = (over: Partial<LearnServePaymentStatusRecord>): LearnServePaymentStatusRecord => ({
  registration_id: 1, opportunity_id: 9, payment_status: "pending", gross_amount: "20.000", currency: "KWD", paid_at: null, refund: null, ...over,
});

test("Upay return: paid, pending and unpaid come from the verify answer", () => {
  assert.equal(paymentReturnState(record({ payment_status: "paid" })), "paid");
  assert.equal(paymentReturnState(record({ payment_status: "pending" })), "pending");
  assert.equal(paymentReturnState(record({ payment_status: "failed" })), "unpaid");
  assert.equal(paymentReturnState(null), null);
});

test("A9: the detail's refund_status drives «الاسترجاع قيد المعالجة»; a failed refund is still in progress", () => {
  assert.equal(refundStateFromDetail("processing"), "processing");
  assert.equal(refundStateFromDetail("failed"), "processing");
  assert.equal(refundStateFromDetail("refunded"), "refunded");
  assert.equal(refundStateFromDetail(null), null);
  assert.equal(refundStateFromDetail(undefined), undefined);
});

test("PDF p.18: every open cancellation request reads «طلب الإلغاء قيد المراجعة»", () => {
  for (const status of ["awaiting_trainer", "pending", "refunding", "refund_failed"]) {
    assert.equal(isCancellationRequestOpen(status), true, status);
    assert.equal(getOpportunityButtonState({ opportunity_type: "learn_serve_opportunity", is_paid: true, is_registered: true, payment_status: "paid", cancellation_status: status }), "cancellation_pending");
  }
  assert.equal(isCancellationRequestOpen("available"), false);
  assert.equal(isCancellationRequestOpen("rejected"), false);
});

test("X1: the server's count and sentence are used when only part of the list is loaded", () => {
  const meta = { missing_check_out_count: 3, missing_check_out_note_ar: "3 مشاركين سجلوا دخول بدون خروج", missing_check_out_note_en: "3 participants" };
  assert.deepEqual(missedCheckOutAlert([], meta, false, "ar"), { count: 3, note: "3 مشاركين سجلوا دخول بدون خروج" });
  // With every row loaded, the local count wins: it follows an hours fix at once.
  assert.deepEqual(missedCheckOutAlert([], meta, true, "ar"), { count: 0, note: null });
  assert.deepEqual(missedCheckOutAlert([], {}, false, "en"), { count: 0, note: null });
});
