/**
 * 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.";
  }

  // The class was re-priced in Classwise between loading the page and paying,
  // so the instalment on screen is no longer on the schedule. Refreshing does
  // fix this one -- the page re-reads the plan.
  if (lower.includes("scheduled payments")) {
    return "This class's payment schedule has changed. Please refresh the page and try again.";
  }

  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;
}
