/**
 * Configuración centralizada para Trading Platform Trading Platform
 *
 * Este archivo define todas las URLs, puertos y configuraciones
 * que deben ser compartidas entre servicios.
 */

// ============================================================================
// Service URLs
// ============================================================================

export const SERVICES = {
  BACKEND: {
    name: 'backend',
    port: 3000,
    url: process.env.BACKEND_URL || 'http://localhost:3000',
    healthPath: '/health',
  },
  ML_ENGINE: {
    name: 'ml-engine',
    port: 8001,
    url: process.env.ML_ENGINE_URL || 'http://localhost:8001',
    healthPath: '/health',
  },
  DATA_SERVICE: {
    name: 'data-service',
    port: 8002,
    url: process.env.DATA_SERVICE_URL || 'http://localhost:8002',
    healthPath: '/health',
  },
  LLM_AGENT: {
    name: 'llm-agent',
    port: 8003,
    url: process.env.LLM_AGENT_URL || 'http://localhost:8003',
    healthPath: '/health',
  },
  TRADING_AGENTS: {
    name: 'trading-agents',
    port: 8004,
    url: process.env.TRADING_AGENTS_URL || 'http://localhost:8004',
    healthPath: '/health',
  },
  FRONTEND: {
    name: 'frontend',
    port: 5173,
    url: process.env.FRONTEND_URL || 'http://localhost:5173',
  },
} as const;

// ============================================================================
// Database
// ============================================================================

export const DATABASE = {
  host: process.env.DB_HOST || 'localhost',
  port: parseInt(process.env.DB_PORT || '5432'),
  name: process.env.DB_NAME || 'trading',
  user: process.env.DB_USER || 'trading',
  password: process.env.DB_PASSWORD || '',
  ssl: process.env.DB_SSL === 'true',
} as const;

// ============================================================================
// Redis
// ============================================================================

export const REDIS = {
  host: process.env.REDIS_HOST || 'localhost',
  port: parseInt(process.env.REDIS_PORT || '6379'),
  password: process.env.REDIS_PASSWORD || '',
  db: parseInt(process.env.REDIS_DB || '0'),
} as const;

// ============================================================================
// External APIs
// ============================================================================

export const EXTERNAL_APIS = {
  BINANCE: {
    apiUrl: process.env.BINANCE_API_URL || 'https://api.binance.com',
    wsUrl: process.env.BINANCE_WS_URL || 'wss://stream.binance.com:9443',
    testnetApiUrl: 'https://testnet.binance.vision',
    testnetWsUrl: 'wss://testnet.binance.vision',
  },
  STRIPE: {
    apiUrl: 'https://api.stripe.com',
    webhookPath: '/webhooks/stripe',
  },
  OLLAMA: {
    url: process.env.OLLAMA_URL || 'http://localhost:11434',
    model: process.env.OLLAMA_MODEL || 'llama3',
  },
} as const;

// ============================================================================
// API Versioning
// ============================================================================

export const API = {
  version: 'v1',
  basePath: '/api/v1',
} as const;

// ============================================================================
// Timeouts & Retries
// ============================================================================

export const TIMEOUTS = {
  default: 30000,
  ml: 60000,
  llm: 120000,
  trading: 10000,
} as const;

export const RETRIES = {
  attempts: 3,
  delay: 1000,
  backoffMultiplier: 2,
} as const;

// ============================================================================
// WebSocket Channels
// ============================================================================

export const WS_CHANNELS = {
  TICKER: 'ticker',
  ORDERBOOK: 'orderbook',
  TRADES: 'trades',
  CANDLES: 'candles',
  SIGNALS: 'signals',
  ORDERS: 'orders',
  POSITIONS: 'positions',
  NOTIFICATIONS: 'notifications',
} as const;

// ============================================================================
// Trading Constants
// ============================================================================

export const TRADING = {
  defaultLeverage: 1,
  maxLeverage: 100,
  minOrderSize: 0.001,
  maxOrderSize: 1000000,
  supportedTimeframes: ['1m', '5m', '15m', '1h', '4h', '1d', '1w'] as const,
  supportedOrderTypes: ['market', 'limit', 'stop_loss', 'take_profit'] as const,
} as const;

// ============================================================================
// Agent Profiles
// ============================================================================

export const AGENT_PROFILES = {
  ATLAS: {
    name: 'Atlas',
    profile: 'conservative',
    maxDrawdown: 0.05,
    targetMonthly: 0.03,
    riskPerTrade: 0.01,
  },
  ORION: {
    name: 'Orion',
    profile: 'moderate',
    maxDrawdown: 0.10,
    targetMonthly: 0.07,
    riskPerTrade: 0.02,
  },
  NOVA: {
    name: 'Nova',
    profile: 'aggressive',
    maxDrawdown: 0.20,
    targetMonthly: 0.15,
    riskPerTrade: 0.03,
  },
} as const;

// ============================================================================
// Helper Functions
// ============================================================================

export function getServiceUrl(serviceName: keyof typeof SERVICES): string {
  return SERVICES[serviceName].url;
}

export function buildApiUrl(serviceName: keyof typeof SERVICES, path: string): string {
  const baseUrl = getServiceUrl(serviceName);
  const cleanPath = path.startsWith('/') ? path : `/${path}`;
  return `${baseUrl}${API.basePath}${cleanPath}`;
}

export function getDatabaseUrl(): string {
  const { user, password, host, port, name, ssl } = DATABASE;
  const sslParam = ssl ? '?sslmode=require' : '';
  return `postgresql://${user}:${password}@${host}:${port}/${name}${sslParam}`;
}

export function getRedisUrl(): string {
  const { host, port, password, db } = REDIS;
  const auth = password ? `:${password}@` : '';
  return `redis://${auth}${host}:${port}/${db}`;
}
