import { recordUpstreamProviderErrorDetail, recordUpstreamValidationDetail, type FailureDetail } from "../request-log-failure-detail";
import { carryProxyErrorPresentation } from "../../lib/client-error-body";
import { carryPolicyCandidateRefusal } from "./policy-request-scope";
import { comboFailureEvidence, carryProxyError } from "../../lib/client-error-body";
import { carryWorkflowRefusal } from "../workflow-refusal";
import {
  codexAccountModelRefusal,
  codexAccountModelRefusalHardStopCode,
  isMalformedResponseFormatRequest,
  isOpenRouterOpaque400Envelope,
  isUnofferedToolCallRefusal,
  isProviderModerationRefusal,
  isProviderIntakeRefusalBody,
  upstreamEnvelopeRootCode,
  type CodexAccountModelRefusal,
} from "../../combos/failover";
import { parseRetryAfterMs } from "../../combos";
import type { ConsumedComboFailure, HandleResponsesOptions } from "./core-options";
import type { OcxUsage } from "../../types";
import { readBoundedResponseBody } from "../../lib/bounded-body";
import { carryAnthropicRefusalCredential } from "../../oauth/anthropic-account-refusal";
import { carryCodexAccountCooldownResponse } from "../../codex/account-cooldown-response";
import { codexQuotaFailureMessage, codexQuotaOutcomeMeta } from "./core-codex-account";
import {
  isRateLimitOrQuotaFailureMessage,
  isCyberPolicyCode,
  isCyberPolicyMessage,
  CYBER_POLICY_ERROR_CODE,
  CYBER_POLICY_FALLBACK_MESSAGE,
  isModelNotInPlanRefusal,
  markVerifiedModelNotInPlanResponse,
  namesModelNotInPlanCode,
} from "../../lib/errors";
import { normalizeUpstreamErrorText } from "./core-errors";
import { resolveClientRetryAfter } from "../../lib/retry-after";
import { formatErrorResponse } from "../../bridge";
import { upstreamCodeForComboFailure } from "../../bridge/errors";
import { OLLAMA_LATE_ATTRIBUTION_LIMIT_MESSAGE } from "../../adapters/ollama-native";
import { carryReplayRefusal, isNonReplayableResponse, isReplayRefusalResponse, markResponseNonReplayable, UPSTREAM_RESET_REPLAY_REFUSED_CODE } from "../../lib/upstream-retry";
import { MODEL_NOT_ALLOWED_FOR_KEY } from "../admission-model-scope";
import { usageFromResponsesPayload } from "../request-log";
import type { ResponsesTerminalStatus } from "../../bridge";
import { isXaiLegacyPrivacyError, isXaiPrivacyModeRefusal, xaiPrivacyModeRefusal, XAI_LEGACY_PRIVACY_MESSAGE } from "../../providers/xai-privacy-mode";
import { stampMetaMusePlanRefusal } from "../../providers/meta-muse-plan-refusal";
import type { RequestFailureCause } from "../../usage/telemetry-contract";
import { isRewrittenUpstreamError } from "../../adapters/upstream-http-error";

export function sanitizedRetryAfter(value: string | null, now: number): string | undefined {
  const trimmed = value?.trim();
  if (!trimmed || trimmed.length > 128) return undefined;
  return parseRetryAfterMs(trimmed, now) !== undefined ? trimmed : undefined;
}




/** A failure envelope this proxy formatted from a provider body it already consumed. */
function isProxyFormattedFailure(text: string): boolean {
  try {
    const parsed: unknown = JSON.parse(text);
    const error = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? (parsed as { error?: unknown }).error : undefined;
    const message = error && typeof error === "object" && !Array.isArray(error) ? (error as { message?: unknown }).message : undefined;
    return typeof message === "string" && /^Provider error \d{3}\b/.test(message);
  } catch {
    return false;
  }
}
export async function consumeComboFailure(
  response: Response,
  signal?: AbortSignal,
  now = Date.now(),
  logCtx?: { failureDetail?: FailureDetail; causeHint?: RequestFailureCause },
  /** True only when `response` is the provider's own HTTP answer, so its closed error class is evidence. */
  rawUpstream = false,
  grokOAuth = false,
): Promise<ConsumedComboFailure> {
  // Read before the body: the marker lives on this Response object, and the failure below is
  // rebuilt as a new one that would otherwise lose it.
  const nonReplayable = isNonReplayableResponse(response);
  const replayRefusal = isReplayRefusalResponse(response);
  const fallback = `Provider error ${response.status}`;
  let classificationText = fallback;
  let codexModelRefusal: CodexAccountModelRefusal | undefined;
  let malformedResponseFormat: boolean | undefined;
  let openRouterOpaque400: boolean | undefined;
  let unofferedToolCallRefusal: boolean | undefined;
  let providerModerationRefusal: boolean | undefined;
  // A 413 may hop only once its complete body was read and judged; an oversized, unreadable or
  // aborted body leaves the evidence unknown, and unknown evidence keeps dev's terminal 413.
  let providerIntakeRefusal: boolean | undefined = response.status === 413 ? false : undefined;
  let usage: OcxUsage | undefined;
  let upstreamCode: string | undefined;
  let upstreamRootCode: string | undefined;
  let upstreamMessage: string | undefined;
  let upstreamType: string | undefined;
  // Whether the body itself confirms a quota/rate-limit refusal, computed on the SAME read as
  // the classification below. `shouldRetryCodexPoolAccountQuota` cannot be called here without
  // a second body read, so this mirrors its normalization: raw 402/429, or a 5xx whose intact,
  // display-safe body carries a recognized quota message.
  let quotaConfirmedByBody = false;
  const serverError = response.status >= 500 && response.status < 600;
  // A local key-scope refusal is already the client's answer: rebuilding it as an upstream_error
  // would hide the stable model_not_allowed_for_key type clients branch on.
  let scopeRefusalBody: string | undefined;
  let verifiedPlanRefusal = false;
  try {
    const legacy = comboFailureEvidence(response);
    if (legacy !== undefined) void response.body?.cancel().catch(() => undefined);
    const body = await readBoundedResponseBody(legacy === undefined ? response : new Response(legacy), { signal, reportUtf8Validity: serverError });
    // A 5xx body counts as quota or classification evidence only when it decoded as valid
    // UTF-8, matching shouldRetryCodexPoolAccountQuota. A malformed byte keeps the status-only
    // fallback, with one exception: a cyber-policy refusal must still stop the combo, so the
    // replacement-decoded text may carry that verdict and nothing else.
    const utf8Trusted = !serverError || body.utf8Valid === true;
    if (utf8Trusted) usage = usageFromComboFailureText(body.text);
    if (serverError && utf8Trusted && body.displaySafe && !body.truncated) {
      const quotaMessage = codexQuotaFailureMessage(body.text);
      quotaConfirmedByBody = quotaMessage !== undefined
        && isRateLimitOrQuotaFailureMessage(quotaMessage);
    }
    if (body.displaySafe && !body.truncated) {
      if (isXaiPrivacyModeRefusal(response) || (grokOAuth && isXaiLegacyPrivacyError(response.status, body.text))) {
        const refusal = xaiPrivacyModeRefusal(logCtx);
        if (response.headers.get("x-should-retry") === "false") refusal.headers.set("x-should-retry", "false");
        if (nonReplayable) markResponseNonReplayable(refusal);
        return {
          response: refusal,
          ...(nonReplayable ? { nonReplayable: true } : {}),
          classificationText: XAI_LEGACY_PRIVACY_MESSAGE,
          upstreamMessage: XAI_LEGACY_PRIVACY_MESSAGE,
          upstreamType: "permission_error",
          upstreamCode: "permission_denied",
        };
      }
      if (logCtx && rawUpstream) recordUpstreamProviderErrorDetail(logCtx, response.status, body.text, response);
      else if (logCtx) recordUpstreamValidationDetail(logCtx, response.status, body.text);
      // A combo child's row is attributed from the same evidence the parent uses: Meta's Muse
      // Code refusal is a plan restriction, not a rejected credential (#8352).
      if (logCtx) stampMetaMusePlanRefusal(logCtx, response.status, body.text);
      if (response.status === 400) {
        codexModelRefusal = codexAccountModelRefusal(response.status, body.text);
        malformedResponseFormat = isMalformedResponseFormatRequest(response.status, body.text);
        openRouterOpaque400 = isOpenRouterOpaque400Envelope(body.text);
        // The intact bounded body, not the 500-char display prefix: a long refusal envelope
        // would fail the classifier's JSON parse on truncated text and stop the combo.
        unofferedToolCallRefusal = isUnofferedToolCallRefusal(response.status, body.text);
      }
      // Judged on the whole body: the classification text below is cut at 500 characters, and a
      // hard refusal after the cut must still veto a moderation hop.
      if (response.status === 400 || response.status === 422) {
        providerModerationRefusal = isProviderModerationRefusal(response.status, body.text, undefined, true);
      }
      // The same whole-body rule for the provider-413 hop (#9017): a hard refusal or proxy marker
      // past the 500-character cut must still keep the combo from sending the turn elsewhere.
      // A native lane's rewritten envelope (Messages or Chat native) is not the provider's body: its
      // terminal codes and an oversized original are gone, so only the original read may clear it.
      // So is a sidecar loop's (web search, image bridge) envelope, whose message is the proxy's own
      // "Provider error 413" summary of a body it already read.
      if (response.status === 413) {
        providerIntakeRefusal = legacy === undefined && !isRewrittenUpstreamError(response) && !isProxyFormattedFailure(body.text)
          && isProviderIntakeRefusalBody(body.text);
      }
      // Status-independent: the nested record is what normalization selects, so a root verdict
      // beside it has to be captured here or the conflict rule never sees it.
      upstreamRootCode = upstreamEnvelopeRootCode(body.text);
      const normalized = normalizeUpstreamErrorText(body.text, fallback);
      if (utf8Trusted || isCyberPolicyCode(normalized.code) || isCyberPolicyMessage(normalized.safeText)) {
        classificationText = normalized.safeText;
        upstreamCode = codexAccountModelRefusalHardStopCode(response.status, body.text)
          ?? upstreamCodeForComboFailure(response, normalized.code);
        // Only the adapter's own normalized refusal may claim the plan-refusal hop; another
        // upstream echoing the code in any spelling keeps the meaning it had before, which was none.
        if (namesModelNotInPlanCode(upstreamCode) && !isModelNotInPlanRefusal(response, upstreamCode)) upstreamCode = undefined;
        verifiedPlanRefusal = isModelNotInPlanRefusal(response, upstreamCode);
        upstreamMessage = normalized.message;
        upstreamType = normalized.type;
      }
      if (nonReplayable && response.status === 403 && upstreamType === MODEL_NOT_ALLOWED_FOR_KEY) {
        scopeRefusalBody = body.text;
      }
    }
  } catch (error) {
    if (signal?.aborted) throw error;
    classificationText = fallback;
  }
  const cyberFailure = isCyberPolicyCode(upstreamCode) || isCyberPolicyMessage(classificationText);
  const normalizedUpstreamCode = cyberFailure ? CYBER_POLICY_ERROR_CODE
    : replayRefusal ? UPSTREAM_RESET_REPLAY_REFUSED_CODE : upstreamCode;
  const message = cyberFailure
    ? upstreamMessage
      ?? (isCyberPolicyCode(upstreamCode) ? CYBER_POLICY_FALLBACK_MESSAGE : classificationText)
    : classificationText === fallback
      ? fallback
      : `${fallback}: ${classificationText}`;
  const upstreamRetryAfter = response.headers.get("retry-after");
  // Past HTTP dates are an immediate retry directive, just like the numeric value zero.
  // Normalize before the client helper discards them and substitutes a default delay.
  const effectiveRetryAfter = parseRetryAfterMs(upstreamRetryAfter, now) === undefined
    && parseRetryAfterMs(upstreamRetryAfter, now, { preserveImmediate: true }) !== undefined
    ? "0"
    : upstreamRetryAfter;
  // Client response may get the synthetic "2" fallback; cooldown metadata must not —
  // otherwise coolComboTarget treats it as a 2s cooldown instead of the 60s default.
  const clientRetryAfter = replayRefusal ? undefined : resolveClientRetryAfter({
    status: response.status,
    message,
    upstreamRetryAfter: effectiveRetryAfter,
    now,
  });
  const cooldownRetryAfter = replayRefusal ? undefined : resolveClientRetryAfter({
    status: response.status,
    message,
    upstreamRetryAfter: effectiveRetryAfter,
    now,
    includeDefault: false,
  });
  const failureResponse = scopeRefusalBody !== undefined
    ? new Response(scopeRefusalBody, { status: 403, headers: { "Content-Type": "application/json" } })
    : formatErrorResponse(
    response.status,
    cyberFailure ? (upstreamType ?? CYBER_POLICY_ERROR_CODE) : "upstream_error",
    message,
    {
      ...(normalizedUpstreamCode !== undefined ? { code: normalizedUpstreamCode } : {}),
      ...(clientRetryAfter !== undefined ? { retryAfter: clientRetryAfter } : {}),
    },
  );
  if (nonReplayable) markResponseNonReplayable(failureResponse);
  carryProxyError(response, failureResponse);
  carryWorkflowRefusal(response, failureResponse);
  carryReplayRefusal(response, failureResponse);
  carryProxyErrorPresentation(response, failureResponse);
  carryPolicyCandidateRefusal(response, failureResponse);
  if (verifiedPlanRefusal) markVerifiedModelNotInPlanResponse(failureResponse);
  carryAnthropicRefusalCredential(response, failureResponse);
  carryCodexAccountCooldownResponse(response, failureResponse);
  return {
    response: failureResponse,
    ...(nonReplayable ? { nonReplayable: true } : {}),
    ...(verifiedPlanRefusal ? { verifiedPlanRefusal: true } : {}),
    classificationText,
    ...(codexModelRefusal !== undefined ? { codexModelRefusal } : {}),
    ...(malformedResponseFormat !== undefined ? { malformedResponseFormat } : {}),
    ...(openRouterOpaque400 !== undefined ? { openRouterOpaque400 } : {}),
    ...(unofferedToolCallRefusal !== undefined ? { unofferedToolCallRefusal } : {}),
    ...(providerModerationRefusal !== undefined ? { providerModerationRefusal } : {}),
    ...(providerIntakeRefusal !== undefined ? { providerIntakeRefusal } : {}),
    ...(normalizedUpstreamCode !== undefined ? { upstreamCode: normalizedUpstreamCode } : {}),
    // A rewritten identity (policy, replay refusal) is the whole verdict; the root's own code
    // would only re-open a decision that rewrite already settled.
    ...(!cyberFailure && !replayRefusal && upstreamRootCode !== undefined ? { upstreamRootCode } : {}),
    ...(upstreamMessage !== undefined ? { upstreamMessage } : {}),
    ...(upstreamType !== undefined ? { upstreamType } : {}),
    ...(!cyberFailure && cooldownRetryAfter !== undefined ? { retryAfter: cooldownRetryAfter } : {}),
    // The EFFECTIVE classification decides, not the raw status. An upstream that wraps a quota
    // refusal in a 5xx still carries `x-codex-*-reset-at`, and gating on 402/429 alone threw
    // those away, so the combo target came back up immediately instead of waiting for the
    // window it was told about. `cyberFailure` stays excluded: a policy block is not a quota.
    ...(!cyberFailure && !replayRefusal
      && (response.status === 429 || response.status === 402 || quotaConfirmedByBody)
      ? { resetAt: codexQuotaOutcomeMeta(response).resetAt }
      : {}),
    ...(usage ? { usage } : {}),
  };
}




export function usageFromComboFailureText(text: string): OcxUsage | undefined {
  try {
    const payload = JSON.parse(text) as Record<string, unknown>;
    const nested = payload.response;
    const source = nested && typeof nested === "object" && !Array.isArray(nested)
      ? nested as Record<string, unknown>
      : payload;
    return usageFromResponsesPayload(source.usage);
  } catch {
    return undefined;
  }
}




export function createChildPassthroughCallbackGate(options: HandleResponsesOptions) {
  type Pending =
    | { kind: "terminal"; status: ResponsesTerminalStatus }
    | { kind: "cancel" };
  let state: "pending" | "committed" | "discarded" = "pending";
  let pending: Pending | undefined;
  let accepted = false;
  let pendingModel: string | undefined;
  let completionAccepted = false;
  let completionRejected = false;
  const publish = (value: Pending): void => {
    if (value.kind === "terminal") options.onNativePassthroughTerminal?.(value.status);
    else options.onNativePassthroughCancel?.();
  };
  const publishCompletion = (): void => {
    if (state !== "committed" || completionRejected || pendingModel === undefined) return;
    const model = pendingModel;
    pendingModel = undefined;
    options.onResponseComplete?.(model);
  };
  const receive = (value: Pending): void => {
    if (state === "discarded" || accepted) return;
    accepted = true;
    if (value.kind === "cancel" || value.status !== "completed") {
      completionRejected = true;
      pendingModel = undefined;
    }
    if (state === "committed") return publish(value);
    pending ??= value;
  };
  return {
    onTerminal: (status: ResponsesTerminalStatus) => receive({ kind: "terminal", status }),
    onCancel: () => receive({ kind: "cancel" }),
    onResponseComplete: (model: string) => {
      if (state === "discarded" || completionRejected || completionAccepted || !model.trim()) return;
      completionAccepted = true;
      pendingModel = model;
      publishCompletion();
    },
    commit: () => {
      if (state !== "pending") return;
      state = "committed";
      if (pending) publish(pending);
      pending = undefined;
      publishCompletion();
    },
    discard: () => {
      state = "discarded";
      pending = undefined;
      pendingModel = undefined;
    },
  };
}



export function buildComboChildHeaders(parentHeaders: HeadersInit): Headers {
  const childHeaders = new Headers(parentHeaders);
  // A provisional caller credential is not authoritative for a Combo child.
  childHeaders.delete("authorization");
  childHeaders.delete("chatgpt-account-id");
  // Combo children re-serialize already-decoded JSON. Keeping transport metadata from
  // the parent would make the child decoder treat plain JSON as compressed bytes.
  childHeaders.delete("content-length");
  childHeaders.delete("content-encoding");
  return childHeaders;
}

/**
 * A member's local Ollama late-attribution refusal: the exact proxy-owned constant on the exact
 * local wire shape, from a replayable attempt. The caller replaces only the client response with
 * the literal constant envelope; classification inputs are never rewritten.
 */
export function isLateAttributionRefusal(failure: ConsumedComboFailure): boolean {
  return !failure.nonReplayable
    && failure.response.status === 413
    && failure.upstreamCode === "request_too_large"
    && failure.upstreamMessage === OLLAMA_LATE_ATTRIBUTION_LIMIT_MESSAGE;
}
