/**
 * Centralized API Configuration
 *
 * This module provides a single source of truth for all API endpoints and URLs.
 * All components should import from here instead of reading environment variables directly.
 *
 * Environment Variables:
 * - NEXT_PUBLIC_API_URL: The main API server URL (default: http://localhost:3001)
 * - NEXT_PUBLIC_ORCHESTRATOR_URL: The orchestrator service URL (default: same as API URL)
 */

/**
 * Default API server URL for local development
 */
const DEFAULT_API_URL = "http://localhost:3001";

/**
 * Main API server URL
 * Used for authentication, tasks, events, knowledge, and all core API calls
 */
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL ?? DEFAULT_API_URL;

/**
 * Orchestrator service URL
 * Used for agent management, task progress, and orchestration features
 * Falls back to main API URL if not specified (they may run on the same server)
 */
export const ORCHESTRATOR_URL = process.env.NEXT_PUBLIC_ORCHESTRATOR_URL ?? API_BASE_URL;

/**
 * Build a full API endpoint URL
 * @param endpoint - The API endpoint path (should start with /)
 * @returns The full URL for the endpoint
 */
export function buildApiUrl(endpoint: string): string {
  return `${API_BASE_URL}${endpoint}`;
}

/**
 * Build a full orchestrator endpoint URL
 * @param endpoint - The orchestrator endpoint path (should start with /)
 * @returns The full URL for the endpoint
 */
export function buildOrchestratorUrl(endpoint: string): string {
  return `${ORCHESTRATOR_URL}${endpoint}`;
}

/**
 * Configuration object for convenient access to all URLs
 */
export const apiConfig = {
  /** Main API base URL */
  baseUrl: API_BASE_URL,
  /** Orchestrator service URL */
  orchestratorUrl: ORCHESTRATOR_URL,
  /** Build full API URL for an endpoint */
  buildUrl: buildApiUrl,
  /** Build full orchestrator URL for an endpoint */
  buildOrchestratorUrl,
} as const;
