import type { UpstreamConditionEvidence } from "../../lib/upstream-condition";
import { ownedAnthropicErrorPayload, ownedAnthropicErrorResponse, messagesOverflowPresentation, messagesRelayedOverflowPresentation } from "../../lib/messages-error-body";
import { proxyMessagePresentation, markProxyError, isProxyError, hasProxyErrorMessage } from "../../lib/client-error-body";
import { admitMessagesFailure, type MessagesFailureVerdictSink } from "../../claude/failure-verdict";
/**
 * AdapterEvent -> Anthropic Messages, without the internal Responses SSE (PF-09).
 *
 * The writer reproduces `responsesSseToAnthropicSse` (src/claude/outbound.ts) for the frames the
 * bridge produced: `message_start` + `ping` on the first semantic output (never before an initial
 * error), text blocks per assistant message, thinking blocks buffered until their item closes
 * and then emitted with the genuine signature or the bounded ocxr1 fallback, redacted blocks
 * ahead of the thinking block they came with, tool_use blocks with streamed `input_json_delta`
 * (WebSearch input buffered and sanitized), the server_tool_use / web_search_tool_result pair,
 * `message_delta` + `message_stop`, and mid-stream `error` frames. Pings follow the bridge's
 * wire-silence heartbeat and the converter's 20 s keepalive, only while the client has demand.
 */
import type { AdapterEvent } from "../../types";
import {
  isTranslatorBudgetExceededError,
  type TranslatorBudget,
} from "../../lib/translator-budget";
import { isTransientUpstreamStatus } from "../../lib/upstream-retry";
import { UNDECLARED_TOOL_CALL_ERROR_CODE } from "../../lib/errors";
import { encodeReasoningEnvelope } from "../../responses/reasoning-envelope";
import { responsesUsage, webSearchAction } from "../../bridge/internal";
import {
  anthropicErrorBody,
  anthropicErrorType,
  isThroughputLimitText,
  anthropicFailedStatus,
  anthropicIncompleteOutcome,
  anthropicUsage,
  boundedReasoningIdentity,
  collectAnthropicMessage,
  isClaudeWebSearchToolName,
  isContextOverflowError,
  messageSnapshot,
  sanitizeWebSearchInput,
  sseFrame,
  webSearchPairFromItem,
} from "../../claude/outbound";
import {
  encodeAdapterEventStream,
  isEncoderTransportFailure,
  encoderOriginalErrorEvidence,
  type AdapterEventEncodeOptions,
  type ClientFrameSink,
  type ClientWireWriter,
} from "./adapter-events";
import type { RelayedEventObservation } from "../../usage/attempt-delivery";

type Rec = Record<string, unknown>;

/** The converter's transport keepalive interval. */
const MESSAGES_KEEPALIVE_MS = 20_000;

export interface AnthropicMessageEncodeOptions extends AdapterEventEncodeOptions {
  /** The model string the client asked for; `message_start` and the folded message carry it. */
  model: string;
  /** This proxy's prompt estimate for `message_start` when no usage arrived first (#4857). */
  inputTokenFloor?: number;
  failureVerdictSink?: MessagesFailureVerdictSink;
  proxyErrorContext?: object;
}

interface OpenBlock {
  kind: "text" | "thinking" | "tool_use";
  index: number;
  bufferWebSearchArgs?: boolean;
  argsBuf?: string;
  argsBufBytes?: number;
  webSearchArgsEmitted?: boolean;
  toolArgsEmitted?: boolean;
  callId?: string;
  reasoningPartKey?: string;
  reasoningItemKey?: string;
  thinkingBuf?: string;
  thinkingBufBytes?: number;
  reasoningSig?: string;
}

function appendedUtf8Bytes(previous: string, previousBytes: number, fragment: string): number {
  let nextBytes = previousBytes + Buffer.byteLength(fragment);
  const previousLast = previous.charCodeAt(previous.length - 1);
  const fragmentFirst = fragment.charCodeAt(0);
  if (previousLast >= 0xd800 && previousLast <= 0xdbff
    && fragmentFirst >= 0xdc00 && fragmentFirst <= 0xdfff) {
    nextBytes -= 2;
  }
  return nextBytes;
}

function createAnthropicMessageWriter(
  sink: ClientFrameSink,
  model: string,
  inputTokenFloor: number | undefined,
  options: AnthropicMessageEncodeOptions,
): ClientWireWriter {
  const budget = sink.budget;
  let started = false;
  let terminated = false;
  // Only a delivered terminal forbids the bounded overflow error.
  let terminalDelivered = false;
  let blockIndex = 0;
  let open: OpenBlock | null = null;
  let sawToolUse = false;
  let webSearchRequests = 0;

  const emit = (name: string, data: Rec, observation?: RelayedEventObservation) => sink.emit(sseFrame(name, data), observation);
  const releaseThinkingBuffer = (block: OpenBlock | null) => {
    if (block?.kind !== "thinking") return;
    budget.releaseRetained(block.thinkingBufBytes ?? 0, { kind: "reasoning" });
    block.thinkingBufBytes = 0;
  };
  const appendRetained = (previous: string, previousBytes: number, fragment: string, scope: { kind: "reasoning" | "tool_args"; callId?: string }) => {
    const nextBytes = appendedUtf8Bytes(previous, previousBytes, fragment);
    const reservation = budget.reserveTransient(nextBytes, scope);
    try {
      const value = previous + fragment;
      reservation.commitRetained();
      budget.releaseRetained(previousBytes, scope);
      return { value, bytes: nextBytes };
    } catch (error) {
      reservation.release();
      throw error;
    }
  };
  const ensureStarted = () => {
    if (started) return;
    started = true;
    emit("message_start", { type: "message_start", message: messageSnapshot(model, undefined, inputTokenFloor) });
    emit("ping", { type: "ping" });
  };
  const closeOpenBlock = () => {
    if (!open) return;
    if (open.kind === "tool_use" && open.bufferWebSearchArgs && !open.webSearchArgsEmitted) {
      let parsed: unknown = {};
      const rawArgs = open.argsBuf ?? "";
      try { parsed = rawArgs.length > 0 ? JSON.parse(rawArgs) : {}; } catch { parsed = {}; }
      emit("content_block_delta", {
        type: "content_block_delta",
        index: open.index,
        delta: { type: "input_json_delta", partial_json: JSON.stringify(sanitizeWebSearchInput(parsed)) },
      });
      open.webSearchArgsEmitted = true;
    }
    if (open.kind === "thinking") {
      // The index and every thinking frame wait for closure, so redacted blocks from the
      // same item can go first.
      open.index = blockIndex++;
      emit("content_block_start", {
        type: "content_block_start", index: open.index,
        content_block: { type: "thinking", thinking: "", signature: "" },
      });
      if (open.thinkingBuf) {
        emit("content_block_delta", {
          type: "content_block_delta", index: open.index,
          delta: { type: "thinking_delta", thinking: open.thinkingBuf },
        }, { semanticBytes: Buffer.byteLength(open.thinkingBuf) });
      }
      const signature = open.reasoningSig ?? encodeReasoningEnvelope({ txt: open.thinkingBuf ?? "" }, budget);
      emit("content_block_delta", {
        type: "content_block_delta", index: open.index,
        delta: { type: "signature_delta", signature },
      });
    }
    emit("content_block_stop", { type: "content_block_stop", index: open.index });
    releaseThinkingBuffer(open);
    if (open.callId) budget.closeCall(open.callId);
    open = null;
  };
  const ensureBlock = (kind: "text" | "thinking") => {
    ensureStarted();
    if (open && open.kind === kind) return;
    closeOpenBlock();
    if (kind === "thinking") {
      open = { kind, index: -1, thinkingBuf: "", thinkingBufBytes: 0 };
      return;
    }
    const index = blockIndex++;
    emit("content_block_start", { type: "content_block_start", index, content_block: { type: "text", text: "" } });
    open = { kind, index };
  };
  const finish = (stopReason: string, usage: unknown) => {
    if (terminated) return;
    terminated = true;
    ensureStarted();
    closeOpenBlock();
    emit("message_delta", {
      type: "message_delta",
      delta: { stop_reason: stopReason, stop_sequence: null },
      usage: anthropicUsage(usage, webSearchRequests),
    });
    emit("message_stop", { type: "message_stop" }, { terminal: true });
    terminalDelivered = true;
  };
  // Transient upstream statuses become overloaded_error so Anthropic SDK clients back off.
  const fail = (status: number, message: string, upstreamDerived = false, code?: string, local = false, contract?: "stream_incomplete" | "transport_failed", evidence?: UpstreamConditionEvidence) => {
    if (terminated && (code !== "translation_buffer_limit" || terminalDelivered)) return;
    terminated = true;
    const type = code === "translation_buffer_limit" ? "request_too_large"
      : upstreamDerived && isTransientUpstreamStatus(status) ? "overloaded_error" : anthropicErrorType(status);
    const legacy = anthropicErrorBody(status, message, type, code);
    const payload = ownedAnthropicErrorPayload({ status, type, code, evidence: evidence ?? { status, message }, contract,
      overflow: messagesOverflowPresentation((legacy.error as Rec).message),
      condition: code === "context_length_exceeded" && isThroughputLimitText(message) ? "rate_limited" : undefined,
      ...(local ? { localError: markProxyError({ message }, proxyMessagePresentation(options.proxyErrorContext, message, { type: evidence?.type, code: evidence?.code ?? code })) } : {}),
    });
    const admitted = () => admitMessagesFailure(options.failureVerdictSink, payload, legacy);
    if (code === "translation_buffer_limit") {
      releaseThinkingBuffer(open);
      if (open?.callId) budget.closeCall(open.callId);
      open = null;
      terminalDelivered = true;
      // No normal close frames are valid after overflow: one bounded typed terminal.
      sink.emitBounded(sseFrame("error", payload), admitted);
      return;
    }
    // An initial failure is an error stream, not a partial message: no message_start first.
    if (started) closeOpenBlock();
    sink.emit(sseFrame("error", payload), { terminal: true }, admitted, sseFrame("error", legacy));
    terminalDelivered = true;
  };

  return {
    start() { /* the bridge's lifecycle preludes carry no usage, so nothing starts here */ },
    heartbeat() {
      if (terminated || sink.desiredSize() <= 0) return;
      sink.emitKeepalive(sseFrame("ping", { type: "ping" }));
    },
    text(delta) {
      if (!delta) return;
      ensureBlock("text");
      const active = open;
      if (!active || active.kind !== "text") return;
      emit("content_block_delta", {
        type: "content_block_delta", index: active.index,
        delta: { type: "text_delta", text: delta },
      }, { semanticBytes: Buffer.byteLength(delta) });
    },
    messageDone() {
      if (open && open.kind === "text") closeOpenBlock();
    },
    reasoning(delta, itemId, channel) {
      if (!delta) return;
      const itemKey = boundedReasoningIdentity(itemId);
      if (open?.kind === "thinking" && open.reasoningItemKey !== itemKey) closeOpenBlock();
      ensureBlock("thinking");
      const active = open as OpenBlock | null;
      if (!active || active.kind !== "thinking") return;
      // The bridge writes summary deltas at summary_index 0 and raw deltas at content_index 0.
      const slot = channel === "summary" ? `s${boundedReasoningIdentity(0)}` : `c${boundedReasoningIdentity(0)}`;
      const partKey = `${itemKey}:${slot}`;
      const needsPartSeparator = active.reasoningPartKey !== undefined && active.reasoningPartKey !== partKey;
      const next = appendRetained(
        active.thinkingBuf ?? "",
        active.thinkingBufBytes ?? 0,
        `${needsPartSeparator ? "\n\n" : ""}${delta}`,
        { kind: "reasoning" },
      );
      active.thinkingBuf = next.value;
      active.thinkingBufBytes = next.bytes;
      active.reasoningItemKey = itemKey;
      active.reasoningPartKey = partKey;
    },
    reasoningDone({ itemId, signature, redacted }) {
      const red = redacted ?? [];
      const itemKey = boundedReasoningIdentity(itemId);
      // A late or unrelated item cannot reorder or sign another item's text.
      if (open?.kind === "thinking" && open.reasoningItemKey !== itemKey) closeOpenBlock();
      if (red.length > 0) {
        ensureStarted();
        if (open?.kind !== "thinking") closeOpenBlock();
      }
      for (const data of red) {
        const index = blockIndex++;
        emit("content_block_start", { type: "content_block_start", index, content_block: { type: "redacted_thinking", data } });
        emit("content_block_stop", { type: "content_block_stop", index });
      }
      if (signature && open?.kind !== "thinking") ensureBlock("thinking");
      const active = open as OpenBlock | null;
      if (active?.kind === "thinking") {
        if (signature) active.reasoningSig = signature;
        closeOpenBlock();
      }
    },
    toolStart(call) {
      // Custom and tool-search calls have no Anthropic representation; the converter skips them.
      if (call.kind !== "function") return;
      ensureStarted();
      closeOpenBlock();
      sawToolUse = true;
      const index = blockIndex++;
      emit("content_block_start", {
        type: "content_block_start", index,
        content_block: { type: "tool_use", id: call.callId, name: call.name, input: {} },
      }, { sideEffect: true });
      budget.openCall(call.callId);
      open = {
        kind: "tool_use",
        index,
        callId: call.callId,
        bufferWebSearchArgs: isClaudeWebSearchToolName(call.name),
        argsBuf: "",
        argsBufBytes: 0,
        webSearchArgsEmitted: false,
        toolArgsEmitted: false,
      };
    },
    toolArgsDelta(_itemId, delta) {
      if (!delta) return;
      if (!open || open.kind !== "tool_use") return;
      if (open.bufferWebSearchArgs) {
        const next = appendRetained(open.argsBuf ?? "", open.argsBufBytes ?? 0, delta, {
          kind: "tool_args",
          ...(open.callId ? { callId: open.callId } : {}),
        });
        open.argsBuf = next.value;
        open.argsBufBytes = next.bytes;
        return;
      }
      emit("content_block_delta", {
        type: "content_block_delta", index: open.index,
        delta: { type: "input_json_delta", partial_json: delta },
      }, { semanticBytes: Buffer.byteLength(delta) });
      open.toolArgsEmitted = true;
    },
    toolArgsDone(_itemId, args) {
      if (!open || open.kind !== "tool_use" || open.bufferWebSearchArgs || open.toolArgsEmitted) return;
      if (args.length === 0) return;
      emit("content_block_delta", {
        type: "content_block_delta", index: open.index,
        delta: { type: "input_json_delta", partial_json: args },
      }, { semanticBytes: Buffer.byteLength(args) });
      open.toolArgsEmitted = true;
    },
    toolDone(call) {
      if (call.kind !== "function" || !open || open.kind !== "tool_use") return;
      if (open.bufferWebSearchArgs && !open.webSearchArgsEmitted) {
        const rawArgs = call.arguments.length > 0 ? call.arguments : (open.argsBuf ?? "");
        let parsed: unknown = {};
        try { parsed = rawArgs.length > 0 ? JSON.parse(rawArgs) : {}; } catch { parsed = {}; }
        emit("content_block_delta", {
          type: "content_block_delta",
          index: open.index,
          delta: { type: "input_json_delta", partial_json: JSON.stringify(sanitizeWebSearchInput(parsed)) },
        });
        open.webSearchArgsEmitted = true;
      } else if (!open.bufferWebSearchArgs && !open.toolArgsEmitted && call.arguments.length > 0) {
        emit("content_block_delta", {
          type: "content_block_delta", index: open.index,
          delta: { type: "input_json_delta", partial_json: call.arguments },
        }, { semanticBytes: Buffer.byteLength(call.arguments) });
        open.toolArgsEmitted = true;
      }
      closeOpenBlock();
    },
    webSearchDone(search) {
      // Server-side search becomes the pair Claude Code parses natively. It never marks a
      // tool use, so the stop reason stays end_turn unless a real tool ran.
      ensureStarted();
      closeOpenBlock();
      const pair = webSearchPairFromItem({
        type: "web_search_call",
        id: search.itemId,
        status: search.status,
        action: webSearchAction(search.queries, search.anthropic),
        ...(search.sources.length > 0 ? { sources: search.sources } : {}),
        ...(search.anthropic ? { anthropic_blocks: search.anthropic } : {}),
      });
      if (!pair) return;
      const toolIndex = blockIndex++;
      emit("content_block_start", {
        type: "content_block_start", index: toolIndex,
        content_block: { type: "server_tool_use", id: pair.id, name: pair.name },
      }, { sideEffect: true });
      emit("content_block_delta", {
        type: "content_block_delta", index: toolIndex,
        delta: { type: "input_json_delta", partial_json: JSON.stringify(pair.input) },
      });
      emit("content_block_stop", { type: "content_block_stop", index: toolIndex });
      const resultIndex = blockIndex++;
      emit("content_block_start", {
        type: "content_block_start", index: resultIndex,
        content_block: { type: pair.resultType, tool_use_id: pair.id, content: pair.resultContent },
      });
      emit("content_block_stop", { type: "content_block_stop", index: resultIndex });
      if (pair.completed && pair.name === "web_search") webSearchRequests++;
    },
    terminal(terminal) {
      if (terminal.status === "completed") {
        if (terminal.endTurn === false && !sawToolUse) {
          fail(529, "upstream turn ended without a final answer", true, undefined, true);
          return;
        }
        finish(sawToolUse ? "tool_use" : "end_turn", responsesUsage(terminal.usage));
        return;
      }
      if (terminal.status === "incomplete") {
        const outcome = anthropicIncompleteOutcome(terminal.incomplete?.reason, terminal.incomplete?.message);
        if ("stopReason" in outcome) finish(outcome.stopReason, terminal.usageOnWire ? responsesUsage(terminal.usage) : null);
        else fail(529, outcome.failMessage, true, undefined, isProxyError(terminal), "stream_incomplete");
        return;
      }
      const error = (terminal.error ?? {}) as unknown as Rec;
      if (error.code === "translation_buffer_limit") {
        fail(413, "upstream translation buffer exceeded the safe limit", false, "translation_buffer_limit", true);
        return;
      }
      const message = typeof error.message === "string" ? error.message : "upstream request failed";
      const deterministic = error.code === UNDECLARED_TOOL_CALL_ERROR_CODE;
      const transportFailure = isEncoderTransportFailure(terminal);
      const classified = transportFailure ? { ...error, code: "upstream_stream_error", status: 502 } : error;
      const overflow = isContextOverflowError(classified, message);
      const original = encoderOriginalErrorEvidence(terminal);
      fail(anthropicFailedStatus(classified, message), message, !deterministic,
        deterministic ? error.code as string : overflow ? "context_length_exceeded" : undefined,
        hasProxyErrorMessage(options.proxyErrorContext, message, { type: typeof error.type === "string" ? error.type : undefined, code: typeof error.code === "string" ? error.code : undefined }), transportFailure ? "transport_failed" : undefined,
        { ...(original ?? { message, type: typeof error.type === "string" ? error.type : undefined, code: typeof error.code === "string" ? error.code : undefined }), status: original?.status ?? anthropicFailedStatus(classified, message) });
    },
    overflow() {
      fail(413, "upstream translation buffer exceeded the safe limit", false, "translation_buffer_limit", true);
    },
    dispose() {
      releaseThinkingBuffer(open);
      if (open?.callId) budget.closeCall(open.callId);
    },
  };
}

/** Stream adapter events as Anthropic Messages SSE bytes. */
export function encodeAnthropicMessageSse(
  events: AsyncIterable<AdapterEvent>,
  options: AnthropicMessageEncodeOptions,
): ReadableStream<Uint8Array> {
  return encodeAdapterEventStream(
    events,
    sink => createAnthropicMessageWriter(sink, options.model, options.inputTokenFloor, options),
    { ...options, get localUpstream() { return options.localUpstream; }, keepaliveMs: MESSAGES_KEEPALIVE_MS },
  );
}

/**
 * Fold an encoded Messages stream into the non-streaming client response, with the status
 * mapping the Messages ingress applied to its collected message: a translator overflow is 413,
 * a classified context rejection is 400; other error frames/unexpected failures stay 502.
 */
export async function collectAnthropicMessageResponse(
  stream: ReadableStream<Uint8Array>,
  model: string,
  translatorBudget: TranslatorBudget,
  failureVerdictSink?: MessagesFailureVerdictSink,
): Promise<Response> {
  let message: Rec;
  const collectorVerdictSink: MessagesFailureVerdictSink = failureVerdictSink ?? {};
  try {
    message = await collectAnthropicMessage(stream, model, translatorBudget, collectorVerdictSink);
  } catch (error) {
    if (isTranslatorBudgetExceededError(error)) {
      const legacy = anthropicErrorBody(413, error.message, "request_too_large", "translation_buffer_limit");
      return ownedAnthropicErrorResponse({ status: 413, type: "request_too_large", code: "translation_buffer_limit",
        evidence: { status: 413, message: error.message }, overflow: messagesRelayedOverflowPresentation("", legacy) }, legacy);
    }
    const message = error instanceof Error ? error.message : String(error);
    const legacy = anthropicErrorBody(502, message, "api_error");
    return ownedAnthropicErrorResponse({ status: 502, type: "api_error", evidence: { status: 502, message },
      contract: "transport_failed", overflow: messagesRelayedOverflowPresentation("", legacy) }, legacy);
  }
  const legacy = collectorVerdictSink.collected ?? message;
  const isError = legacy.type === "error";
  const translatedError = isError && typeof legacy.error === "object"
    ? (legacy as { error: { code?: unknown; message?: unknown } }).error
    : undefined;
  if (translatedError?.code === "translation_buffer_limit") {
    const originalMessage = typeof translatedError.message === "string" ? translatedError.message : "upstream translation buffer exceeded the safe limit";
    const legacy = anthropicErrorBody(413, originalMessage, "request_too_large", "translation_buffer_limit");
    return ownedAnthropicErrorResponse({ status: 413, type: "request_too_large", code: "translation_buffer_limit",
      evidence: { status: 413, message: originalMessage }, overflow: messagesRelayedOverflowPresentation("", legacy) }, legacy);
  }
  const baselineOverflow = isError && translatedError?.code === "context_length_exceeded";
  const status = isError ? (baselineOverflow ? 400 : 502) : 200;
  if (collectorVerdictSink.collectedErrorEvent && !collectorVerdictSink.collectedProjection) {
    const error = (message.error ?? {}) as Rec;
    const originalMessage = typeof error.message === "string" ? error.message : "";
    const throughput = isThroughputLimitText(originalMessage);
    message = ownedAnthropicErrorPayload({ status, type: typeof error.type === "string" ? error.type : undefined,
      code: typeof error.code === "string" ? error.code : undefined,
      evidence: { status, message: originalMessage },
      overflow: messagesRelayedOverflowPresentation(JSON.stringify(message), message),
      ...(error.code === "context_length_exceeded" && throughput ? { condition: "rate_limited" as const } : {}),
    });
  }
  return new Response(JSON.stringify(message), {
    status,
    headers: { "Content-Type": "application/json" },
  });
}

/** Non-streaming client: the encoded stream folded exactly as the Messages collector folds it. */
export function foldAnthropicMessage(
  events: AsyncIterable<AdapterEvent>,
  options: AnthropicMessageEncodeOptions,
): Promise<Response> {
  const failureVerdictSink: MessagesFailureVerdictSink = options.failureVerdictSink ?? {};
  return collectAnthropicMessageResponse(
    encodeAnthropicMessageSse(events, { ...options, failureVerdictSink }),
    options.model,
    options.translatorBudget,
    failureVerdictSink,
  );
}
