version: '3.8'

# Production compose for Raspberry Pi 5 (4GB)
# No RabbitMQ — Redis serves as Celery broker to save ~300MB RAM
# Usage: docker compose -f docker-compose.prod.yml up -d

services:
  app-prod:
    build: .
    container_name: trading_app_prod
    command: gunicorn app.main:app -w 2 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000 --access-logfile - --error-logfile -
    ports:
      - "127.0.0.1:8000:8000"
    volumes:
      - ./logs:/app/logs
      - ../agentic:/app/agentic
    env_file: .env
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    environment:
      ENVIRONMENT: production
      DEBUG: "False"
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
      CELERY_BROKER_URL: redis://:${REDIS_PASSWORD}@redis:6379/1
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0
      DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      LUCKYST_NETWORK: ${LUCKYST_NETWORK:-testnet}
      KALSHI_API_SECRET: /app/LiveTrader2.txt
    deploy:
      resources:
        limits:
          memory: 512M
    restart: unless-stopped

  postgres:
    image: postgres:15-alpine
    container_name: trading_postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data
    command: >
      postgres
        -c shared_buffers=128MB
        -c effective_cache_size=256MB
        -c work_mem=4MB
        -c maintenance_work_mem=64MB
        -c max_connections=30
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          memory: 512M
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: trading_redis
    ports:
      - "127.0.0.1:6379:6379"
    volumes:
      - redis_data:/data
    command: redis-server --appendonly yes --requirepass ${REDIS_PASSWORD} --maxmemory 128mb --maxmemory-policy allkeys-lru
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
    deploy:
      resources:
        limits:
          memory: 192M
    restart: unless-stopped

  celery-worker:
    build: .
    container_name: trading_celery
    command: celery -A app.tasks.trading_tasks worker --loglevel=info --concurrency=2
    volumes:
      - ./logs:/app/logs
      - ../agentic:/app/agentic
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    env_file: .env
    environment:
      POSTGRES_HOST: postgres
      REDIS_HOST: redis
      CELERY_BROKER_URL: redis://:${REDIS_PASSWORD}@redis:6379/1
      CELERY_RESULT_BACKEND: redis://:${REDIS_PASSWORD}@redis:6379/0
      LUCKYST_NETWORK: ${LUCKYST_NETWORK:-testnet}
      KALSHI_API_SECRET: /app/LiveTrader2.txt
      LUCKBOT_BASE_ADDRESS: ${LUCKBOT_BASE_ADDRESS}
      LUCKBOT_BASE_PRIVATE_KEY: ${LUCKBOT_BASE_PRIVATE_KEY}
    deploy:
      resources:
        limits:
          memory: 384M
    restart: unless-stopped

  luckbot:
    build:
      context: ../agentic
      dockerfile: Dockerfile
    container_name: trading_luckbot
    env_file: .env
    environment:
      LUCKYST_API_BASE: http://app-prod:8000/api/v1
      REDIS_HOST: redis
      REDIS_PORT: 6379
      REDIS_PASSWORD: ${REDIS_PASSWORD}
      LUCKBOT_SESSION_DIR: /app/data
    depends_on:
      app-prod:
        condition: service_started
      redis:
        condition: service_healthy
    volumes:
      - luckbot_data:/app/data
    deploy:
      resources:
        limits:
          memory: 256M
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:
  luckbot_data:
