/**
 * Frontend follow-ups to the backend delivery `client-update-oct-2026@1983649`.
 */
import { test } from "node:test";
import assert from "node:assert/strict";
import moment from "moment";

import { isPositiveKwdPrice } from "@/features/shared/opportunityFormRules";
import { isUnpaidCheckoutAttempt } from "@/features/shared/opportunityButtonState";
import { cardStatusBadges } from "@/features/opportunities/cardStatusBadges";
import {
  DEFAULT_TRAINER_SETTINGS,
  canRequestTrainerTypes,
  requestableTrainerTypes,
} from "@/features/trainer/trainerRequest";
import { fromOwnOrganizationProfile } from "@/features/profile/components/individual/individualProfile";

test("price follows backend PlainPrice: ≤ 7 digits, ≤ 3 decimals, > 0", () => {
  for (const ok of ["1", "20.5", "0.005", "9999999.999", " 12.250 "]) assert.equal(isPositiveKwdPrice(ok), true, ok);
  for (const bad of [".5", "10000000", "1.2345", "1e3", "0", "0.000", "-1", "1.", "", "abc"]) {
    assert.equal(isPositiveKwdPrice(bad), false, bad);
  }
});

const course = {
  opportunity_type: "learn_serve_opportunity",
  is_paid: true,
  is_registered: true,
  relationship_tags: ["registered"] as const,
  start_date: "2026-12-10",
  start_time: "09:00",
  end_date: "2026-12-10",
  end_time: "12:00",
};
const before = moment("2026-12-01T10:00:00+03:00");
const labels = (item: Record<string, unknown>) =>
  cardStatusBadges(item as never, "viewer", true, true, before).map((badge) => badge.label);

test("BE-231: a card's unpaid, refunded or cancelled booking shows no registered badge", () => {
  assert.ok(labels({ ...course, payment_status: "paid" }).includes("COMMON.CARD_REGISTERED_PAID"));
  for (const payment_status of ["pending", "failed", "expired", "refunded"]) {
    assert.ok(!labels({ ...course, payment_status }).includes("COMMON.CARD_REGISTERED_PAID"), payment_status);
  }
  assert.ok(!labels({ ...course, payment_status: "paid", cancellation_status: "approved" }).includes("COMMON.CARD_REGISTERED_PAID"));
});

test("an unpaid attempt is only a paid development booking", () => {
  assert.equal(isUnpaidCheckoutAttempt({ opportunity_type: "learn_serve_opportunity", is_paid: true, payment_status: "failed" }), true);
  assert.equal(isUnpaidCheckoutAttempt({ opportunity_type: "learn_serve_opportunity", is_paid: false, payment_status: "failed" }), false);
  assert.equal(isUnpaidCheckoutAttempt({ opportunity_type: "volunteer_opportunity", is_paid: true, payment_status: "pending" }), false);
});

test("BE-186: an approved trainer may ask for the types not yet approved", () => {
  const approved = { trainer_status: "approved" as const, trainer_types: ["course" as const] };
  const remaining = requestableTrainerTypes(DEFAULT_TRAINER_SETTINGS, approved).map((type) => type.slug);
  assert.ok(remaining.length > 0);
  assert.ok(!remaining.includes("course"));
  assert.equal(canRequestTrainerTypes(DEFAULT_TRAINER_SETTINGS, approved), true);
  // Waiting for review: nothing to send until the admin decides.
  assert.equal(canRequestTrainerTypes(DEFAULT_TRAINER_SETTINGS, { ...approved, trainer_pending_types: [remaining[0]] }), false);
  // Every offered type approved: nothing left to ask for.
  const all = requestableTrainerTypes(DEFAULT_TRAINER_SETTINGS, null).map((type) => type.slug);
  assert.equal(canRequestTrainerTypes(DEFAULT_TRAINER_SETTINGS, { trainer_status: "approved", trainer_types: all }), false);
  assert.equal(canRequestTrainerTypes(DEFAULT_TRAINER_SETTINGS, { trainer_status: "pending" }), false);
  assert.equal(canRequestTrainerTypes(DEFAULT_TRAINER_SETTINGS, { trainer_status: "rejected" }), true);
});

test("BE-238: the organization's own profile shows its sponsorship type and level", () => {
  const sponsorship = { type: "Gold sponsor", level: "Strategic" };
  const data = fromOwnOrganizationProfile({ sponsorship_profile: sponsorship } as never, { id: 7 } as never);
  assert.deepEqual(data.sponsorshipProfile, sponsorship);
  assert.equal(fromOwnOrganizationProfile({} as never, { id: 7 } as never).sponsorshipProfile, null);
});

test("PDF p.37: a role's server field errors land on the form field, not in a toast", async () => {
  const { splitFormApiErrors } = await import("@/features/shared/splitFormApiErrors");
  const { ROLE_FIELD_ALIASES, ROLE_FORM_FIELDS } = await import("@/features/opportunities/components/roleRequestBody");
  const set: Record<string, string> = {};
  const error = { response: { data: { key: "fail", response_status: { error: true, validation_errors: {
    participants_needed: ["Not enough seats left"], role_name_ar: ["مطلوب"], opportunity_id: ["Invalid"],
  } } } } };
  const { mapped, rest } = splitFormApiErrors(error, "en", ROLE_FORM_FIELDS, (field, message) => { set[field] = message; }, ROLE_FIELD_ALIASES);
  assert.equal(mapped, 2);
  assert.equal(set.participants_needed, "Not enough seats left");
  assert.ok(set.role_name);
  assert.deepEqual(rest, ["Invalid"]);
});
