import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";

export const defaultAgentDirectory = dirname(fileURLToPath(import.meta.url));

export function resolveAgentRuntimePaths({
  env = process.env,
  agentDirectory = defaultAgentDirectory,
} = {}) {
  const configuredDataDirectory = String(env.PRIVATE_REEL_DATA_DIR || "").trim();
  const dataDirectory = configuredDataDirectory
    ? resolve(configuredDataDirectory)
    : agentDirectory;
  const configuredConfigPath = String(env.PRIVATE_REEL_CONFIG || "").trim();

  return {
    agentDirectory,
    dataDirectory,
    cacheDirectory: join(dataDirectory, "cache"),
    configPath: configuredConfigPath || join(dataDirectory, "gallery.config.json"),
    tailscaleCLI: String(env.PRIVATE_REEL_TAILSCALE_CLI || "").trim() || "tailscale",
  };
}
