version: '3.8'

# =============================================================================
# TRADING PLATFORM - Docker Compose Configuration
# =============================================================================
# Este archivo define los servicios de APLICACION del proyecto.
# PostgreSQL y Redis usan instancias NATIVAS compartidas del workspace.
# Ref: orchestration/inventarios/DEVENV-PORTS-INVENTORY.yml
# Fecha: 2026-01-07
# =============================================================================
#
# IMPORTANTE - Arquitectura de Instancia Unica Compartida:
#   - PostgreSQL: Instancia nativa en puerto 5432 (NO Docker)
#   - Redis: Instancia nativa en puerto 6379 (NO Docker)
#   - Los proyectos se separan por DATABASE + USER, no por puerto
#
# =============================================================================

services:
  # ===========================================================================
  # NOTA: PostgreSQL y Redis son NATIVOS (no Docker)
  # ===========================================================================
  # PostgreSQL nativo: localhost:5432
  #   - Database: trading_platform
  #   - User: trading_user
  #   - Password: trading_dev_2025
  #
  # Redis nativo: localhost:6379
  #   - DB: 1 (asignado a trading-platform)
  # ===========================================================================

  # ===========================================================================
  # BACKEND SERVICES
  # ===========================================================================

  backend:
    build:
      context: ./apps/backend
      dockerfile: Dockerfile
    container_name: trading-backend
    restart: unless-stopped
    environment:
      NODE_ENV: ${NODE_ENV:-development}
      PORT: ${BACKEND_API_PORT:-3081}
      # PostgreSQL nativo (host.docker.internal para acceder desde Docker a host)
      DB_HOST: host.docker.internal
      DB_PORT: 5432
      DB_NAME: trading_platform
      DB_USER: trading_user
      DB_PASSWORD: ${DB_PASSWORD:-trading_dev_2025}
      # Redis nativo
      REDIS_HOST: host.docker.internal
      REDIS_PORT: 6379
      REDIS_DB: 1
      ML_ENGINE_URL: http://ml-engine:3083
      FRONTEND_URL: http://localhost:${FRONTEND_WEB_PORT:-3080}
      JWT_ACCESS_SECRET: ${JWT_ACCESS_SECRET}
      JWT_REFRESH_SECRET: ${JWT_REFRESH_SECRET}
      STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
    ports:
      - "${BACKEND_API_PORT:-3081}:3081"
      - "${BACKEND_WS_PORT:-3082}:3082"
    volumes:
      - ./apps/backend/src:/app/src
      - backend_node_modules:/app/node_modules
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    command: npm run dev

  # ===========================================================================
  # FRONTEND SERVICES
  # ===========================================================================

  frontend:
    build:
      context: ./apps/frontend
      dockerfile: Dockerfile
    container_name: trading-frontend
    restart: unless-stopped
    environment:
      VITE_API_URL: http://localhost:${BACKEND_API_PORT:-3081}/api/v1
      VITE_WS_URL: ws://localhost:${BACKEND_WS_PORT:-3082}
      VITE_STRIPE_PUBLISHABLE_KEY: ${STRIPE_PUBLISHABLE_KEY}
    ports:
      - "${FRONTEND_WEB_PORT:-3080}:3080"
    volumes:
      - ./apps/frontend/src:/app/src
      - ./apps/frontend/public:/app/public
      - frontend_node_modules:/app/node_modules
    networks:
      - trading-network
    depends_on:
      - backend
    command: npm run dev -- --host 0.0.0.0 --port 3080

  # ===========================================================================
  # PYTHON SERVICES
  # ===========================================================================

  ml-engine:
    build:
      context: ./apps/ml-engine
      dockerfile: Dockerfile
    container_name: trading-ml-engine
    restart: unless-stopped
    environment:
      PYTHONUNBUFFERED: 1
      # PostgreSQL nativo
      DB_HOST: host.docker.internal
      DB_PORT: 5432
      DB_NAME: trading_platform
      DB_USER: trading_user
      DB_PASSWORD: ${DB_PASSWORD:-trading_dev_2025}
      # Redis nativo
      REDIS_HOST: host.docker.internal
      REDIS_PORT: 6379
      REDIS_DB: 1
      PORT: ${ML_ENGINE_PORT:-3083}
      LOG_LEVEL: INFO
    ports:
      - "${ML_ENGINE_PORT:-3083}:3083"
    volumes:
      - ./apps/ml-engine/src:/app/src
      - ./apps/ml-engine/models:/app/models
      - ml_models:/app/trained_models
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    command: uvicorn src.api.main:app --host 0.0.0.0 --port 3083 --reload

  data-service:
    build:
      context: ./apps/data-service
      dockerfile: Dockerfile
    container_name: trading-data-service
    restart: unless-stopped
    environment:
      PYTHONUNBUFFERED: 1
      # PostgreSQL nativo
      DB_HOST: host.docker.internal
      DB_PORT: 5432
      DB_NAME: trading_platform
      DB_USER: trading_user
      DB_PASSWORD: ${DB_PASSWORD:-trading_dev_2025}
      POLYGON_API_KEY: ${POLYGON_API_KEY}
      METAAPI_TOKEN: ${METAAPI_TOKEN}
      METAAPI_ACCOUNT_ID: ${METAAPI_ACCOUNT_ID}
      SYNC_INTERVAL_MINUTES: 5
      LOG_LEVEL: INFO
      PORT: ${DATA_SERVICE_PORT:-3084}
    ports:
      - "${DATA_SERVICE_PORT:-3084}:3084"
    volumes:
      - ./apps/data-service/src:/app/src
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    command: python -m src.main

  llm-agent:
    build:
      context: ./apps/llm-agent
      dockerfile: Dockerfile
    container_name: trading-llm-agent
    restart: unless-stopped
    environment:
      PYTHONUNBUFFERED: 1
      PORT: ${LLM_AGENT_PORT:-3085}
      ML_ENGINE_URL: http://ml-engine:3083
      DATA_SERVICE_URL: http://data-service:3084
      # PostgreSQL nativo
      DB_HOST: host.docker.internal
      DB_PORT: 5432
      DB_NAME: trading_platform
      DB_USER: trading_user
      DB_PASSWORD: ${DB_PASSWORD:-trading_dev_2025}
      # Redis nativo
      REDIS_HOST: host.docker.internal
      REDIS_PORT: 6379
      REDIS_DB: 1
      LLM_PROVIDER: ${LLM_PROVIDER:-ollama}
      LLM_MODEL: ${LLM_MODEL:-llama3.2}
      OLLAMA_URL: ${OLLAMA_URL:-http://host.docker.internal:11434}
      LOG_LEVEL: INFO
    ports:
      - "${LLM_AGENT_PORT:-3085}:3085"
    volumes:
      - ./apps/llm-agent/src:/app/src
      - ./apps/llm-agent/config:/app/config
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      - ml-engine
      - data-service
    command: uvicorn src.api.main:app --host 0.0.0.0 --port 3085 --reload

  trading-agents:
    build:
      context: ./apps/trading-agents
      dockerfile: Dockerfile
    container_name: trading-trading-agents
    restart: unless-stopped
    environment:
      PYTHONUNBUFFERED: 1
      PORT: ${TRADING_AGENTS_PORT:-3086}
      ML_ENGINE_URL: http://ml-engine:3083
      LLM_AGENT_URL: http://llm-agent:3085
      DATA_SERVICE_URL: http://data-service:3084
      # PostgreSQL nativo
      DB_HOST: host.docker.internal
      DB_PORT: 5432
      DB_NAME: trading_platform
      DB_USER: trading_user
      DB_PASSWORD: ${DB_PASSWORD:-trading_dev_2025}
      # Redis nativo
      REDIS_HOST: host.docker.internal
      REDIS_PORT: 6379
      REDIS_DB: 1
      EXCHANGE_MODE: ${EXCHANGE_MODE:-paper}
      INITIAL_BALANCE: ${INITIAL_BALANCE:-100000}
      LOG_LEVEL: INFO
    ports:
      - "${TRADING_AGENTS_PORT:-3086}:3086"
    volumes:
      - ./apps/trading-agents/src:/app/src
      - ./apps/trading-agents/config:/app/config
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    depends_on:
      - ml-engine
      - llm-agent
    command: uvicorn src.api.main:app --host 0.0.0.0 --port 3086 --reload

  # ===========================================================================
  # DEVELOPMENT TOOLS (Optional)
  # ===========================================================================

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: trading-pgadmin
    restart: unless-stopped
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL:-admin@trading-platform.local}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    ports:
      - "${PGADMIN_PORT:-5050}:80"
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    networks:
      - trading-network
    extra_hosts:
      - "host.docker.internal:host-gateway"
    profiles:
      - dev-tools

  mailhog:
    image: mailhog/mailhog:latest
    container_name: trading-mailhog
    restart: unless-stopped
    ports:
      - "${MAILHOG_SMTP_PORT:-1025}:1025"  # SMTP
      - "${MAILHOG_WEB_PORT:-8025}:8025"   # Web UI
    networks:
      - trading-network
    profiles:
      - dev-tools

# =============================================================================
# VOLUMES
# =============================================================================
# NOTA: postgres_data y redis_data eliminados - se usan instancias nativas
volumes:
  backend_node_modules:
    driver: local
  frontend_node_modules:
    driver: local
  ml_models:
    driver: local
  pgadmin_data:
    driver: local

# =============================================================================
# NETWORKS
# =============================================================================
networks:
  trading-network:
    driver: bridge
    name: trading-network

# =============================================================================
# USAGE INSTRUCTIONS
# =============================================================================
# Start all services:
#   docker-compose up -d
#
# Start with development tools:
#   docker-compose --profile dev-tools up -d
#
# View logs:
#   docker-compose logs -f [service-name]
#
# Stop all services:
#   docker-compose down
#
# Reset everything (including volumes):
#   docker-compose down -v
#
# Rebuild a service:
#   docker-compose up -d --build [service-name]
# =============================================================================
