import { createHash, randomUUID } from "node:crypto";
import type { OcxProviderConfig } from "../types";
import { isEgressTransparentExecutor, markEgressTransparentExecutor } from "../lib/provider-egress";
import { isPassiveKeyTransparent, markPassiveKeyTransparent } from "../lib/passive-key-fetch-proof";
import { configuredOutboundFetch } from "../lib/proxy-env";
import { resolveGithubCopilotTransport } from "./github-copilot-transport";
import { resolveMistralTransport } from "./mistral-transport";

export const XAI_GROK_CLI_BASE_URL = "https://cli-chat-proxy.grok.com/v1";

/** Exact xAI Responses hosts: global and US public APIs, and the Grok CLI proxy. */
const XAI_RESPONSES_HOSTS = new Set(["api.x.ai", "us.api.x.ai", "cli-chat-proxy.grok.com"]);

/**
 * True when this provider's Responses traffic terminates at xAI itself.
 *
 * The global API and CLI proxy were probed 2026-08-22; the US regional API documents
 * the same request parameters. Matching is exact-host over
 * https, which keeps lookalikes (`api.x.ai.evil.test`) and nonstandard ports out.
 */
export function isXaiResponsesDestination(provider: Pick<OcxProviderConfig, "baseUrl">): boolean {
  try {
    const url = new URL(provider.baseUrl);
    return url.protocol === "https:"
      && XAI_RESPONSES_HOSTS.has(url.hostname.toLowerCase())
      && (url.port === "" || url.port === "443");
  } catch {
    return false;
  }
}

export const XAI_GROK_COMPATIBILITY = {
  version: "1.0.46",
  userAgent: "opencodex-grok/1.0.46",
  headers: {
    clientIdentifier: "x-grok-client-identifier",
    clientVersion: "x-grok-client-version",
    tokenAuth: "x-xai-token-auth",
    authenticateResponse: "x-authenticateresponse",
    conversationId: "x-grok-conv-id",
    requestId: "x-grok-req-id",
    sessionId: "x-grok-session-id",
    userAgent: "User-Agent",
  },
} as const;

export const XAI_GROK_CLIENT_VERSION = XAI_GROK_COMPATIBILITY.version;
export const XAI_CONV_ID_HEADER = XAI_GROK_COMPATIBILITY.headers.conversationId;

export type OcxProviderTransport = OcxProviderConfig & {
  /** Request executor used only at runtime; never persisted. */
  fetch?: typeof globalThis.fetch;
};

const XAI_GROK_CLI_HEADERS: Readonly<Record<string, string>> = {
  [XAI_GROK_COMPATIBILITY.headers.clientIdentifier]: "opencodex",
  [XAI_GROK_COMPATIBILITY.headers.clientVersion]: XAI_GROK_CLIENT_VERSION,
  [XAI_GROK_COMPATIBILITY.headers.tokenAuth]: "xai-grok-cli",
  [XAI_GROK_COMPATIBILITY.headers.authenticateResponse]: "authenticate-response",
};

function hasHeaderCaseInsensitive(
  headers: Record<string, string> | undefined,
  name: string,
): boolean {
  const target = name.toLowerCase();
  return Object.keys(headers ?? {}).some(key => key.toLowerCase() === target);
}

function withoutUserOverridden(
  defaults: Readonly<Record<string, string>>,
  userHeaders: Record<string, string> | undefined,
): Record<string, string> {
  return Object.fromEntries(
    Object.entries(defaults).filter(([name]) => !hasHeaderCaseInsensitive(userHeaders, name)),
  );
}

function withGeneratedRequestId(
  init: RequestInit | undefined,
  pinnedRequestId: string,
  stableHeaders: Readonly<Record<string, string>>,
): RequestInit {
  const headers = new Headers(init?.headers);
  for (const [name, value] of Object.entries(stableHeaders)) {
    if (!headers.has(name)) headers.set(name, value);
  }
  if (!headers.has(XAI_GROK_COMPATIBILITY.headers.requestId)) {
    headers.set(
      XAI_GROK_COMPATIBILITY.headers.requestId,
      pinnedRequestId,
    );
  }
  return { ...init, headers };
}

function findHeaderCaseInsensitive(
  headers: Record<string, string> | undefined,
  name: string,
): string | undefined {
  const target = name.toLowerCase();
  return Object.entries(headers ?? {}).find(([key]) => key.toLowerCase() === target)?.[1];
}

export function deriveXaiConvId(promptCacheKey: string): string {
  return createHash("sha256").update(promptCacheKey).digest("hex").slice(0, 32);
}

/**
 * Resolve xAI's runtime transport without mutating persisted config. Conversation/session
 * affinity is stable for this resolved transport; request identity is pinned per resolved
 * transport (= per logical request until key rotation), so same-target replays and transient
 * retries carry the same id while a rotated key gets a fresh one.
 * Agent, deployment, model-override, turn, mode, and user identity headers are intentionally
 * omitted because opencodex has no truthful values for the official fields.
 */
export function resolveProviderTransport(
  providerName: string,
  provider: OcxProviderTransport,
  promptCacheKey?: string,
  apiBaseUrl?: string,
): OcxProviderTransport {
  if (providerName === "github-copilot") {
    return resolveGithubCopilotTransport(provider, apiBaseUrl);
  }
  if (providerName !== "xai") return resolveMistralTransport(provider);

  const cacheKey = promptCacheKey?.trim();
  const affinity = cacheKey ? deriveXaiConvId(cacheKey) : undefined;
  const stableDefaults: Record<string, string> = {
    [XAI_GROK_COMPATIBILITY.headers.userAgent]: XAI_GROK_COMPATIBILITY.userAgent,
    ...(affinity
      ? {
          [XAI_GROK_COMPATIBILITY.headers.conversationId]: affinity,
          [XAI_GROK_COMPATIBILITY.headers.sessionId]: affinity,
        }
      : {}),
    ...(provider.authMode === "oauth" ? XAI_GROK_CLI_HEADERS : {}),
  };
  const stableHeaders = {
    ...withoutUserOverridden(stableDefaults, provider.headers),
    ...(provider.headers ?? {}),
  };
  // Keep API-key provider metadata compatible with key-pool rotation; session/UA defaults
  // remain transport-scoped and are applied by the wrapper immediately below.
  const headers = provider.authMode === "oauth"
    ? stableHeaders
    : {
        ...(affinity && !hasHeaderCaseInsensitive(provider.headers, XAI_GROK_COMPATIBILITY.headers.conversationId)
          ? { [XAI_GROK_COMPATIBILITY.headers.conversationId]: affinity }
          : {}),
        ...(provider.headers ?? {}),
      };
  const configuredRequestId = findHeaderCaseInsensitive(
    provider.headers,
    XAI_GROK_COMPATIBILITY.headers.requestId,
  );
  // Pin the request id per resolved transport (= per logical request until key rotation):
  // same-target 429 replays must carry the SAME x-grok-req-id as the original dispatch, and
  // transient retries reuse one id so the upstream can dedupe them. A rotated key resolves a
  // fresh transport, which gets its own id.
  const requestId = configuredRequestId ?? randomUUID();
  // Without a configured executor the default routes through `configuredOutboundFetch` rather
  // than the bare global fetch, so a per-provider SOCKS5 route reaches the SOCKS transport.
  // Bare Bun fetch ignores a socks5 value, which would have sent the request unproxied while
  // the configuration named a proxy.
  const baseFetch = provider.fetch
    ?? markEgressTransparentExecutor(((input, init) => configuredOutboundFetch(input, init)) as typeof globalThis.fetch);
  if (!provider.fetch) markPassiveKeyTransparent(baseFetch);
  const attemptFetch = ((input, init) =>
    baseFetch(input, withGeneratedRequestId(init, requestId, stableHeaders))) as typeof globalThis.fetch;
  // This wrapper only adds a header and forwards the init, so it carries a request-scoped proxy
  // option through to whatever it wraps — but only if what it wraps carries it too. A
  // configured executor owns its own routing and is not assumed to.
  if (isEgressTransparentExecutor(baseFetch)) markEgressTransparentExecutor(attemptFetch);
  if (isPassiveKeyTransparent(baseFetch) && !Object.keys(stableHeaders).some(name => ["authorization", "x-api-key", "api-key"].includes(name.toLowerCase()))) markPassiveKeyTransparent(attemptFetch);

  return {
    ...provider,
    ...(provider.authMode === "oauth" ? { baseUrl: XAI_GROK_CLI_BASE_URL } : {}),
    headers,
    fetch: attemptFetch,
  };
}
