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

import { awaitsApproval } from "@/features/opportunities/components/volunteerListHelpers";
import { deriveVolunteerEventView } from "@/features/opportunities/components/volunteer-event/volunteerEventView";
import type { VolunteerOpportunityDetail } from "@/features/opportunities/components/volunteer-event/types";

import {
  directRegisterFailures,
  formatDirectRegisterFailures,
  type VolunteerPickerRow,
} from "@/features/opportunities/volunteerPicker";

const row = (id: number, user_id: number | null, full_name: string | null): VolunteerPickerRow => ({
  id, user_id, nickname: null, full_name, profile_pic: null, gender_display: null, is_verified: false,
  civil_id_last4: null, passport_last4: null, phone_last4: null,
  user: user_id ? { id: user_id, full_name, profile_pic: null, nickname: null } : null,
});

test("manual add: a 201 with refused ids lists each refused person by name", () => {
  const body = {
    key: "success",
    data: {
      successful_registrations: [{ user_id: 7 }],
      failed_registrations: [
        { user_id: 8, error: "Your age is outside this opportunity's age range." },
        { user_id: 9, error: "User is already registered." },
      ],
      success_count: 1,
      failed_count: 2,
    },
  };
  const lines = directRegisterFailures(body, [row(1, 7, "A"), row(2, 8, "Sara Ali"), row(3, 9, "Omar")]);
  assert.deepEqual(lines.map((l) => [l.userId, l.name]), [[8, "Sara Ali"], [9, "Omar"]]);
  assert.equal(
    formatDirectRegisterFailures(lines),
    "Sara Ali: Your age is outside this opportunity's age range.\nOmar: User is already registered.",
  );
});

test("manual add: the 400 body (nobody added) is read the same way; unknown rows fall back to the id", () => {
  const body = { key: "fail", data: { failed_registrations: [{ user_id: "12", error: "" }] } };
  const lines = directRegisterFailures(body, []);
  assert.deepEqual(lines, [{ userId: 12, name: "#12", reason: "" }]);
  assert.equal(formatDirectRegisterFailures(lines), "#12");
});

test("manual add: a body without the list, or with junk entries, yields nothing", () => {
  assert.deepEqual(directRegisterFailures(null, []), []);
  assert.deepEqual(directRegisterFailures({ data: { failed_registrations: "x" } }, []), []);
  assert.deepEqual(directRegisterFailures({ data: { failed_registrations: [null, { user_id: "abc" }] } }, []), []);
});

test("BE-258: pending and rejected opportunities take no manual add, check-in or QR; an absent status is not a refusal", () => {
  assert.equal(awaitsApproval("pending"), true);
  assert.equal(awaitsApproval("rejected"), true);
  assert.equal(awaitsApproval("approved"), false);
  assert.equal(awaitsApproval(null), false);
  assert.equal(awaitsApproval(undefined), false);

  const now = moment.parseZone("2026-11-01T12:00:00+03:00");
  const organizer = { id: 8, signedIn: true, hasAuthToken: true, isVerified: true, userType: "organization" };
  const detail = (approval_status?: string) => ({
    opportunity_type: "volunteer_opportunity", start_date: "2026-11-05", start_time: "10:00", end_date: "2026-11-05", end_time: "14:00",
    is_paid: false, id: 2, title_ar: "فرصة", title_en: "Opportunity", created_by: { id: 8 }, opportunity_images: [], total_roles: 0,
    approval_status,
  }) as VolunteerOpportunityDetail;
  assert.equal(deriveVolunteerEventView(detail("pending"), organizer, now).canGenerateAttendanceQr, false);
  assert.equal(deriveVolunteerEventView(detail("rejected"), organizer, now).canGenerateAttendanceQr, false);
  assert.equal(deriveVolunteerEventView(detail("approved"), organizer, now).canGenerateAttendanceQr, true);
  assert.equal(deriveVolunteerEventView(detail(), organizer, now).canGenerateAttendanceQr, true);
});
