/**
 * Logging a failed request without printing what was sent.
 *
 * `console.error("Login failed:", err)` looks harmless, but an axios rejection
 * carries `config.data` — the serialized request body. On the login, register
 * and reset-password paths that body contains the password in cleartext, so
 * expanding the logged object in devtools (or capturing console output in an
 * error reporter) exposes it. The same applies to `config.headers`, which
 * carries `Authorization: Token …`.
 *
 * This logs only what is useful for diagnosis — where it failed, the HTTP
 * status, and the server's message — and never the request.
 */
interface RequestLikeError {
  message?: unknown;
  response?: { status?: unknown; data?: { msg?: unknown } };
}

/** The non-sensitive summary of a thrown request error. */
export function describeRequestError(error: unknown): {
  status?: number;
  msg?: string;
  message?: string;
} {
  if (!error || typeof error !== "object") {
    return typeof error === "string" ? { message: error } : {};
  }
  const e = error as RequestLikeError;
  const status =
    typeof e.response?.status === "number" ? e.response.status : undefined;
  const msg = typeof e.response?.data?.msg === "string" ? e.response.data.msg : undefined;
  const message = typeof e.message === "string" ? e.message : undefined;
  return { ...(status !== undefined && { status }), ...(msg && { msg }), ...(message && { message }) };
}

/**
 * `console.error` for a failed request, with the request itself stripped.
 *
 * Use this instead of logging the error object on any path that submits
 * credentials or personal data.
 */
export function logRequestError(context: string, error: unknown): void {
  console.error(context, describeRequestError(error));
}
