# ==============================================================================
# ADTLAS DAI MANAGEMENT SYSTEM - DOCKER COMPOSE CONFIGURATION
# ==============================================================================
#
# This Docker Compose file defines the complete infrastructure for the Adtlas
# Dynamic Ad Insertion (DAI) Management System. It includes:
#
# CORE SERVICES:
# - web: Django application server with Gunicorn
# - db: PostgreSQL database for data persistence
# - redis: Redis cache and message broker for Celery
# - celery: Background task worker for async operations
# - celery-beat: Periodic task scheduler
# - flower: Celery monitoring dashboard
# - nginx: Reverse proxy and static file server
#
# MONITORING STACK:
# - prometheus: Metrics collection and storage
# - grafana: Metrics visualization and dashboards
# - alertmanager: Alert routing and management
# - node-exporter: System metrics collection
# - cadvisor: Container metrics collection
#
# VOLUMES:
# - postgres_data: PostgreSQL database persistence
# - redis_data: Redis data persistence
# - static_volume: Django static files
# - media_volume: User uploaded media files
# - prometheus_data: Prometheus metrics storage
# - grafana_data: Grafana configuration and dashboards
#
# NETWORKS:
# - adtlas_network: Internal communication between services
# - monitoring_network: Isolated network for monitoring stack
#
# Author: Adtlas Development Team
# Version: 2.0.0
# Last Updated: 2025-01-20
# ==============================================================================

version: '3.8'

# ==============================================================================
# NETWORKS DEFINITION
# ==============================================================================
# Network isolation for different service groups
networks:
  # Main application network for core services
  adtlas_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.28.0.0/16
    labels:
      - "com.adtlas.network.description=Main application network"
  
  # Monitoring network for metrics and alerting services
  monitoring_network:
    driver: bridge
    ipam:
      config:
        - subnet: 172.29.0.0/16
    labels:
      - "com.adtlas.network.description=Monitoring and metrics network"

# ==============================================================================
# VOLUMES DEFINITION
# ==============================================================================
# Persistent storage volumes for data that needs to survive container restarts
volumes:
  # PostgreSQL database data
  postgres_data:
    driver: local 
  # Redis cache and session data
  redis_data:
    driver: local 
  # Django static files (CSS, JS, images)
  static_volume:
    driver: local 
  # User uploaded media files
  media_volume:
    driver: local 
  # Prometheus metrics storage
  prometheus_data:
    driver: local 
  # Grafana configuration and dashboards
  grafana_data:
    driver: local 
  # Alertmanager data
  alertmanager_data:
    driver: local 
  # Application logs
  app_logs:
    driver: local

  # celery_beat_data:
  #   driver: local  

# ==============================================================================
# SERVICES DEFINITION
# ==============================================================================
services:

  # ============================================================================
  # CONTAINER METRICS EXPORTER - cAdvisor
  # ============================================================================
  # cAdvisor collects container and resource usage metrics:
  # - Container CPU and memory usage
  # - Network and disk I/O statistics
  # - Container lifecycle events
  # - Resource limits and quotas
  cadvisor:
    image: gcr.io/cadvisor/cadvisor:v0.47.2
    container_name: adtlas_cadvisor
    restart: unless-stopped 
    privileged: true
    environment:
      - GLOG_v=0  # Suppress verbose logging
    devices:
      - /dev/kmsg:/dev/kmsg
    volumes:
      # Docker and system access for container metrics
      - /:/rootfs:ro
      - /var/run:/var/run:ro
      - /sys:/sys:ro
      - /var/lib/docker:/var/lib/docker:ro
      - /cgroup:/cgroup:ro
    ports:
      # cAdvisor web interface
      - "8089:8080"
    networks:
      - monitoring_network
    security_opt:
      - no-new-privileges:true  
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # SYSTEM METRICS EXPORTER - Node Exporter
  # ============================================================================
  # Node Exporter collects system-level metrics:
  # - CPU, memory, disk usage
  # - Network statistics
  # - File system metrics
  # - System load and processes
  node-exporter:
    image: prom/node-exporter:v1.8.1
    container_name: adtlas_node_exporter
    restart: unless-stopped
    command:
      - '--path.procfs=/host/proc'
      - '--path.rootfs=/rootfs'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
    volumes:
      # Host system access for metrics collection
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    ports:
      # Node Exporter metrics endpoint
      - "9100:9100"
    networks:
      - monitoring_network
    healthcheck:
      test: ["CMD", "wget", "--spider", "-q", "http://localhost:9100/metrics"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    security_opt:
      - no-new-privileges:true  
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # ALERT MANAGEMENT SERVICE - Alertmanager
  # ============================================================================
  # Alertmanager handles alerts from Prometheus:
  # - Alert routing and grouping
  # - Notification delivery (email, Slack, etc.)
  # - Alert silencing and inhibition
  # - High availability clustering
  alertmanager:
    image: prom/alertmanager:latest
    container_name: adtlas_alertmanager
    restart: unless-stopped
    entrypoint: ["/bin/sh", "/usr/local/bin/alertmanager-startup.sh"]
    ports:
      # Alertmanager web interface
      - "9093:9093"
    env_file:
      - .env.alertmanager
    volumes:
      # Alertmanager configuration template
      - ./docker/monitoring/alertmanager/alertmanager.yml.template:/etc/alertmanager/alertmanager.yml.template:ro
      # Alertmanager startup script
      - ./docker/monitoring/alertmanager/alertmanager-startup.sh:/usr/local/bin/alertmanager-startup.sh:ro
      # Persistent storage for alert data
      - alertmanager_data:/alertmanager
    networks:
      - monitoring_network
    healthcheck:
      # Health check to ensure Alertmanager is running
      test: ["CMD", "wget", "--spider", "--quiet", "http://localhost:9093/-/healthy"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    security_opt:
      - no-new-privileges:true  
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # DATABASE SERVICE - PostgreSQL
  # ============================================================================
  # Primary database for storing all application data including:
  # - User accounts and authentication
  # - Campaign and ad spot information
  # - Channel and playlist data
  # - Analytics and reporting data
  # - VAST response tracking
  db:
    # Use the latest Postgres image from Docker Hub
    image: postgres:15-alpine
    # Assign a specific name to the container
    container_name: adtlas_postgres
    # Always restart the container if it stops 
    restart: unless-stopped 
    env_file:
      - .env.database
    volumes: 
      # Persistent storage for database data
      - postgres_data:/var/lib/postgresql/data
      # Custom initialization scripts
      - ./docker/database/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:ro 
    networks:
      - adtlas_network 
    healthcheck:
      # Health check to ensure database is ready (optimized to reduce connection noise)
      test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER:-adtlas_user} -h localhost"]
      interval: 60s
      timeout: 5s
      retries: 3
      start_period: 40s
    security_opt:
      - no-new-privileges:true  
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # CACHE AND MESSAGE BROKER SERVICE - Redis
  # ============================================================================
  # Redis serves multiple purposes:
  # - Session storage for user authentication
  # - Cache for frequently accessed data
  # - Message broker for Celery task queue
  # - Real-time data storage for live features
  redis: 
    # Use the latest Redis image from Docker Hub
    image: redis:7-alpine
    # Assign a specific name to the container
    container_name: adtlas_redis
    # Always restart the container if it stops
    restart: unless-stopped  
    command: redis-server /usr/local/etc/redis/redis.conf
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:-}
    volumes:
      # Persistent storage for Redis data
      - redis_data:/data
      # Custom Redis configuration
      - ./docker/redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
    networks:
      - adtlas_network
    healthcheck:
      # Health check to ensure Redis is responsive
      test: ["CMD", "redis-cli", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s 
    security_opt:
      - no-new-privileges:true   
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # WEB APPLICATION SERVICE - Django with Gunicorn
  # ============================================================================
  # Main Django application server handling:
  # - HTTP requests and responses
  # - Business logic processing
  # - Database operations
  # - API endpoints
  # - Template rendering
  web:
    build:
      context: .
      dockerfile: docker/web/Dockerfile
      args:
        # Build arguments for customization
        PYTHON_VERSION: 3.11
        DJANGO_ENV: ${DJANGO_ENV:-production}
    container_name: adtlas_web
    restart: unless-stopped
    command: /usr/local/bin/entrypoint.sh web
    env_file:
      - .env
    volumes:
      # Application source code (development mode)
      - ./src:/app
      # Static files for Nginx serving
      - static_volume:/app/staticfiles
      # Media files for user uploads
      - media_volume:/app/media
      # Application logs
      - app_logs:/app/logs
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - adtlas_network
      - monitoring_network
    healthcheck:
      # Health check for Django application
      test: ["CMD", "curl", "-f", "http://localhost:8000"]
      interval: 30s
      timeout: 10s
      retries: 5
      start_period: 60s
    security_opt:
      - no-new-privileges:true
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # BACKGROUND TASK WORKER SERVICE - Celery
  # ============================================================================
  # Celery worker for processing background tasks:
  # - VAST call processing and tracking
  # - File encoding and transcoding
  # - Analytics data processing
  # - Report generation
  # - Email and notification sending
  # - Data synchronization
  celery:
    build:
      context: .
      dockerfile: docker/web/Dockerfile
      args:
        PYTHON_VERSION: 3.11
        DJANGO_ENV: ${DJANGO_ENV:-production}
    container_name: adtlas_celery
    restart: unless-stopped
    command: /usr/local/bin/entrypoint.sh celery
    env_file:
      - .env  
    volumes:
      # Application source code
      - ./src:/app
      # Media files for processing
      - media_volume:/app/media
      # Logs for debugging
      - app_logs:/app/logs
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      web:
        condition: service_healthy
    networks:
      - adtlas_network
    healthcheck:
      test: ["CMD", "celery", "-A", "core", "inspect", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    security_opt:
      - no-new-privileges:true
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # PERIODIC TASK SCHEDULER SERVICE - Celery Beat
  # ============================================================================
  # Celery beat scheduler for running periodic tasks:
  # - Scheduled analytics reports
  # - Data cleanup operations
  # - System maintenance tasks
  # - Campaign status synchronization
  # - VAST data updates
  celery-beat:
    build:
      context: .
      dockerfile: docker/web/Dockerfile
      args:
        PYTHON_VERSION: 3.11
        DJANGO_ENV: ${DJANGO_ENV:-production}
    container_name: adtlas_celery_beat
    restart: unless-stopped
    command: /usr/local/bin/entrypoint.sh beat
    env_file:
      - .env
    volumes:
      # Application source code
      - ./src:/app
      # Logs for debugging
      - app_logs:/app/logs
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      web:
        condition: service_healthy
      celery:
        condition: service_healthy
    security_opt:
      - no-new-privileges:true
    networks:
      - adtlas_network 
    healthcheck:
      test: ["CMD", "python", "-c", "import sys; sys.exit(0)"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # CELERY MONITORING SERVICE - Flower
  # ============================================================================
  # Flower web-based monitoring tool for Celery:
  # - Real-time task monitoring
  # - Worker status and statistics
  # - Task history and results
  # - Broker monitoring
  # - Performance metrics
  flower:
    build:
      context: .
      dockerfile: docker/web/Dockerfile
      args:
        PYTHON_VERSION: 3.11
        DJANGO_ENV: ${DJANGO_ENV:-production}
    container_name: adtlas_flower
    restart: unless-stopped
    command: /usr/local/bin/entrypoint.sh flower
    env_file:
      - .env
    ports:
      # Expose Flower web interface
      - "5555:5555"
    depends_on:
      redis:
        condition: service_healthy
      web:
        condition: service_healthy
      celery:
        condition: service_healthy
      celery-beat:
        condition: service_healthy
    networks:
      - adtlas_network
    healthcheck:
      test: ["CMD", "curl", "-f", "--user", "admin:flower_admin", "http://localhost:5555"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    security_opt:
      - no-new-privileges:true

  # ============================================================================
  # REVERSE PROXY AND STATIC FILE SERVER - Nginx
  # ============================================================================
  # Nginx serves as:
  # - Reverse proxy for Django application
  # - Static file server for CSS, JS, images
  # - Media file server for user uploads
  # - Load balancer for multiple Django instances
  # - SSL termination point
  nginx:
    image: nginx:alpine
    container_name: adtlas_nginx
    restart: unless-stopped
    volumes:
      # Nginx configuration
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./docker/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
      # Static and media files
      - static_volume:/var/www/static:ro
      - media_volume:/var/www/media:ro
      # SSL certificates (if using HTTPS)
      - ./docker/nginx/ssl:/etc/nginx/ssl:ro
    ports:
      # HTTP and HTTPS ports
      - "8090:80"
      - "9490:443"
    depends_on:
      web:
        condition: service_healthy
    networks:
      - adtlas_network
    security_opt:
      - no-new-privileges:true
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # ============================================================================
  # METRICS COLLECTION SERVICE - Prometheus
  # ============================================================================
  # Prometheus collects and stores metrics from:
  # - Django application metrics
  # - System and container metrics
  # - Database performance metrics
  # - Redis performance metrics
  # - Custom business metrics
  prometheus:
    image: prom/prometheus:latest
    container_name: adtlas_prometheus
    restart: unless-stopped
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/etc/prometheus/console_libraries'
      - '--web.console.templates=/etc/prometheus/consoles'
      - '--storage.tsdb.retention.time=15d'
      - '--web.enable-lifecycle'
      - '--web.enable-admin-api'
    volumes:
      # Prometheus configuration
      - ./docker/monitoring/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ./docker/monitoring/prometheus/rules:/etc/prometheus/rules:ro
      # Persistent storage for metrics
      - prometheus_data:/prometheus
    ports:
      # Prometheus web interface
      - "9091:9090"
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9090/-/healthy"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - monitoring_network
      - adtlas_network
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"
    security_opt:
      - no-new-privileges:true

  # ============================================================================
  # METRICS VISUALIZATION SERVICE - Grafana
  # ============================================================================
  # Grafana provides:
  # - Interactive dashboards for metrics visualization
  # - Alerting and notification system
  # - User management and access control
  # - Data source integration
  # - Custom dashboard creation
  grafana:
    image: grafana/grafana:latest
    container_name: adtlas_grafana
    restart: unless-stopped    
    env_file:
      - .env 
    volumes:
      # Persistent storage for Grafana data
      - grafana_data:/var/lib/grafana
      # Pre-configured dashboards
      - ./docker/monitoring/grafana/provisioning/dashboards:/etc/grafana/provisioning/dashboards:ro
      - ./docker/monitoring/grafana/provisioning/datasources:/etc/grafana/provisioning/datasources:ro
    ports:
      # Grafana web interface
      - "3001:3000" 
    depends_on:
      prometheus:
        condition: service_healthy 
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 30s
    security_opt:
      - no-new-privileges:true
    networks:
      - monitoring_network
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"


