import { configureSocks5Fetch, socks5ProxyFromEnv } from "../lib/proxy-env";
import { redactUrlForLog } from "../lib/redact";
import { join } from "node:path";
import { DEFAULT_SUBAGENT_MODELS, SUBAGENT_MODELS_VERSION } from "./subagent-models";
import { MULTI_AGENT_SURFACE_ADVISORY_VERSION } from "./multi-agent-surface";
import { DEFAULT_APP_OWNED_MEMORY_BUDGET_BYTES } from "../lib/app-owned-memory";
import { describeProxyForLog, readWindowsSystemProxy, type WindowsProxyRegistryReader } from "../lib/windows-system-proxy";
import { readMacOSSystemProxy, type MacOSProxyReader, type MacOSSystemProxyResult } from "./macos-system-proxy";
import { OPENAI_PROVIDER_TIER_VERSION, type OcxConfig } from "../types";
import type { OcxRuntimeRole } from "../types/config";

export function codexAutoStartEnabled(config: Pick<OcxConfig, "codexAutoStart">): boolean {
  return config.codexAutoStart !== false;
}

export const CODEX_SHIM_AUTO_RESTORE_ENV = "OPENCODEX_CODEX_SHIM_AUTO_RESTORE";

export function codexShimAutoRestoreEnabled(
  config: Pick<OcxConfig, "codexShimAutoRestore">,
  env: NodeJS.ProcessEnv = process.env,
): boolean {
  return config.codexShimAutoRestore !== false && env[CODEX_SHIM_AUTO_RESTORE_ENV] !== "0";
}

export function multiAgentGuidanceEnabled(
  config: Pick<OcxConfig, "multiAgentGuidanceEnabled">,
): boolean {
  return config.multiAgentGuidanceEnabled !== false;
}

export function runtimeRole(config: Pick<OcxConfig, "runtimeRole">): OcxRuntimeRole {
  return config.runtimeRole ?? "standalone";
}

export function getDefaultConfig(): OcxConfig {
  // Fresh-install default: works out of the box with Codex's ChatGPT OAuth (no API key).
  // gpt-* requests forward the caller's incoming OAuth headers to the ChatGPT backend.
  // Adding extra providers (e.g. opencode-go) and switching defaultProvider is a user/runtime choice.
  return {
    port: 10100,
    emptyCompletionRetry: false,
    dropCodexSafetyBuffering: false,
    fastRows: true,
    managementUsageMaxReadBytes: 64 * 1024 * 1024,
    appOwnedMemoryBudgetMb: DEFAULT_APP_OWNED_MEMORY_BUDGET_BYTES / (1024 * 1024),
    // Fresh/re-initialized configs are already written in the current three-tier
    // OpenAI shape. Mark them as such so startup does not mistake them for a
    // legacy config and collide with an immutable backup from an earlier setup.
    openaiProviderTierVersion: OPENAI_PROVIDER_TIER_VERSION,
    providers: {
      openai: {
        adapter: "openai-responses",
        baseUrl: "https://chatgpt.com/backend-api/codex",
        authMode: "forward",
        codexAccountMode: "pool",
      },
    },
    defaultProvider: "openai",
    subagentModels: [...DEFAULT_SUBAGENT_MODELS],
    subagentModelsVersion: SUBAGENT_MODELS_VERSION,
    // v1 is the shipped surface while a v2 native-to-routed task is undeliverable
    // ciphertext. Written explicitly rather than left absent, because an absent key
    // means base everywhere else. A fresh install starts already acknowledged: there is
    // nothing to advise an operator who is on the recommended surface.
    multiAgentMode: "v1",
    multiAgentSurfaceAdvisoryVersion: MULTI_AGENT_SURFACE_ADVISORY_VERSION,
    multiAgentGuidanceEnabled: true,
    websockets: false,
    codexAutoStart: true,
    codexShimAutoRestore: true,
  };
}

export function resolveEnvValue(value: string | undefined): string | undefined {
  if (!value) return undefined;
  const match = value.match(/^\$\{(\w+)\}$/);
  if (match) return process.env[match[1]];
  if (value.startsWith("$")) return process.env[value.slice(1)];
  return value;
}

const warnedProxyConfigDiscards = new Set<"proxy" | "noProxy" | "noProxyElements">();

function warnProxyConfigDiscardOnce(kind: "proxy" | "noProxy" | "noProxyElements"): void {
  if (warnedProxyConfigDiscards.has(kind)) return;
  warnedProxyConfigDiscards.add(kind);
  if (kind === "proxy") {
    console.warn(
      "⚠️  config.json proxy was discarded because it is not a non-empty resolved string — configured proxy routing is disabled; existing proxy environment variables remain authoritative, otherwise outbound requests use direct egress",
    );
  } else if (kind === "noProxy") {
    console.warn(
      "⚠️  config.json noProxy was discarded because it is not a string, string array, or resolved environment reference — existing NO_PROXY and loopback bypasses remain",
    );
  } else {
    console.warn(
      "⚠️  config.json noProxy contains invalid elements — invalid elements were ignored; valid entries, existing NO_PROXY, and loopback bypasses remain",
    );
  }
}

const LOOPBACK_NO_PROXY = ["localhost", "127.0.0.1", "::1", "[::1]"] as const;
const LOOPBACK_ADDRESS_NO_PROXY = ["127.0.0.1", "::1", "[::1]"] as const;

// With no config.proxy, which loopback bypasses are written depends on who reads them. The
// installed SOCKS fetch wrapper (src/lib/proxy-env.ts configuredOutboundFetch) matches these
// entries as exact hosts. Bun applies an inherited HTTP(S) proxy itself and matches NO_PROXY
// entries as domain suffixes, so a bare "localhost" there would also send any *.localhost name
// direct, including from a fetch that never passes the wrapper. The full list is therefore
// written only when an inherited SOCKS proxy is the only one. Whenever Bun applies an inherited
// HTTP(S) proxy, only the loopback addresses are added: they cannot widen that way (a URL host
// ending in a numeric label parses as IPv4) and keep local health and management calls to
// 127.0.0.1 off the proxy. A proxy-free process is left untouched: writing NO_PROXY into it is
// itself a proxy-env mutation callers observe (the lab sandbox rejects these keys).
function inheritedLoopbackBypass(): readonly string[] | undefined {
  const schemeProxy = ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy"].some(key => process.env[key]?.trim());
  const httpAllProxy = ["ALL_PROXY", "all_proxy"].some(key => {
    const value = process.env[key]?.trim();
    if (!value) return false;
    try {
      return ["http:", "https:"].includes(new URL(value).protocol);
    } catch {
      return false;
    }
  });
  if (schemeProxy || httpAllProxy) return LOOPBACK_ADDRESS_NO_PROXY;
  return socks5ProxyFromEnv() !== undefined ? LOOPBACK_NO_PROXY : undefined;
}

function withNoProxyEntries(existing: string, configured: readonly string[], loopback: readonly string[]): string {
  const entries = existing.split(",").map(s => s.trim()).filter(Boolean);
  const seen = new Set(entries.map(entry => entry.toLowerCase()));
  for (const host of [...configured, ...loopback]) {
    const key = host.toLowerCase();
    if (!seen.has(key)) {
      entries.push(host);
      seen.add(key);
    }
  }
  return entries.join(",");
}

function mergeNoProxyEntries(configured: readonly string[] = [], loopback: readonly string[] = LOOPBACK_NO_PROXY): void {
  process.env.NO_PROXY = withNoProxyEntries(process.env.NO_PROXY ?? process.env.no_proxy ?? "", configured, loopback);
  // Bun's native fetch reads a non-empty lowercase no_proxy before NO_PROXY
  // (src/codex/catalog/remote.ts), so an inherited one would shadow the loopback entries above.
  // Only the loopback addresses join it: Bun matches entries as domain suffixes, and any name
  // (a bare "localhost", or a configured noProxy entry the inherited value always shadowed)
  // would send its subdomains past a proxy the inherited value kept them on.
  const inherited = process.env.no_proxy;
  if (inherited !== undefined && inherited.trim() !== "") {
    process.env.no_proxy = withNoProxyEntries(inherited, [], loopback.filter(host => host !== "localhost"));
  }
}

function configuredNoProxyEntries(config: OcxConfig): string[] {
  const raw = config.noProxy;
  let entries: string[];
  if (Array.isArray(raw)) {
    if (raw.some(entry => typeof entry !== "string")) warnProxyConfigDiscardOnce("noProxyElements");
    entries = raw.filter((entry): entry is string => typeof entry === "string");
  } else if (typeof raw === "string") {
    const resolved = resolveEnvValue(raw);
    if (raw && resolved === undefined) warnProxyConfigDiscardOnce("noProxy");
    entries = (resolved ?? "").split(",");
  } else {
    if (raw !== undefined) warnProxyConfigDiscardOnce("noProxy");
    entries = [];
  }
  return entries.map(entry => entry.trim()).filter(Boolean);
}

/**
 * Mirror `config.proxy` into HTTP(S)_PROXY env vars. Bun fetch consumes them natively; transports
 * such as the ChatGPT upstream WebSocket select the same environment explicitly. User-set HTTP(S)_PROXY
 * variables win; config fills missing scheme proxies, which take precedence over ALL_PROXY for WS.
 * localhost/127.0.0.1 are appended to NO_PROXY so the CLI's own health checks and
 * running-proxy API calls stay direct. Call once per process entry that makes outbound provider
 * requests (server start, catalog sync). `announce` prints the one-line startup banner naming
 * the outbound proxy actually in effect; only server start passes it, because on catalog sync
 * that line is noise. The URL is redacted because a proxy URL can carry credentials.
 */
export function applyProxyEnv(config: OcxConfig, announce = false): void {
  applyProxyEnvWith(config);
  if (!announce) return;
  const outbound = process.env.ALL_PROXY?.trim() || process.env.HTTPS_PROXY?.trim() || process.env.HTTP_PROXY?.trim();
  if (outbound) console.log(`   outbound proxy: ${redactUrlForLog(outbound)}`);
}

/**
 * Builds the refusal reason for an unsafe macOS discovery outcome. Toggle refusals
 * name the toggle only — they are not an exception-translation problem, so the
 * "exceptions" framing is reserved for entries. Entry refusals report shape counts
 * and setting names, never the entries themselves: naming them would leak
 * bypass-list hostnames (internal servers, banked domains) into shared logs.
 */
function describeDiscoveryRefusal(
  found: Extract<MacOSSystemProxyResult, { kind: "unsafe-exceptions" }>,
): string {
  const counts = found.unrepresentable;
  if (counts) {
    const parts = [
      counts.cidr > 0 && `${counts.cidr} CIDR`,
      counts.hostname > 0 && `${counts.hostname} bare-hostname`,
      counts.wildcard > 0 && `${counts.wildcard} wildcard-shaped`,
      counts.other > 0 && `${counts.other} other`,
    ].filter(Boolean).join(", ");
    const entries = counts.cidr + counts.hostname + counts.wildcard + counts.other;
    const entriesPhrase = `${entries} ${entries === 1 ? "exception entry" : "exception entries"} cannot be safely translated (${parts})`;
    return found.setting
      ? `${found.setting} is enabled; ${entriesPhrase}; discovery refused`
      : `macOS exceptions cannot be safely translated (${parts}); discovery refused`;
  }
  if (found.setting) return `${found.setting} is enabled; discovery refused`;
  return "macOS exceptions cannot be safely translated; discovery refused";
}

/** Test seam for `proxy: "auto"`: the registry reader and platform are injectable. */
export function applyProxyEnvWith(
  config: OcxConfig,
  auto: { reader?: WindowsProxyRegistryReader; macOSReader?: MacOSProxyReader; platform?: NodeJS.Platform } = {},
): void {
  // `proxy` and `noProxy` are not declared in the top-level schema, which ends in
  // `.passthrough()`, so whatever is on disk arrives here verbatim. A non-string value
  // reached string-only methods and threw out of this function, and it runs once per
  // process entry point — the failure was a startup crash, not a degraded proxy. Ignore
  // malformed values with a privacy-safe warning instead: they cannot express a routing
  // intent, and refusing to start is a worse answer than starting without them.
  const rawProxy = config.proxy;
  let proxy = typeof rawProxy === "string" ? resolveEnvValue(rawProxy) : undefined;
  if (!proxy) {
    if (rawProxy !== undefined) warnProxyConfigDiscardOnce("proxy");
    // Inherited-SOCKS path: only loopback bypasses are appended. A configured noProxy is
    // deliberately NOT merged here — with no config.proxy the operator's bypass list has
    // no declared proxy to apply against, and merging it would silently widen direct
    // egress beyond the loopback fix this branch exists for.
    const loopback = inheritedLoopbackBypass();
    if (loopback) mergeNoProxyEntries([], loopback);
    configureSocks5Fetch();
    return;
  }
  if (proxy.trim().toLowerCase() === "auto") {
    if ((auto.platform ?? process.platform) === "darwin") {
      // An inherited scheme or ALL_PROXY route owns both its proxy and bypass
      // variables. Combining it with system exceptions would change that route.
      if (["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "ALL_PROXY", "all_proxy"]
        .some(key => process.env[key]?.trim())) {
        console.log('[opencodex] proxy "auto": existing proxy environment wins; macOS system proxy not consulted');
        configureSocks5Fetch();
        return;
      }
      const found = readMacOSSystemProxy(auto.macOSReader);
      if (found.kind !== "proxy") {
        const reason = found.kind === "unsafe-exceptions"
          ? describeDiscoveryRefusal(found)
          : found.kind === "socks-only"
            ? "macOS system proxy is SOCKS-only, which HTTP_PROXY cannot express; using direct egress"
            : found.kind === "disabled"
              ? "macOS system proxy is disabled"
              : "macOS proxy settings could not be read";
        console.log(`[opencodex] proxy "auto": ${reason}; proxy environment unchanged`);
        return;
      }
      const configured = configuredNoProxyEntries(config);
      const inheritedLowercase = process.env.no_proxy?.trim();
      if (inheritedLowercase && configured.some(host => /^localhost\.?$/i.test(host))) {
        console.log('[opencodex] proxy "auto": configured noProxy "localhost" cannot be represented exactly for Bun while an inherited no_proxy is set; discovery refused');
        return;
      }
      const origins = [
        found.httpUrl && `HTTP ${describeProxyForLog(found.httpUrl)}`,
        found.httpsUrl && `HTTPS ${describeProxyForLog(found.httpsUrl)}`,
      ].filter(Boolean).join(", ");
      console.log(`[opencodex] proxy "auto": using macOS system proxy ${origins}`);
      if (found.droppedLinkLocal) {
        console.log('[opencodex] proxy "auto": link-local IP literals use the proxy; macOS link-local range exceptions are not expressible');
      }
      if (found.httpUrl) process.env.HTTP_PROXY = found.httpUrl;
      if (found.httpsUrl) process.env.HTTPS_PROXY = found.httpsUrl;
      // Bun gives non-empty lowercase no_proxy priority over NO_PROXY. Before
      // discovery there was no proxy, so an ordinary configured name and its
      // subdomains can stay direct in both paths. Bare localhost is refused
      // above when lowercase is inherited: Bun cannot match it exactly there.
      mergeNoProxyEntries([...configured, ...found.exceptions], LOOPBACK_ADDRESS_NO_PROXY);
      if (process.env.no_proxy?.trim()) {
        process.env.no_proxy = withNoProxyEntries(process.env.no_proxy, [...configured, ...found.exceptions], LOOPBACK_ADDRESS_NO_PROXY);
      }
      configureSocks5Fetch();
      return;
    }
    // #1525 slice 1: one startup read of the Windows static proxy. Never copy the literal
    // "auto" into HTTP_PROXY; every non-proxy outcome leaves outbound routing as it was.
    if (process.env.HTTP_PROXY?.trim() || process.env.http_proxy?.trim()
      || process.env.HTTPS_PROXY?.trim() || process.env.https_proxy?.trim()) {
      console.log("[opencodex] proxy \"auto\": existing HTTP_PROXY/HTTPS_PROXY environment wins; system proxy not consulted");
      proxy = undefined;
    } else {
      const found = readWindowsSystemProxy(auto.reader, auto.platform);
      if (found.kind === "proxy") {
        const origins = [
          found.httpUrl && `HTTP ${describeProxyForLog(found.httpUrl)}`,
          found.httpsUrl && `HTTPS ${describeProxyForLog(found.httpsUrl)}`,
        ].filter(Boolean).join(", ");
        console.log(`[opencodex] proxy "auto": using Windows system proxy ${origins}`);
        if (found.httpUrl) process.env.HTTP_PROXY = found.httpUrl;
        if (found.httpsUrl) process.env.HTTPS_PROXY = found.httpsUrl;
        proxy = undefined;
      } else {
        const reason = found.kind === "unsupported"
          ? "only Windows and macOS system proxy discovery is supported; using direct egress on this OS"
          : found.kind === "disabled"
            ? "Windows system proxy is disabled; using direct egress"
            : found.kind === "socks-only"
              ? "Windows system proxy is SOCKS-only, which HTTP_PROXY cannot express; using direct egress"
              : "Windows proxy settings could not be read; using direct egress";
        console.log(`[opencodex] proxy "auto": ${reason}`);
        proxy = undefined;
      }
    }
  }
  if (proxy) {
    if (/^socks/i.test(proxy.trim()) && !/^socks5h?:\/\//i.test(proxy.trim())) {
      throw new Error("Only SOCKS5 proxy URLs are supported; use socks5://host:port");
    }
    if (/^socks5h?:\/\//i.test(proxy.trim())) {
      for (const key of ["HTTP_PROXY", "HTTPS_PROXY", "http_proxy", "https_proxy", "ALL_PROXY", "all_proxy"] as const) {
        delete process.env[key];
      }
      process.env.ALL_PROXY = proxy;
    } else {
      if (!process.env.HTTP_PROXY?.trim() && !process.env.http_proxy?.trim()) process.env.HTTP_PROXY = proxy;
      if (!process.env.HTTPS_PROXY?.trim() && !process.env.https_proxy?.trim()) process.env.HTTPS_PROXY = proxy;
    }
  }
  // Configured entries first, then loopback: loopback is unconditional, so appending it last
  // keeps it present even when the operator lists a loopback host themselves.
  mergeNoProxyEntries(configuredNoProxyEntries(config));
  configureSocks5Fetch();
}
