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

const FALLBACK_PI_AGENT_DIR = join(homedir(), ".pi", "agent");

export function getScopedPiAgentDir(): string {
  const configuredAgentDir = process.env.PI_CODING_AGENT_DIR?.trim();
  if (configuredAgentDir) {
    return resolve(configuredAgentDir);
  }

  return FALLBACK_PI_AGENT_DIR;
}

export function getScopedMcpConfigPath(): string {
  return join(getScopedPiAgentDir(), "mcp.json");
}

export function getScopedMcpMetadataCachePath(): string {
  return join(getScopedPiAgentDir(), "mcp-cache.json");
}

export function getScopedMcpNpxCachePath(): string {
  return join(getScopedPiAgentDir(), "mcp-npx-cache.json");
}

export function getScopedMcpOAuthTokensPath(serverName: string): string {
  return join(getScopedPiAgentDir(), "mcp-oauth", serverName, "tokens.json");
}
