import { pickLocalized } from "@/features/shared/types/cms";

/**
 * Client answer A15 (p.4): certificates go out automatically at the end; after
 * that «إرسال الشهادات» is enabled only when an edit (hours, attendance or a
 * participant's name) left a certificate out of date, with the server's note
 * beside it. Backend `bf3140b` sends these next to `can_send_certificates`.
 */
export interface CertificatesOutdatedSource {
  certificates_outdated?: boolean | null;
  certificates_outdated_count?: number | null;
  certificates_outdated_note_en?: string | null;
  certificates_outdated_note_ar?: string | null;
  /** The detail's resolved lifecycle status (`completed` from the actual end). */
  opportunity_status?: string | null;
}

/**
 * The status the list gates «إرسال الشهادات» on: the live detail's resolved
 * one when it has loaded, else the stashed snapshot. The snapshot is taken
 * when the organizer opens the list (often during the final session) and
 * survives reloads in sessionStorage, so on its own it kept the button on
 * «not ended» after the end while the server already said `certificates_outdated`.
 */
export function liveOpportunityStatus(
  source: CertificatesOutdatedSource | null | undefined,
  stashed: string | null | undefined
): string | null {
  return source?.opportunity_status || stashed || null;
}

/**
 * Whether anything is left to send. Only an explicit `false` disables: a
 * payload without the field (older API) keeps the previous window rule rather
 * than locking the organizer out.
 */
export function certificatesNeedSending(source: CertificatesOutdatedSource | null | undefined): boolean {
  return source?.certificates_outdated !== false;
}

/** The server's note, in the reader's language, only while something is outdated. */
export function certificatesOutdatedNote(
  source: CertificatesOutdatedSource | null | undefined,
  language: string
): string | null {
  if (source?.certificates_outdated !== true) return null;
  const note = pickLocalized(source.certificates_outdated_note_en, source.certificates_outdated_note_ar, language);
  return note || null;
}

export type CertificatesSendBlock = "up-to-date" | "loading" | "load-failed" | null;

/**
 * A15 gate from the live detail read. Send is never enabled on data that did
 * not load: while loading it waits, and a failed read (first load or a refetch
 * after an edit) blocks it with its own reason so the screen can say why and
 * offer a retry — a stale `certificates_outdated: true` after a send must not
 * re-enable a second send.
 */
export function certificatesSendState(read: {
  hasData: boolean;
  isError: boolean;
  source: CertificatesOutdatedSource | null | undefined;
}): { needsSending: boolean; blockedReason: CertificatesSendBlock } {
  if (read.isError) return { needsSending: false, blockedReason: "load-failed" };
  if (!read.hasData) return { needsSending: false, blockedReason: "loading" };
  const needsSending = certificatesNeedSending(read.source);
  return { needsSending, blockedReason: needsSending ? null : "up-to-date" };
}
