# =============================================================================
# Docker Compose Production Configuration
# Use with: docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
# =============================================================================

version: '3.8'

services:
  # Production web service configuration
  web:
    environment:
      - DEBUG=false
      - LOG_LEVEL=WARNING
      - SKIP_STARTUP_TEST=true
      - PYTHONOPTIMIZE=1
    
    # Production resource limits
    deploy:
      replicas: 3
      resources:
        limits:
          memory: 2G
          cpus: "2.0"
        reservations:
          memory: 1G
          cpus: "1.0"
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      update_config:
        parallelism: 1
        delay: 30s
        failure_action: rollback
        order: start-first
    
    # Remove development volumes
    volumes:
      - /dev/shm:/dev/shm
      - ./static:/app/static:ro
      - ./logs:/app/logs
    
    # Production logging
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "5"

  # Production MongoDB with authentication
  mongodb:
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD}
    
    # Production resource limits
    deploy:
      resources:
        limits:
          memory: 2G
          cpus: "1.0"
        reservations:
          memory: 1G
          cpus: "0.5"
    
    # Production logging
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "3"

  # Production Redis configuration
  redis:
    command: >
      redis-server
      --save 300 10
      --save 60 10000
      --loglevel warning
      --maxmemory 1gb
      --maxmemory-policy allkeys-lru
      --appendonly yes
      --appendfsync everysec
      --auto-aof-rewrite-percentage 100
      --auto-aof-rewrite-min-size 64mb
    
    # Production resource limits
    deploy:
      resources:
        limits:
          memory: 1G
          cpus: "0.5"
        reservations:
          memory: 512M
          cpus: "0.25"
    
    # Production logging
    logging:
      driver: "json-file"
      options:
        max-size: "50m"
        max-file: "3"

  # Enable nginx for production
  nginx:
    profiles:
      - ""  # Enable by default in production

  # Enable monitoring for production
  prometheus:
    profiles:
      - ""  # Enable by default in production

  grafana:
    profiles:
      - ""  # Enable by default in production

# Production networks with custom settings
networks:
  scraper-network:
    driver: bridge
    driver_opts:
      com.docker.network.driver.mtu: 1450
      com.docker.network.bridge.enable_icc: "true"
      com.docker.network.bridge.enable_ip_masquerade: "true"
    ipam:
      driver: default
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1

# Production volumes with performance optimizations
volumes:
  mongodb_data:
    driver: local
    driver_opts:
      type: none
      o: bind,noatime
      device: ${DATA_PATH:-/var/lib/scraper}/mongodb

  redis_data:
    driver: local
    driver_opts:
      type: none
      o: bind,noatime
      device: ${DATA_PATH:-/var/lib/scraper}/redis

  prometheus_data:
    driver: local
    driver_opts:
      type: none
      o: bind,noatime
      device: ${DATA_PATH:-/var/lib/scraper}/prometheus

  grafana_data:
    driver: local
    driver_opts:
      type: none
      o: bind,noatime
      device: ${DATA_PATH:-/var/lib/scraper}/grafana
