import { inheritAccountAutoPauseResponse } from "../lib/account-auto-pause-response";
import { readBoundedResponseBody } from "../lib/bounded-body";
import { providerErrorTypeOf, withProviderFailureFacts } from "../lib/failure-detail";
import { redactSecretString } from "../lib/redact";

const ABSOLUTE_PATH_PATTERN = /(?:\/Users\/[^ "';,]+|\/home\/[^ "';,]+|\/root\/[^ "';,]*|[A-Za-z]:\\Users\\[^ "';,]+)/g;

export function sanitizeUpstreamErrorText(value: string): string {
  return redactSecretString(value).replace(ABSOLUTE_PATH_PATTERN, "[REDACTED_PATH]");
}

export function safeUpstreamErrorString(value: unknown): string | undefined {
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
}

export function parseUpstreamJsonPayload(payloadText: string): unknown | undefined {
  const trimmed = payloadText.trim();
  if (!trimmed || (!trimmed.startsWith("{") && !trimmed.startsWith("["))) return undefined;
  try {
    return JSON.parse(trimmed) as unknown;
  } catch {
    return undefined;
  }
}

export async function readDisplaySafeErrorPayloadText(res: Response, signal?: AbortSignal): Promise<string> {
  try {
    const body = await readBoundedResponseBody(res, { signal });
    return body.displaySafe ? body.text : "";
  } catch (error) {
    if (signal?.aborted) throw error;
    return "";
  }
}

const rewrittenUpstreamErrors = new WeakSet<Response>();

/**
 * Whether an adapter replaced this provider failure's body with display text. The provider's own
 * codes, and the fact that a body was unreadable or oversized, are gone from such a response, so
 * combo failover must not read it as the provider's evidence (#9017).
 */
export function isRewrittenUpstreamError(response: Response): boolean {
  return rewrittenUpstreamErrors.has(response);
}

/** Record a replacement response whose body no longer holds the provider's complete failure body. */
export function markRewrittenUpstreamError<T extends Response>(response: T): T {
  rewrittenUpstreamErrors.add(response);
  return response;
}

export async function normalizeUpstreamHttpErrorResponse(
  res: Response,
  opts: { signal?: AbortSignal; formatMessage: (payloadText: string) => string | Promise<string> },
): Promise<Response> {
  if (res.ok) return res;
  const payloadText = await readDisplaySafeErrorPayloadText(res, opts.signal);
  const headers = new Headers(res.headers);
  headers.delete("content-encoding");
  headers.delete("content-length");
  // The rewritten body is display text; keep the provider's closed error class for the request log (#8446).
  const rewritten = withProviderFailureFacts(inheritAccountAutoPauseResponse(res, new Response(await opts.formatMessage(payloadText), {
    status: res.status,
    statusText: res.statusText,
    headers,
  })), { type: providerErrorTypeOf(parseUpstreamJsonPayload(payloadText)) });
  rewrittenUpstreamErrors.add(rewritten);
  return rewritten;
}
