// Pi Worker Configuration
// Loads and validates environment variables

export interface Config {
  // Asana
  asanaAccessToken: string;
  asanaProjectGid: string;
  asanaWorkspaceGid: string;
  
  // Pi Agent
  piBin: string;
  piModel: string;
  anthropicApiKey: string;
  
  // Paths
  targetProjectPath: string;
  extensionsPath: string;
  agentsPath: string;
  logDir: string;
  projectsPath: string;
  
  // Timing
  pollIntervalSec: number;
  improvementIntervalSec: number;
  healthPort: number;
}

function requireEnv(key: string): string {
  const value = process.env[key];
  if (!value) {
    throw new Error(`Missing required environment variable: ${key}`);
  }
  return value;
}

export function loadConfig(): Config {
  return {
    asanaAccessToken: requireEnv("ASANA_ACCESS_TOKEN"),
    asanaProjectGid: process.env.ASANA_PROJECT_GID || "",
    asanaWorkspaceGid: process.env.ASANA_WORKSPACE_GID || "",
    
    piBin: process.env.PI_BIN || "pi",
    piModel: process.env.PI_MODEL || "claude-sonnet-4-6",
    anthropicApiKey: requireEnv("ANTHROPIC_API_KEY"),
    
    targetProjectPath: process.env.TARGET_PROJECT_PATH || process.cwd(),
    extensionsPath: process.env.EXTENSIONS_PATH || "../extensions",
    agentsPath: process.env.AGENTS_PATH || "../.pi/agents",
    logDir: process.env.LOG_DIR || "./logs",
    projectsPath: process.env.PROJECTS_PATH || "./projects",
    
    pollIntervalSec: parseInt(process.env.POLL_INTERVAL_SEC || "120"),
    improvementIntervalSec: parseInt(process.env.IMPROVEMENT_INTERVAL_SEC || "3600"),
    healthPort: parseInt(process.env.HEALTH_PORT || "8787"),
  };
}
