# Docker Compose configuration for Ad-tlas application
# Supports development, staging, and production environments

version: '3.8'

services:
  # PostgreSQL Database
  db:
    image: postgres:15-alpine
    container_name: adtlas-postgres
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${POSTGRES_DB:-adtlas_db}
      POSTGRES_USER: ${POSTGRES_USER:-adtlas_user}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-adtlas_password}
      POSTGRES_INITDB_ARGS: "--encoding=UTF-8 --lc-collate=C --lc-ctype=C"
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./postgres-init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    ports:
      - "${POSTGRES_PORT:-5432}:5432"
    networks:
      - adtlas-network
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-adtlas_user} -d ${POSTGRES_DB:-adtlas_db}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 10s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Redis Cache and Message Broker
  redis:
    image: redis:7-alpine
    container_name: adtlas-redis
    restart: unless-stopped
    command: redis-server /usr/local/etc/redis/redis.conf
    volumes:
      - redis_data:/data
      - ./redis.conf:/usr/local/etc/redis/redis.conf:ro
    ports:
      - "${REDIS_PORT:-6379}:6379"
    networks:
      - adtlas-network
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 10s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Django Web Application
  web:
    build:
      context: .
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-production}
      args:
        BUILD_DATE: ${BUILD_DATE}
        VCS_REF: ${VCS_REF}
        VERSION: ${VERSION:-1.0.0}
    container_name: adtlas-web
    restart: unless-stopped
    environment:
      # Django settings
      DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE:-config.settings.production}
      DJANGO_ENV: ${DJANGO_ENV:-production}
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
      DJANGO_DEBUG: ${DJANGO_DEBUG:-False}
      DJANGO_ALLOWED_HOSTS: ${DJANGO_ALLOWED_HOSTS:-localhost,127.0.0.1}
      
      # Database settings
      DB_NAME: ${POSTGRES_DB:-adtlas_db}
      DB_USER: ${POSTGRES_USER:-adtlas_user}
      DB_PASSWORD: ${POSTGRES_PASSWORD:-adtlas_password}
      DB_HOST: db
      DB_PORT: 5432
      
      # Redis settings
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
      
      # Email settings
      EMAIL_HOST: ${EMAIL_HOST}
      EMAIL_PORT: ${EMAIL_PORT:-587}
      EMAIL_HOST_USER: ${EMAIL_HOST_USER}
      EMAIL_HOST_PASSWORD: ${EMAIL_HOST_PASSWORD}
      EMAIL_USE_TLS: ${EMAIL_USE_TLS:-True}
      
      # Security settings
      SECURE_SSL_REDIRECT: ${SECURE_SSL_REDIRECT:-False}
      SECURE_HSTS_SECONDS: ${SECURE_HSTS_SECONDS:-31536000}
      
      # Application settings
      LOAD_INITIAL_DATA: ${LOAD_INITIAL_DATA:-false}
      WARM_CACHE: ${WARM_CACHE:-true}
      
      # Superuser settings
      DJANGO_SUPERUSER_USERNAME: ${DJANGO_SUPERUSER_USERNAME}
      DJANGO_SUPERUSER_EMAIL: ${DJANGO_SUPERUSER_EMAIL}
      DJANGO_SUPERUSER_PASSWORD: ${DJANGO_SUPERUSER_PASSWORD}
      
      # Gunicorn settings
      GUNICORN_WORKERS: ${GUNICORN_WORKERS}
      GUNICORN_WORKER_CLASS: ${GUNICORN_WORKER_CLASS:-gevent}
      GUNICORN_TIMEOUT: ${GUNICORN_TIMEOUT:-30}
      
      # Monitoring
      SENTRY_DSN: ${SENTRY_DSN}
    volumes:
      - static_files:/app/staticfiles
      - media_files:/app/media
      - app_logs:/app/logs
    ports:
      - "${WEB_PORT:-8000}:8000"
    networks:
      - adtlas-network
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/health/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Celery Worker
  celery-worker:
    build:
      context: .
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-production}
    container_name: adtlas-celery-worker
    restart: unless-stopped
    command: celery -A config worker --loglevel=info --concurrency=${CELERY_WORKER_CONCURRENCY:-2}
    environment:
      # Django settings
      DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE:-config.settings.production}
      DJANGO_ENV: ${DJANGO_ENV:-production}
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
      
      # Database settings
      DB_NAME: ${POSTGRES_DB:-adtlas_db}
      DB_USER: ${POSTGRES_USER:-adtlas_user}
      DB_PASSWORD: ${POSTGRES_PASSWORD:-adtlas_password}
      DB_HOST: db
      DB_PORT: 5432
      
      # Redis settings
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
      
      # Celery settings
      CELERY_WORKER_CONCURRENCY: ${CELERY_WORKER_CONCURRENCY:-2}
      CELERY_WORKER_MAX_TASKS_PER_CHILD: ${CELERY_WORKER_MAX_TASKS_PER_CHILD:-1000}
    volumes:
      - app_logs:/app/logs
      - media_files:/app/media
    networks:
      - adtlas-network
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "celery", "-A", "config", "inspect", "ping"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Celery Beat Scheduler
  celery-beat:
    build:
      context: .
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-production}
    container_name: adtlas-celery-beat
    restart: unless-stopped
    command: celery -A config beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler
    environment:
      # Django settings
      DJANGO_SETTINGS_MODULE: ${DJANGO_SETTINGS_MODULE:-config.settings.production}
      DJANGO_ENV: ${DJANGO_ENV:-production}
      DJANGO_SECRET_KEY: ${DJANGO_SECRET_KEY}
      
      # Database settings
      DB_NAME: ${POSTGRES_DB:-adtlas_db}
      DB_USER: ${POSTGRES_USER:-adtlas_user}
      DB_PASSWORD: ${POSTGRES_PASSWORD:-adtlas_password}
      DB_HOST: db
      DB_PORT: 5432
      
      # Redis settings
      REDIS_URL: redis://redis:6379/0
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
    volumes:
      - app_logs:/app/logs
      - celery_beat_data:/app/celerybeat-schedule
    networks:
      - adtlas-network
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # Celery Flower (Monitoring)
  celery-flower:
    build:
      context: .
      dockerfile: Dockerfile
      target: ${BUILD_TARGET:-production}
    container_name: adtlas-celery-flower
    restart: unless-stopped
    command: celery -A config flower --port=5555
    environment:
      # Redis settings
      CELERY_BROKER_URL: redis://redis:6379/1
      CELERY_RESULT_BACKEND: redis://redis:6379/2
      
      # Flower settings
      FLOWER_BASIC_AUTH: ${FLOWER_BASIC_AUTH:-admin:admin}
    ports:
      - "${FLOWER_PORT:-5555}:5555"
    networks:
      - adtlas-network
    depends_on:
      - redis
    profiles:
      - monitoring
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

# Named volumes for data persistence
volumes:
  postgres_data:
    driver: local
  redis_data:
    driver: local
  static_files:
    driver: local
  media_files:
    driver: local
  app_logs:
    driver: local
  celery_beat_data:
    driver: local

# Networks
networks:
  adtlas-network:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.100.0/24