import { homedir } from "node:os";
import { join, resolve } from "node:path";

export const DEFAULT_ZENCODE_CODEX_HOME = join(
  homedir(),
  ".zencode",
  "codex-home",
);

export function getLegacyCodexHome(): string {
  return join(homedir(), ".codex");
}

export function resolveZencodeCodexHome(explicitCodexHome?: string): string {
  // Intentionally do NOT fall back to process.env.CODEX_HOME here.
  // That env var points to the user's global Codex home (~/.codex), which
  // would break per-session isolation by nesting session dirs inside it.
  return resolve(explicitCodexHome || DEFAULT_ZENCODE_CODEX_HOME);
}

export function resolveZencodeCodexSessionHome(
  sessionId: string,
  explicitCodexHome?: string,
): string {
  return join(resolveZencodeCodexHome(explicitCodexHome), sessionId);
}
