version: '3.8'

# Trading Platform - Trading Platform Services
# ML Engine, LLM Agent, Trading Agents

services:
  # ============================================
  # ML ENGINE - Señales y Predicciones
  # ============================================
  ml-engine:
    build:
      context: ./apps/ml-engine
      dockerfile: Dockerfile
    container_name: trading-ml-engine
    ports:
      - "8001:8000"
    environment:
      - ENV=development
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/trading
      - REDIS_URL=redis://redis:6379/0
      - GPU_ENABLED=true
    volumes:
      - ./apps/ml-engine:/app
      - ml-models:/app/models
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    depends_on:
      - postgres
      - redis
    networks:
      - trading-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # LLM AGENT - Copiloto de Trading
  # ============================================
  llm-agent:
    build:
      context: ./apps/llm-agent
      dockerfile: Dockerfile
    container_name: trading-llm-agent
    ports:
      - "8003:8000"
    environment:
      - ENV=development
      - LLM_PROVIDER=ollama
      - OLLAMA_BASE_URL=http://ollama:11434
      - LLM_MODEL=llama3:8b
      - ML_ENGINE_URL=http://ml-engine:8000
      - BACKEND_URL=http://backend:3000
      - DATA_SERVICE_URL=http://data-service:8002
      - REDIS_URL=redis://redis:6379/1
    volumes:
      - ./apps/llm-agent:/app
    depends_on:
      - ollama
      - ml-engine
      - redis
    networks:
      - trading-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # OLLAMA - LLM Local con GPU
  # ============================================
  ollama:
    image: ollama/ollama:latest
    container_name: trading-ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama-models:/root/.ollama
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: 1
              capabilities: [gpu]
    networks:
      - trading-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:11434/api/tags"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # TRADING AGENTS - Atlas, Orion, Nova
  # ============================================
  trading-agents:
    build:
      context: ./apps/trading-agents
      dockerfile: Dockerfile
    container_name: trading-trading-agents
    ports:
      - "8004:8000"
    environment:
      - ENV=development
      - ML_ENGINE_URL=http://ml-engine:8000
      - DATABASE_URL=postgresql://postgres:postgres@postgres:5432/trading
      - REDIS_URL=redis://redis:6379/2
      - BINANCE_TESTNET=true
      - BINANCE_API_KEY=${BINANCE_API_KEY:-}
      - BINANCE_API_SECRET=${BINANCE_API_SECRET:-}
    volumes:
      - ./apps/trading-agents:/app
      - trading-data:/app/data
    depends_on:
      - ml-engine
      - postgres
      - redis
    networks:
      - trading-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ============================================
  # INFRAESTRUCTURA COMPARTIDA
  # ============================================

  postgres:
    image: postgres:15-alpine
    container_name: trading-postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=trading
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./apps/database/schemas:/docker-entrypoint-initdb.d
    networks:
      - trading-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    container_name: trading-redis
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    networks:
      - trading-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  # ============================================
  # MONITOREO (Opcional)
  # ============================================

  # Ollama Web UI para testing
  ollama-webui:
    image: ghcr.io/open-webui/open-webui:main
    container_name: trading-ollama-webui
    ports:
      - "3001:8080"
    environment:
      - OLLAMA_BASE_URL=http://ollama:11434
    volumes:
      - ollama-webui-data:/app/backend/data
    depends_on:
      - ollama
    networks:
      - trading-network
    profiles:
      - debug

volumes:
  postgres-data:
  redis-data:
  ml-models:
  ollama-models:
  ollama-webui-data:
  trading-data:

networks:
  trading-network:
    driver: bridge
