import { createRegisteredAdapter } from "../adapters/registry";
import type { OcxProviderConfig } from "../types";
import { isWirePinnedModel, MODEL_ADAPTER_OVERRIDE_ALLOWED, pinnedWireAdapter } from "../types";
import { isCanonicalOpenAiForwardProvider } from "../providers/openai-tiers";
import { type InboundWire, providerModelWireDefault } from "../providers/registry";
import type { ResolvedModelPolicy } from "../providers/resolved-model-policy";
import { GCP_CREDENTIAL_MARKER_PREFIX } from "../providers/api-key-resolve";

function selectWire(provider: OcxProviderConfig, adapter: string): OcxProviderConfig {
  const stripMarker = adapter !== "google" && provider.apiKey?.startsWith(GCP_CREDENTIAL_MARKER_PREFIX);
  if (adapter === provider.adapter && !stripMarker) return provider;
  return { ...provider, adapter, ...(stripMarker ? { apiKey: undefined } : {}) };
}

/**
 * Project only the captured exact-model custom-tool contract onto a call-local provider copy.
 * The captured route policy already folds the provider value under any model declaration, so a
 * refreshed transport cannot add or keep a capability the captured route did not resolve. Native
 * compact uses this alone: it keeps the provider's own wire and credential.
 */
export function projectCapturedCustomToolPolicy(
  provider: OcxProviderConfig,
  staticPolicy: ResolvedModelPolicy | undefined,
): OcxProviderConfig {
  if (!staticPolicy) return provider;
  const supportsResponsesCustomTools = staticPolicy.model.supportsResponsesCustomTools;
  if (supportsResponsesCustomTools === provider.supportsResponsesCustomTools) return provider;
  if (supportsResponsesCustomTools === undefined) {
    const { supportsResponsesCustomTools: _discardedLiveCapability, ...captured } = provider;
    return captured;
  }
  return { ...provider, supportsResponsesCustomTools };
}

/**
 * Resolve the wire a single model should use: a hard pin first, then a configured
 * per-model override, then a registry default for a mixed-wire provider, then the provider's
 * own adapter.
 *
 * Safe to call more than once on its own output — the pin check does not look at the
 * current adapter, so a second pass cannot let an override displace a pin.
 *
 * `inbound` is the protocol the ORIGINAL client spoke. It defaults to `"responses"`
 * because the Chat and Anthropic surfaces translate into a Responses-shaped body and
 * replay through `handleResponses`; those two callers pass their real inbound so a
 * scoped registry default cannot fire for a client that never asked for that wire.
 */
export function resolveWireProtocolOverride(
  providerName: string,
  modelId: string,
  providerConfig: OcxProviderConfig,
  inbound: InboundWire = "responses",
  staticPolicy?: ResolvedModelPolicy,
): OcxProviderConfig {
  // RouteResult policy is recaptured for the original inbound protocol whenever routing selects
  // or replaces a destination, so every downstream rebuild consumes the same adapter authority.
  if (staticPolicy) {
    return projectCapturedCustomToolPolicy(selectWire(providerConfig, staticPolicy.model.adapter), staticPolicy);
  }
  const pinned = pinnedWireAdapter(providerName, modelId, providerConfig);
  if (pinned && providerConfig.adapter !== pinned) {
    return selectWire(providerConfig, pinned);
  }
  // Re-check the allow-list here, not just in the config validator: the file may have
  // been hand-edited, or written by a build that allowed more values.
  const configured = providerConfig.modelAdapters?.[modelId];
  // An explicit allowed override wins, including one naming the provider-wide adapter (the
  // opt-out from a registry default). Invalid hand-edited values fall through to the default.
  const requested = configured && MODEL_ADAPTER_OVERRIDE_ALLOWED.has(configured)
    ? configured
    : providerModelWireDefault(providerName, providerConfig, modelId, MODEL_ADAPTER_OVERRIDE_ALLOWED, inbound);
  if (requested
    && MODEL_ADAPTER_OVERRIDE_ALLOWED.has(requested)
    && requested !== providerConfig.adapter
    && !isWirePinnedModel(providerName, modelId, providerConfig)
    // A forward provider hands the caller's own credential upstream; the chat adapter
    // only ever sends provider.apiKey, so switching wires here would drop the auth.
    && !isCanonicalOpenAiForwardProvider(providerConfig)) {
    return selectWire(providerConfig, requested);
  }
  return selectWire(providerConfig, providerConfig.adapter);
}

/** Build the provider adapter for a resolved provider config. */
export function resolveAdapter(
  providerConfig: OcxProviderConfig,
  cacheRetention?: "none" | "short" | "long",
  providerId?: string,
) {
  return createRegisteredAdapter(providerConfig, { cacheRetention, ...(providerId ? { providerId } : {}) });
}
