# ═══════════════════════════════════════════════════
# Docker Compose - Production Configuration
# Google Alerts Reporter Enterprise
# ═══════════════════════════════════════════════════

version: '3.8'

services:
  # FastAPI application
  web:
    build:
      context: ..
      dockerfile: Dockerfile
    container_name: alerts-reporter-prod
    image: alerts-reporter:production

    environment:
      - DEBUG=false
      - HOST=0.0.0.0
      - PORT=8000
      - LOG_LEVEL=INFO

    env_file:
      - .env.prod

    volumes:
      # Persistent data
      - ../reports:/app/reports
      - ../logs:/app/logs
      - ./backup:/app/backup

      # Gmail credentials
      - ${GMAIL_CREDENTIALS_PATH}:/app/credentials.json:ro
      - ${GMAIL_TOKEN_PATH}:/app/token.json

      # Historical data
      - historical_data:/app/data

    restart: always

    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

    networks:
      - alerts-network

    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Nginx reverse proxy
  nginx:
    image: nginx:1.25-alpine
    container_name: alerts-nginx
    ports:
      - "80:80"
      - "443:443"

    volumes:
      # Nginx configuration
      - ./nginx/nginx.conf:/etc/nginx/templates/default.conf.template:ro

      # SSL certificates
      - ./certbot/conf:/etc/letsencrypt:ro
      - ./certbot/www:/var/www/certbot:ro

      # Static files (for direct serving)
      - ../static:/app/static:ro

      # Logs
      - ./nginx/logs:/var/log/nginx

    environment:
      - DOMAIN=${DOMAIN}

    depends_on:
      - web

    restart: always

    networks:
      - alerts-network

    logging:
      driver: "json-file"
      options:
        max-size: "5m"
        max-file: "3"

  # Certbot for SSL renewal
  certbot:
    image: certbot/certbot:latest
    container_name: alerts-certbot
    volumes:
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot

    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew --webroot -w /var/www/certbot --quiet; sleep 12h & wait $${!}; done;'"

    networks:
      - alerts-network

    logging:
      driver: "json-file"
      options:
        max-size: "2m"
        max-file: "2"

  # Backup service (runs daily)
  backup:
    build:
      context: ..
      dockerfile: Dockerfile
    container_name: alerts-backup
    image: alerts-reporter:production

    volumes:
      - ../reports:/app/reports:ro
      - ./backup:/backup
      - ${GMAIL_TOKEN_PATH}:/app/token.json:ro
      - .env.prod:/app/.env:ro

    environment:
      - BACKUP_ENABLED=true

    entrypoint: "/bin/sh -c 'while true; do /app/backup/backup.sh; sleep 86400; done'"

    networks:
      - alerts-network

    logging:
      driver: "json-file"
      options:
        max-size: "2m"
        max-file: "2"

networks:
  alerts-network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/16

volumes:
  historical_data:
    driver: local
