import {
  compressedBodyDecodeLimit,
  MAX_DECOMPRESSED_BODY_BYTES,
  resolveInboundBodyLimitBytes,
} from "./request-decompress";
import { withCors, type RequestPolicyView } from "./auth-cors";
import { codexCompatibleUrl } from "../codex/context-compat";
import { addFinalRequestLog, markLocalRequestLogRefusal } from "./request-log";
import type { WorkflowRefusalLog } from "./workflow-refusal";
import { inboundBodyBudget, bindInboundBodyLease, type InboundBodyBudget } from "./inbound-body-budget";
import { isGeminiBodyPath } from "./gemini-native-paths";
import { OLLAMA_JSON_BODY_ROUTES } from "./ollama-native-paths";

export { configureInboundBodyBudget, MAX_CONCURRENT_INBOUND_BODY_BYTES } from "./inbound-body-budget";

// Match the HTTP routes whose JSON readers use maxInboundBodyBytes. Keep the
// current image/search/count_tokens support; management, audio, context relay,
// and WebSocket frames have separate limits and are not part of this gate.
// Exported so the contract tests classify the dispatcher's own route list
// instead of copying it: a new loopback route has to land in one of these two
// sets, or `tests/server/inbound-body-admission.test.ts` fails.
export const CONFIGURABLE_JSON_BODY_ROUTES: ReadonlySet<string> = new Set([
  "/v1/responses",
  "/v1/responses/compact",
  "/v1/chat/completions",
  "/v1/messages",
  "/v1/messages/count_tokens",
  "/v1/images/generations",
  "/v1/images/edits",
  "/v1/alpha/search",
]);

/**
 * Loopback routes deliberately outside this gate. Audio uploads, realtime/live
 * control and the model list do not read JSON under `maxInboundBodyBytes`, so
 * reserving that allowance for them would refuse traffic the limit never covered.
 */
export const UNGATED_LOOPBACK_ROUTES: ReadonlySet<string> = new Set([
  "/v1/audio/transcriptions",
  "/v1/audio/transcriptions/stream",
  "/v1/models",
  "/v1/realtime",
  "/v1/realtime/calls",
  "/v1/live",
]);

export class InboundBodyCapacityError extends Error {
  constructor() {
    super("OpenCodex inbound request body capacity is temporarily exhausted. Retry after the current request completes.");
    this.name = "InboundBodyCapacityError";
  }
}

function cancelUnreadBody(req: Request, reason: unknown): void {
  if (!req.body || req.body.locked) return;
  try {
    // A tee's cancellation can wait for the other branch. Refusal must not wait
    // for a peer that has not finished uploading, or for an unconsumed clone.
    void req.body.cancel(reason).catch(() => undefined);
  } catch { /* Non-standard streams may throw synchronously. */ }
}

function capacityResponse(pathname: string, error: InboundBodyCapacityError): Response {
  const anthropic = pathname === "/v1/messages" || pathname === "/v1/messages/count_tokens";
  return Response.json({
    ...(anthropic ? { type: "error" } : {}),
    error: {
      type: anthropic ? "overloaded_error" : "server_error",
      code: "server_busy",
      message: error.message,
    },
  }, { status: 503, headers: { "Retry-After": "1" } });
}

function retainUntilResponseSettles(response: Response, signal: AbortSignal, release: () => void): Response {
  if (!response.body) {
    release();
    return response;
  }
  const reader = response.body.getReader();
  let cancelling = false;
  let cancellation: Promise<void> | undefined;
  let finalized = false;
  let bodyController!: ReadableStreamDefaultController<Uint8Array>;
  const onAbort = (): void => {
    if (finalized) return;
    bodyController.error(signal.reason);
    void cancel(signal.reason).catch(() => undefined);
  };
  const finalize = (): void => {
    if (finalized) return;
    finalized = true;
    signal.removeEventListener("abort", onAbort);
    try { reader.releaseLock(); } finally { release(); }
  };
  const cancel = (reason: unknown): Promise<void> => {
    if (cancellation) return cancellation;
    if (finalized) return Promise.resolve();
    cancelling = true;
    // Share one settlement even if explicit cancellation races request abort.
    cancellation = Promise.resolve().then(() => reader.cancel(reason)).finally(finalize);
    return cancellation;
  };
  const body = new ReadableStream<Uint8Array>({
    start(controller) { bodyController = controller; },
    async pull(controller) {
      try {
        const result = await reader.read();
        // cancel() can resolve a pending read as EOF before the producer's
        // asynchronous cancellation completes. Only cancel owns release then.
        if (cancelling) return;
        if (result.done) {
          finalize();
          controller.close();
        } else {
          controller.enqueue(result.value);
        }
      } catch (error) {
        if (cancelling) return;
        finalize();
        controller.error(error);
      }
    },
    cancel,
  }, { highWaterMark: 0 });
  // This is the outermost HTTP wrapper, after protocol conversion, relay
  // preflight and request logging; no internal relay marker is consumed later.
  const wrapped = new Response(body, {
    status: response.status,
    statusText: response.statusText,
    headers: response.headers,
  });
  signal.addEventListener("abort", onAbort, { once: true });
  if (signal.aborted) onAbort();
  return wrapped;
}

/** A body that will be decoded: any Content-Encoding other than identity. */
function encodedBody(req: Request): boolean {
  const encoding = req.headers.get("content-encoding")?.trim().toLowerCase();
  return encoding !== undefined && encoding !== "" && encoding !== "identity";
}

/**
 * Own one raised-limit or encoded HTTP request from before body reading through response
 * EOF/error or settled cancellation. An encoded body joins the shared budget at every limit
 * (#3023): it reserves what it may decode to (`compressedBodyDecodeLimit` of its declared wire
 * bytes, or the whole limit without a declaration) before a byte is read, and the reader shrinks
 * that to the measured size, so the decoded bodies held at once stay within the budget however
 * small their wire bytes are. An identity body at the default limit is not gated, as before. Call only at the admitted HTTP boundary,
 * after auth/origin checks, not from readers or internal combo/translation hops.
 * pathname is the same canonical pathname used by the server's dispatcher.
 * The optional budget/wait override isolates lifecycle tests from the process-wide owner.
 */
export async function withRaisedInboundBodyAdmission(
  req: Request,
  pathname: string,
  configuredLimit: number | undefined,
  work: () => Promise<Response>,
  onRefusal: (response: Response) => Response = response => response,
  options?: { budget?: InboundBodyBudget; maxWaitMs?: number; onAdmissionError?: (error: unknown) => Response },
): Promise<Response> {
  const maxBytes = resolveInboundBodyLimitBytes(configuredLimit);
  // Native Ollama generation routes are served only on the authenticated listener, so they sit in
  // their own set: the loopback oracle checks CONFIGURABLE_JSON_BODY_ROUTES against that allowlist.
  const raised = maxBytes > MAX_DECOMPRESSED_BODY_BYTES;
  if (req.method !== "POST" || !(CONFIGURABLE_JSON_BODY_ROUTES.has(pathname) || OLLAMA_JSON_BODY_ROUTES.has(pathname) || isGeminiBodyPath(pathname))
    || !(raised || encodedBody(req)) || req.signal.aborted) {
    return work();
  }
  const declared = req.headers.get("content-length");
  const declaredBytes = declared === null || declared.trim() === "" ? NaN : Number(declared);
  if (Number.isFinite(declaredBytes) && declaredBytes > maxBytes) {
    // The existing bounded reader rejects this before allocating. Let its
    // protocol-specific 413 win over temporary capacity refusal, unchanged.
    return work();
  }
  const reservation = raised || !Number.isFinite(declaredBytes) ? maxBytes : compressedBodyDecodeLimit(declaredBytes, maxBytes);
  let lease;
  try {
    lease = await (options?.budget ?? inboundBodyBudget).acquire(reservation, req.signal, options?.maxWaitMs);
  } catch (error) {
    cancelUnreadBody(req, error);
    if (options?.onAdmissionError) return options.onAdmissionError(error);
    throw error;
  }
  if (!lease) {
    const error = new InboundBodyCapacityError();
    cancelUnreadBody(req, error);
    return onRefusal(capacityResponse(pathname, error));
  }
  // An immediate grant can race a disconnect before this async function resumes.
  if (req.signal.aborted) {
    lease.release();
    cancelUnreadBody(req, req.signal.reason);
    if (options?.onAdmissionError) return options.onAdmissionError(req.signal.reason);
    throw req.signal.reason;
  }
  const unbind = bindInboundBodyLease(req, lease);
  const release = (): void => { unbind(); lease.release(); };
  try {
    return retainUntilResponseSettles(await work(), req.signal, release);
  } catch (error) {
    release();
    throw error;
  }
}

/**
 * The admitted HTTP call site. Refusal is recorded as a local terminal and
 * carries the receiving listener's CORS headers, so a browser dashboard can
 * read it. No request content is read or logged on this path.
 */
export function runAdmittedBodyWork(
  req: Request,
  policy: RequestPolicyView,
  configuredLimit: number | undefined,
  work: () => Promise<Response>,
  refusalLog?: WorkflowRefusalLog,
  mapRefusal?: (response: Response) => Response,
  onAdmissionError?: (error: unknown) => Response,
): Promise<Response> {
  return withRaisedInboundBodyAdmission(req, codexCompatibleUrl(req.url).pathname, configuredLimit, work, refused => {
    if (refusalLog) {
      markLocalRequestLogRefusal(refusalLog.logCtx, "server_busy");
      refusalLog.logCtx.errorCode = "server_busy";
      addFinalRequestLog(refusalLog.requestId, refusalLog.start, refusalLog.logCtx, refused.status, {
        closeReason: "terminal",
      });
      refusalLog.onLogged?.();
    }
    return withCors(mapRefusal ? mapRefusal(refused) : refused, req, policy);
  }, onAdmissionError ? { onAdmissionError } : undefined);
}
