version: '3.8'

# Production Docker Compose for tipsharks-elo-api
# Images are built from Dockerfiles (no code volume mounts).
# Environment variables loaded from .env file.
# API is exposed through nginx reverse proxy on port 80.
# Worker runs on-demand (profile: tools).

services:
  db:
    image: postgres:16-alpine
    container_name: tipsharks_prod_db
    restart: unless-stopped
    env_file:
      - .env
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-tipsharks}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-tipsharks}
      POSTGRES_DB: ${POSTGRES_DB:-tipsharks}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-tipsharks}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - tipsharks-network
    # Production: restrict ulimits, no extra ports exposed beyond the network

  redis:
    image: redis:7-alpine
    container_name: tipsharks_prod_redis
    restart: unless-stopped
    command: redis-server --appendonly yes --save 900 1 --save 300 10 --save 60 10000
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    networks:
      - tipsharks-network

  api:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.api
    image: tipsharks-elo-api:latest
    container_name: tipsharks_prod_api
    restart: unless-stopped
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-tipsharks}:${POSTGRES_PASSWORD:-tipsharks}@db:5432/${POSTGRES_DB:-tipsharks}
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    # No code volume mounts in production (immutable image)
    healthcheck:
      test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=5)"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 15s
    networks:
      - tipsharks-network

  worker:
    build:
      context: .
      dockerfile: infrastructure/docker/Dockerfile.worker
    image: tipsharks-elo-api-worker:latest
    container_name: tipsharks_prod_worker
    restart: unless-stopped
    env_file:
      - .env
    environment:
      DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-tipsharks}:${POSTGRES_PASSWORD:-tipsharks}@db:5432/${POSTGRES_DB:-tipsharks}
      REDIS_URL: redis://redis:6379/0
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    # Worker runs on-demand via profile; not started by default
    profiles:
      - tools
    networks:
      - tipsharks-network

  nginx:
    image: nginx:alpine
    container_name: tipsharks_prod_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      # Uncomment for SSL termination:
      # - "443:443"
    volumes:
      - ./infrastructure/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
      # Mount static files for direct serving
      - ./apps/backend/web/static:/app/static:ro
    depends_on:
      api:
        condition: service_healthy
    networks:
      - tipsharks-network

volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local

networks:
  tipsharks-network:
    driver: bridge
