import { CodexInstallationIdUnavailableError } from "../codex/installation-id";
import { formatErrorResponse } from "../bridge";
import {
  CodexAccountCooldownError,
  CodexAuthContextError,
  CodexMainProfileDrainingError,
  codexMainProfileDrainingResponse,
  cooldownErrorResponse,
  isCodexAuthContextUsable,
  materializeCodexUpstreamAuthAsync,
  releaseCodexAuthContextProbeLease,
  resolveCodexAuthContext,
  type CodexAuthContext,
} from "../codex/auth-context";
import { MAIN_CODEX_ACCOUNT_ID, MainAccountTokenRefreshError, MainAuthJsonChangedDuringRefreshError } from "../codex/main-account";
import { NativeProfileError } from "../codex/native-profile-types";
import { formatCodexProviderForLog } from "../codex/routing";
import type { AdmissionLease } from "../lib/admission";
import { captureExplicitOpenAiCallerAuth, resolveFirstUsableOpenAiSidecar, selectOpenAiImagesProvider } from "../providers/openai-sidecar";
import type { OcxConfig } from "../types";
import { admissionScopeDenial } from "./admission-model-scope";
import {
  isProxyAdmissionSecret,
  resolveDataPlaneAdmissionSecret,
  validateForwardAdmissionCredential,
  type DataPlaneAdmission,
  type DataPlaneAdmissionOptions,
} from "./auth-cors";
import { codexAccountSelectionForTurn } from "./lifecycle";
import type { RequestLogContext } from "./request-log";
import { authorizationCredential } from "../lib/authorization-scheme";
import { nativeMainRefreshFailureResponse } from "./responses/codex-auth-error";

export const TRANSCRIPTION_MODEL = "gpt-4o-transcribe";
export const LIVE_AUDIO_MODEL = "gpt-live-1-codex";

/** Audio keys remain identifiable even on the otherwise unauthenticated local listener. */
export function resolveAudioAdmission(
  headers: Headers,
  config: OcxConfig,
  options: DataPlaneAdmissionOptions = {},
): DataPlaneAdmission | null {
  const dedicated = headers.get("x-opencodex-api-key")?.trim();
  if (dedicated) return resolveDataPlaneAdmissionSecret(dedicated, config, "dedicated", options);
  const authorization = headers.get("authorization")?.trim();
  if (authorization) {
    const token = /^Bearer\s+([^\s,]+)$/i.exec(authorization)?.[1];
    return token ? resolveDataPlaneAdmissionSecret(token, config, "bearer", options) : null;
  }
  const key = headers.get("x-api-key")?.trim();
  return key ? resolveDataPlaneAdmissionSecret(key, config, "x-api-key", options) : null;
}

export interface AudioUpstream {
  providerName: string;
  providerBaseUrl: string;
  headers: Record<string, string>;
  keyed: boolean;
  authContext?: CodexAuthContext;
  recordOutcome?: (status: number | "timeout" | "connect_error") => void;
  release: () => void;
}

export interface AudioUpstreamOptions {
  admission: DataPlaneAdmission;
  model: string;
  lease?: AdmissionLease;
  exactAccountId?: string;
  providerName?: string;
  signal?: AbortSignal;
}

/** Keep client admission, account selection, and outbound credentials as separate values. */
export async function resolveAudioUpstream(
  incoming: Headers,
  config: OcxConfig,
  log: RequestLogContext,
  options: AudioUpstreamOptions,
): Promise<AudioUpstream | Response> {
  const candidates = selectOpenAiImagesProvider(config);
  if (options.providerName) {
    candidates.forwardCandidates = candidates.forwardCandidates.filter(candidate => candidate.providerName === options.providerName);
    if (candidates.keyed?.providerName !== options.providerName) delete candidates.keyed;
  }
  const headers = new Headers(incoming);
  const bearer = authorizationCredential(headers);
  if (bearer && isProxyAdmissionSecret(bearer, config)) {
    headers.delete("authorization");
    headers.delete("chatgpt-account-id");
  }
  headers.delete("x-opencodex-api-key");
  headers.delete("x-api-key");
  headers.delete("sec-websocket-protocol");
  let context: CodexAuthContext | undefined;
  try {
    options.signal?.throwIfAborted();
    const candidate = candidates.forwardCandidates[0];
    if (candidate) {
      let selected: Headers;
      let recordOutcome: AudioUpstream["recordOutcome"];
      const beginCodexAccountSelection = codexAccountSelectionForTurn(options.lease);
      if (candidate.accountMode === "direct" && options.exactAccountId === undefined
        && !captureExplicitOpenAiCallerAuth(incoming, config)) {
        context = await resolveCodexAuthContext(headers, config, "direct", {
          admission: options.admission,
          substituteMainCredentialForDirect: true,
          beginCodexAccountSelection,
          signal: options.signal,
        });
        options.signal?.throwIfAborted();
        selected = await materializeCodexUpstreamAuthAsync(headers, context, {
          config,
          admission: options.admission,
          substituteMainCredential: true,
          signal: options.signal,
        });
      } else {
        const resolved = await resolveFirstUsableOpenAiSidecar(candidates.forwardCandidates, headers, config, {
          admission: options.admission,
          beginCodexAccountSelection,
          signal: options.signal,
          ...(options.exactAccountId ? { exactAccount: { accountId: options.exactAccountId, modelId: options.model } } : {}),
        });
        if (!resolved) return formatErrorResponse(401, "authentication_error", "Connect a ChatGPT account to use audio");
        context = resolved.authContext;
        selected = resolved.headers;
        recordOutcome = resolved.recordOutcome;
      }
      options.signal?.throwIfAborted();
      if (!isCodexAuthContextUsable(context, config)) {
        releaseCodexAuthContextProbeLease(context);
        return formatErrorResponse(401, "authentication_error", "Selected audio account is unavailable");
      }
      validateForwardAdmissionCredential(selected, config);
      // Every audio surface — file transcription, the dictation socket, voice
      // call-create and the sideband join — resolves its upstream here, and
      // `options.model` is the model that upstream will run, not a selector the
      // caller can rewrite afterwards. One check therefore covers all of them.
      const forwardDenial = admissionScopeDenial(config, options.admission, options.model, {
        providerName: candidate.providerName,
        modelId: options.model,
      });
      if (forwardDenial) {
        releaseCodexAuthContextProbeLease(context);
        return forwardDenial;
      }
      log.provider = formatCodexProviderForLog(candidate.providerName, context.accountId, config);
      log.model = options.model;
      return {
        providerName: candidate.providerName,
        providerBaseUrl: candidate.provider.baseUrl,
        headers: Object.fromEntries(selected),
        keyed: false,
        authContext: context,
        recordOutcome,
        release: () => releaseCodexAuthContextProbeLease(context),
      };
    }
    if (candidates.keyed && options.exactAccountId === undefined) {
      const { providerName, provider, apiKey } = candidates.keyed;
      const selected = new Headers(provider.headers);
      selected.set("authorization", `Bearer ${apiKey}`);
      validateForwardAdmissionCredential(selected, config);
      const keyedDenial = admissionScopeDenial(config, options.admission, options.model, {
        providerName,
        modelId: options.model,
      });
      if (keyedDenial) return keyedDenial;
      log.provider = providerName;
      log.model = options.model;
      return {
        providerName, providerBaseUrl: provider.baseUrl, headers: Object.fromEntries(selected),
        keyed: true, release: () => {},
      };
    }
    return formatErrorResponse(400, "invalid_request_error", "Audio requires a connected ChatGPT account or OpenAI API provider");
  } catch (error) {
    releaseCodexAuthContextProbeLease(context);
    if (error instanceof CodexInstallationIdUnavailableError) return formatErrorResponse(503, "server_busy", error.message);
    if (error instanceof CodexAccountCooldownError) return cooldownErrorResponse(error);
    if (error instanceof CodexMainProfileDrainingError) return codexMainProfileDrainingResponse();
    const mainCause = error instanceof CodexAuthContextError && error.accountId === MAIN_CODEX_ACCOUNT_ID ? error.cause : error;
    if (mainCause instanceof MainAccountTokenRefreshError || mainCause instanceof MainAuthJsonChangedDuringRefreshError
      || (mainCause instanceof NativeProfileError && mainCause.retryable)) return nativeMainRefreshFailureResponse(mainCause);
    return formatErrorResponse(401, "authentication_error", "Audio account authentication unavailable; reconnect the selected OpenAI account");
  }
}
