/**
 * Map backend payment errors to user-facing copy (UI text unchanged for known cases).
 */
export function formatPaymentErrorMessage(message?: string | null): string {
  if (!message) {
    return "Payment initiation failed.";
  }

  const lower = message.toLowerCase();

  if (lower.includes("no active stripe payment method")) {
    return "The franchisee has not configured their payment setup yet. Please contact the franchisee for assistance or try again later.";
  }

  if (lower.includes("already been paid")) {
    return "This registration has already been paid.";
  }

  if (lower.includes("payment amount does not match")) {
    return "The payment total has changed. Please refresh the page and try again.";
  }

  if (lower.includes("not ready to accept payments") || lower.includes("complete onboarding")) {
    return message;
  }

  if (lower.includes("something went wrong")) {
    return "Payment could not be started. Please refresh the page and try again, or contact support if this continues.";
  }

  return message;
}
