import type { CapturedLog, RequestFormat, TokenUsage } from "../schemas";

/**
 * Structured result of parsing a raw request body.
 */
export type ParsedRequest = {
  model: string | null;
  sessionId: string | null;
};

/**
 * FormatHandler defines the contract for all API format implementations.
 *
 * Each API format (Anthropic, OpenAI) provides its own handler that encapsulates
 * request parsing, response parsing, SSE stream extraction, and token extraction.
 * This replaces the scattered if/else format branching previously in handler.ts.
 */
export type FormatHandler = {
  /** The format identifier */
  readonly format: RequestFormat;

  /**
   * Parse a raw request body to extract model name and metadata.
   */
  parseRequest(rawBody: string, headers?: Headers): ParsedRequest | null;

  /**
   * Extract token usage from a non-streaming response body.
   */
  extractTokens(responseBody: string): TokenUsage;

  /**
   * Extract structured content from a streaming SSE response.
   */
  extractStream(
    raw: string,
    log: CapturedLog,
    fallbackModel?: string,
    collectChunks?: boolean,
  ): string;

  /**
   * Detect if a raw request body uses this format.
   */
  detectFormat(rawBody: string | null): boolean;
};
