/**
 * The shape of a service definition: the shell command both Unix backends bake into
 * their unit/plist, the port they pin into it, and the proxy environment it carries.
 *
 * Leaf module. It reads config and the token-file path, and asks nothing of the
 * backends that consume it — importing it from `launchd.ts`/`systemd.ts` cannot
 * route back through them (see `installedServiceListenPort` in `health.ts`, which
 * intentionally stays behind because it asks every backend).
 */
import { basename, join } from "node:path";
import { getConfigDir, loadConfig } from "../config";
import { readClaudeInterceptProxyToken } from "../claude/intercept/proxy-auth";
import { serviceApiTokenFilePath } from "../lib/service-secrets";
import { PROXY_ENV_KEYS } from "../lib/proxy-env";

/**
 * The service token file under one OpenCodex home, for a definition that pins that home.
 * The file name comes from {@link serviceApiTokenFilePath}, which stays its only owner.
 */
export function serviceApiTokenFileUnder(opencodexHome: string): string {
  return join(opencodexHome, basename(serviceApiTokenFilePath()));
}

export function shellQuote(value: string): string {
  return `'${value.replace(/'/g, "'\\''")}'`;
}

/**
 * Assign the resolved service homes AFTER the login shell initializes, with shell-safe quoting.
 *
 * launchd and systemd run the service command through `/bin/sh -lc`. A login profile can
 * export CODEX_HOME/OPENCODEX_HOME after the manager supplied the definition environment, and
 * the shell assignment that runs last wins. Pinning only `<key>/Environment` therefore cannot
 * guarantee the proxy sees the home the service install recorded. The command assigns both
 * homes immediately before `exec`, so a profile export cannot override the pinned values.
 */
export function pinnedServiceHomeShellAssignments(
  codexHome: string,
  opencodexHome: string,
): string {
  return `CODEX_HOME=${shellQuote(codexHome)} OPENCODEX_HOME=${shellQuote(opencodexHome)} export CODEX_HOME OPENCODEX_HOME;`;
}

/**
 * Listen port baked into service wrappers / WinSW XML.
 * Priority: explicit override → OCX_BAKE_PORT (update restart) → config.port → 10100.
 * `config.port === 0` means ephemeral for interactive start; services need a stable pin,
 * so treat 0 / invalid like unset (default 10100) instead of baking `--port 0`.
 */
export function resolveServiceListenPort(override?: number): number {
  if (typeof override === "number" && Number.isFinite(override) && override > 0 && override <= 65535) {
    return Math.trunc(override);
  }
  const baked = process.env.OCX_BAKE_PORT?.trim();
  if (baked && /^\d+$/.test(baked)) {
    const n = Number(baked);
    if (n > 0 && n <= 65535) return n;
  }
  const configured = loadConfig().port;
  if (typeof configured === "number" && configured > 0 && configured <= 65535) return configured;
  return 10100;
}

export function buildServiceShellCommand(
  bun: string,
  cli: string | null,
  port = resolveServiceListenPort(),
  tokenFile = serviceApiTokenFilePath(),
): string {
  const args = cli ? `${shellQuote(cli)} start` : "start";
  return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(bun)} ${args} --port ${port}`;
}

/**
 * The same command shape, launched through a stable `ocx` executable instead of an
 * explicit Bun + CLI pair. The token-file preamble is identical and deliberately shared
 * in form: the service still reads the token from disk at start and never carries it in
 * the unit.
 */
export function buildServiceLauncherShellCommand(
  launcher: string,
  port = resolveServiceListenPort(),
  tokenFile = serviceApiTokenFilePath(),
): string {
  return `if [ -f ${shellQuote(tokenFile)} ]; then OPENCODEX_API_AUTH_TOKEN="$(cat ${shellQuote(tokenFile)})"; export OPENCODEX_API_AUTH_TOKEN; fi; exec ${shellQuote(launcher)} start --port ${port}`;
}

/**
 * Shared tail parser for the baked `--port <n>`.
 *
 * Terminators cover all three artifact shapes: whitespace (batch wrapper, systemd
 * unit), `"` (systemd's quoted ExecStart), `<` (WinSW's `</arguments>`), and `&` (an
 * XML-escaped quote). Matched LAST because every artifact carries the Bun and CLI
 * paths ahead of the argument, and a path containing the literal must not shadow it.
 */
export function parseBakedListenPort(read: () => string): number | null {
  try {
    const last = [...read().matchAll(/start --port (\d{1,5})(?:\s|"|&|<|$)/gm)].at(-1);
    if (!last) return null;
    const n = Number(last[1]);
    return n > 0 && n <= 65535 ? n : null;
  } catch {
    return null;
  }
}

/**
 * Outbound proxy settings the installing shell had, resolved for baking into a service
 * definition.
 *
 * A service manager does not inherit the environment of the shell that installed it, and
 * `ExecStart=/bin/sh -lc` is dash on Ubuntu/WSL — login dash reads `.profile`, not
 * `.bashrc`, which is where proxy exports usually live. So a user who needs a proxy to
 * reach the upstream got a service that dialed direct: the socket was reset, the retry
 * budget drained, and the request surfaced as `502 Provider unreachable` (#2107). The
 * same install driven through `ocx codex-shim` worked, because that path spawns with
 * `{ ...process.env }`.
 *
 * Lower-case variants are honored because curl-style tooling sets them and the runtime's
 * own `applyProxyEnv` already treats both cases as equivalent. Only the canonical
 * upper-case name is baked, so a definition never carries two spellings of one setting.
 */
export function resolvedProxyEnv(
  env: NodeJS.ProcessEnv = process.env,
  ownClaudeInterceptProxy?: string | null,
): { name: string; value: string }[] {
  const token = ownClaudeInterceptProxy === undefined ? readClaudeInterceptProxyToken(getConfigDir()) : null;
  const resolved: { name: string; value: string }[] = [];
  for (const key of PROXY_ENV_KEYS) {
    const value = [env[key]?.trim(), env[key.toLowerCase()]?.trim()].find(v => v && (
      ownClaudeInterceptProxy === undefined ? !isOwnedClaudeInterceptProxy(v, token) : v !== ownClaudeInterceptProxy
    ));
    if (value) resolved.push({ name: key, value });
  }
  return resolved;
}

/**
 * Match this installation's credential and exact URL components without creating a token.
 * An inherited URL can still use an old bound port after the persisted config changes.
 * Shape alone cannot establish ownership: a user proxy may also use an `opencodex` username.
 */
function isOwnedClaudeInterceptProxy(value: string, token: string | null): boolean {
  if (!token) return false;
  const prefix = `http://opencodex:${encodeURIComponent(token)}@127.0.0.1:`;
  if (!value.startsWith(prefix)) return false;
  const port = value.slice(prefix.length).replace(/\/$/, "");
  return /^\d{1,5}$/.test(port) && Number(port) > 0 && Number(port) <= 65535;
}
