/**
 * Pull a human-readable message out of a NestJS error response.
 *
 * The backend has no custom exception filter, so it emits the stock Nest shape
 * — and `message` is a STRING for service-thrown exceptions but a STRING[] for
 * class-validator pipe failures. Both must be handled or validation errors
 * render as "[object Object]".
 */
export function getApiErrorMessage(error: unknown, fallback: string): string {
  const message = (
    error as { response?: { data?: { message?: unknown } } } | undefined
  )?.response?.data?.message;

  if (typeof message === "string" && message.trim()) return message;

  if (Array.isArray(message)) {
    const joined = message.filter((m) => typeof m === "string").join(" • ");
    if (joined) return joined;
  }

  return fallback;
}
