/**
 * «إرسال الشهادات» for a development opportunity — BE-102.
 *
 * The rules live here rather than in the screen because they are genuinely
 * subtle: which learning types grant a certificate at all, and when the send
 * is allowed.
 */

/**
 * **Live since 2026-10-03.** This was off for one reason only — the route did
 * not exist, and there was no workaround, because the per-registration
 * `POST /certificates/{id}/issue/` refuses anyone but the registration's own
 * owner ("You can only issue your own certificates."), so an organizer looping
 * it would have collected one 403 per participant.
 *
 * `POST /learn-serve-opportunities/{id}/certificates/send/` shipped with the
 * same shape as the volunteering call we already make: `{ certificates_sent }`,
 * idempotent (an already-certified registration is skipped, so pressing twice
 * is safe), `400` before the opportunity is completed, `422` when the learning
 * type grants no certificate — **or when nobody has been checked in yet** —
 * and `404` to a non-owner.
 *
 * That last 422 is new and is why `certificates_sent: 0` is now rare: a send
 * with no attendance recorded is refused outright rather than reported as
 * zero. `handleSendCertificates` already shows the server's message for it.
 */
export const SEND_LEARN_SERVE_CERTIFICATES_LIVE = true;

/**
 * The learning types that grant a certificate, matched on the master-choice
 * `value_en` — `Course` / دورة and `Internship` / تدريب عملي.
 *
 * This is `CertificateController::learningTypeGrantsCertificate()`'s rule, and
 * deliberately *not* `BackfillMissingCertificatesCommand`'s, which also issues
 * for any type carrying «شهادة المنظم». The two disagree in the backend today;
 * BE-102 asks which is right. Until then this follows the one that governs the
 * endpoint a participant actually opens their certificate through, so we never
 * offer to send something they would then be refused.
 */
const CERTIFICATE_LEARNING_TYPES = ["course", "internship"];

interface LocalizedChoice {
  value_en?: string | null;
  value_ar?: string | null;
}

/** True when this opportunity's type grants a certificate at all. */
export function grantsCertificate(
  learningTypeDisplay?: LocalizedChoice | null
): boolean {
  const label = learningTypeDisplay?.value_en?.trim().toLowerCase();
  if (!label) return false;
  return CERTIFICATE_LEARNING_TYPES.includes(label);
}

/**
 * Why the button is not pressable yet, or null when it is.
 *
 * `completed` is the server's own gate — `sendCertificates()` returns 400 for
 * anything else — so this only mirrors it; it does not decide it.
 *
 * `no-attendee` (client, 2026-10-10: «زر إصدار الشهادات لا يتفعل إلا إذا تم
 * تحضير شخص واحد على الأقل») — the server 422s it as well
 * (`LearnServeCertificateService::hasAnyCheckedIn()`). The list is paged and
 * nothing reports an attended count, so it is only claimed when every row is
 * loaded, unfiltered, and none is attended; anything less certain leaves the
 * button to the server.
 */
export function sendCertificatesBlockedReason(
  opportunityStatus?: string | null,
  attendance?: { anyAttended: boolean; certain: boolean }
): "not-ended" | "no-attendee" | null {
  if (String(opportunityStatus ?? "").toLowerCase() !== "completed") {
    return "not-ended";
  }
  if (attendance && attendance.certain && !attendance.anyAttended) {
    return "no-attendee";
  }
  return null;
}

/** Organizer-issued certificates never trigger Fursa generation. */
export function usesFursaCertificate(choice?: LocalizedChoice | null): boolean {
  const english = choice?.value_en?.toLowerCase().trim() ?? "";
  const arabic = choice?.value_ar?.trim() ?? "";
  return english.includes("forsa") || english.includes("fursa") || arabic === "شهادة فرصة";
}
