import { registerAs } from "@nestjs/config";

export const orchestratorConfig = registerAs("orchestrator", () => ({
  host: process.env.HOST ?? process.env.BIND_ADDRESS ?? "127.0.0.1",
  port: parseInt(process.env.ORCHESTRATOR_PORT ?? "3001", 10),
  valkey: {
    host: process.env.VALKEY_HOST ?? "localhost",
    port: parseInt(process.env.VALKEY_PORT ?? "6379", 10),
    password: process.env.VALKEY_PASSWORD,
    url: process.env.VALKEY_URL ?? "redis://localhost:6379",
    connectTimeout: parseInt(process.env.VALKEY_CONNECT_TIMEOUT_MS ?? "5000", 10),
    commandTimeout: parseInt(process.env.VALKEY_COMMAND_TIMEOUT_MS ?? "3000", 10),
  },
  claude: {
    apiKey: process.env.CLAUDE_API_KEY,
  },
  docker: {
    socketPath: process.env.DOCKER_SOCKET ?? "/var/run/docker.sock",
  },
  git: {
    userName: process.env.GIT_USER_NAME ?? "Mosaic Orchestrator",
    userEmail: process.env.GIT_USER_EMAIL ?? "orchestrator@mosaicstack.dev",
  },
  killswitch: {
    enabled: process.env.KILLSWITCH_ENABLED === "true",
  },
  sandbox: {
    enabled: process.env.SANDBOX_ENABLED !== "false",
    defaultImage: process.env.SANDBOX_DEFAULT_IMAGE ?? "node:20-alpine",
    defaultMemoryMB: parseInt(process.env.SANDBOX_DEFAULT_MEMORY_MB ?? "512", 10),
    defaultCpuLimit: parseFloat(process.env.SANDBOX_DEFAULT_CPU_LIMIT ?? "1.0"),
    networkMode: process.env.SANDBOX_NETWORK_MODE ?? "bridge",
  },
  coordinator: {
    url: process.env.COORDINATOR_URL ?? "http://localhost:8000",
    timeout: parseInt(process.env.COORDINATOR_TIMEOUT_MS ?? "30000", 10),
    retries: parseInt(process.env.COORDINATOR_RETRIES ?? "3", 10),
    apiKey: process.env.COORDINATOR_API_KEY,
  },
  yolo: {
    enabled: process.env.YOLO_MODE === "true",
  },
  spawner: {
    maxConcurrentAgents: parseInt(process.env.MAX_CONCURRENT_AGENTS ?? "20", 10),
  },
  queue: {
    completedRetentionCount: parseInt(process.env.QUEUE_COMPLETED_RETENTION_COUNT ?? "100", 10),
    completedRetentionAgeSeconds: parseInt(
      process.env.QUEUE_COMPLETED_RETENTION_AGE_S ?? "3600",
      10
    ),
    failedRetentionCount: parseInt(process.env.QUEUE_FAILED_RETENTION_COUNT ?? "1000", 10),
    failedRetentionAgeSeconds: parseInt(process.env.QUEUE_FAILED_RETENTION_AGE_S ?? "86400", 10),
  },
}));
