export function getFriendlyActionError(
  error: unknown,
  fallback: string,
  options?: {
    invalidInputMessage?: string;
    unauthenticatedMessage?: string;
    forbiddenMessage?: string;
  }
) {
  const message =
    typeof error === "object" && error && "message" in error
      ? String((error as { message?: unknown }).message || "")
      : "";

  if (!message) return fallback;

  const normalized = message.toLowerCase();

  if (
    normalized.includes("unauthorized") ||
    normalized.includes("non authentifié") ||
    normalized.includes("authentication") ||
    normalized.includes("session")
  ) {
    return options?.unauthenticatedMessage || "Votre session semble avoir expiré. Reconnectez-vous puis recommencez.";
  }

  if (
    normalized.includes("forbidden") ||
    normalized.includes("permission") ||
    normalized.includes("not allowed")
  ) {
    return options?.forbiddenMessage || "Vous n'avez pas les droits nécessaires pour effectuer cette action.";
  }

  if (
    normalized.includes("invalid") ||
    normalized.includes("required") ||
    normalized.includes("obligatoire") ||
    normalized.includes("validation")
  ) {
    return options?.invalidInputMessage || fallback;
  }

  if (
    normalized.includes("internal server error") ||
    normalized.includes("mutation failed") ||
    normalized.includes("failed")
  ) {
    return fallback;
  }

  return message;
}
