# ==============================================================================
# ADTLAS DEVELOPMENT DOCKER COMPOSE OVERRIDE
# ==============================================================================
# 
# Development-specific Docker Compose configuration that extends the base
# docker-compose.yml file with development-optimized settings.
#
# Features:
# - Hot code reloading with volume mounts
# - Development tools and debugging
# - Exposed ports for direct access
# - Development-friendly logging
# - Debug mode enabled
#
# Usage:
#   docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
#   OR
#   make dev
#
# Author: Adtlas Development Team
# Version: 1.0.0
# Last Updated: 2025-07-08
# ==============================================================================

version: '3.3'

services:
  # ==============================================================================
  # WEB SERVICE - DEVELOPMENT OVERRIDES
  # ==============================================================================
  web:
    # Development command with hot reloading
    command: >
      sh -c "
        python manage.py collectstatic --noinput &&
        python manage.py migrate &&
        python manage.py runserver 0.0.0.0:8000
      "
    
    # Development environment variables
    environment:
      - DJANGO_SETTINGS_MODULE=core.settings
      - DEBUG=True
      - DJANGO_ENV=development
      - PYTHONPATH=/app
      - PYTHONUNBUFFERED=1
    
    # Development volume mounts for hot reloading
    volumes:
      - ./src:/app:cached  # Use cached for better performance on macOS
      - ./logs:/app/logs:delegated
      # Don't mount static files in development to allow collectstatic
    
    # Expose port for direct access
    ports:
      - "8003:8000"
    
    # Development labels
    labels:
      - "adtlas.environment=development"
      - "adtlas.hot-reload=enabled"

  # ==============================================================================
  # DATABASE SERVICE - DEVELOPMENT OVERRIDES
  # ==============================================================================
  db:
    # Expose database port for external tools
    ports:
      - "5433:5432"
    
    # Development environment variables
    environment:
      POSTGRES_DB: adtlas_db
      POSTGRES_USER: adtlas_user
      POSTGRES_PASSWORD: adtlas_password
      # Enable statement logging for debugging
      POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C"
    
    # Development command with logging
    command: >
      postgres
      -c log_statement=all
      -c log_min_duration_statement=0
      -c log_line_prefix='%t [%p]: [%l-1] user=%u,db=%d,app=%a,client=%h '
    
    # Development labels
    labels:
      - "adtlas.environment=development"
      - "adtlas.logging=verbose"

  # ==============================================================================
  # REDIS SERVICE - DEVELOPMENT OVERRIDES
  # ==============================================================================
  redis:
    # Development command with verbose logging
    command: >
      redis-server
      --appendonly yes
      --appendfsync everysec
      --maxmemory 256mb
      --maxmemory-policy allkeys-lru
      --loglevel notice
      --save ""
    
    # Development labels
    labels:
      - "adtlas.environment=development"

  # ==============================================================================
  # CELERY WORKER - DEVELOPMENT OVERRIDES
  # ==============================================================================
  celery_worker:
    # Development command with hot reloading
    command: >
      celery -A core worker
      --loglevel=debug
      --concurrency=2
      --pool=solo
      --without-gossip
      --without-mingle
    
    # Development environment variables
    environment:
      - DJANGO_SETTINGS_MODULE=core.settings
      - DEBUG=True
      - DJANGO_ENV=development
      - PYTHONPATH=/app
      - PYTHONUNBUFFERED=1
      - C_FORCE_ROOT=1
    
    # Development volume mounts
    volumes:
      - ./src:/app:cached
      - ./logs:/app/logs:delegated
    
    # Development labels
    labels:
      - "adtlas.environment=development"
      - "adtlas.hot-reload=enabled"

  # ==============================================================================
  # CELERY BEAT - DEVELOPMENT OVERRIDES
  # ==============================================================================
  celery_beat:
    # Development environment variables
    environment:
      - DJANGO_SETTINGS_MODULE=core.settings
      - DEBUG=True
      - DJANGO_ENV=development
      - PYTHONPATH=/app
      - PYTHONUNBUFFERED=1
    
    # Development volume mounts
    volumes:
      - ./src:/app:cached
      - ./logs:/app/logs:delegated
    
    # Development labels
    labels:
      - "adtlas.environment=development"
      - "adtlas.hot-reload=enabled"

  # ==============================================================================
  # NGINX SERVICE - DEVELOPMENT OVERRIDES
  # ==============================================================================
  nginx:
    # Development ports (standard HTTP/HTTPS)
    ports:
      - "8084:80"
      - "8085:443"
    
    # Development labels
    labels:
      - "adtlas.environment=development"

# ==============================================================================
# DEVELOPMENT PROFILES - ADDITIONAL TOOLS
# ==============================================================================
# Use with: docker-compose --profile dev up

  # ==============================================================================
  # PGADMIN - DATABASE ADMINISTRATION TOOL
  # ==============================================================================
  pgadmin:
    profiles: ["dev"]
    image: dpage/pgadmin4:latest
    container_name: adtlas_pgadmin
    restart: unless-stopped
    
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@adtlas.com
      PGADMIN_DEFAULT_PASSWORD: admin123
      PGADMIN_CONFIG_SERVER_MODE: 'False'
      PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False'
    
    ports:
      - "5050:80"
    
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    
    networks:
      - backend
    
    depends_on:
      - db
    
    labels:
      - "adtlas.tool=pgadmin"
      - "adtlas.environment=development"

  # ==============================================================================
  # REDIS COMMANDER - REDIS ADMINISTRATION TOOL
  # ==============================================================================
  redis-commander:
    profiles: ["dev"]
    image: rediscommander/redis-commander:latest
    container_name: adtlas_redis_commander
    restart: unless-stopped
    
    environment:
      REDIS_HOSTS: local:redis:6379
      HTTP_USER: admin
      HTTP_PASSWORD: admin123
    
    ports:
      - "8081:8081"
    
    networks:
      - backend
    
    depends_on:
      - redis
    
    labels:
      - "adtlas.tool=redis-commander"
      - "adtlas.environment=development"

  # ==============================================================================
  # MAILHOG - EMAIL TESTING TOOL
  # ==============================================================================
  mailhog:
    profiles: ["dev"]
    image: mailhog/mailhog:latest
    container_name: adtlas_mailhog
    restart: unless-stopped
    
    ports:
      - "1025:1025"  # SMTP port
      - "8025:8025"  # Web UI port
    
    networks:
      - backend
    
    labels:
      - "adtlas.tool=mailhog"
      - "adtlas.environment=development"

  # ==============================================================================
  # FLOWER - CELERY MONITORING TOOL
  # ==============================================================================
  flower:
    profiles: ["dev"]
    image: mher/flower:latest
    container_name: adtlas_flower
    restart: unless-stopped
    
    command: >
      flower
      --broker=redis://redis:6379/3
      --port=5555
      --basic_auth=admin:admin123
    
    ports:
      - "5555:5555"
    
    networks:
      - backend
    
    depends_on:
      - redis
      - celery_worker
    
    labels:
      - "adtlas.tool=flower"
      - "adtlas.environment=development"

# ==============================================================================
# DEVELOPMENT VOLUMES
# ==============================================================================
volumes:
  pgadmin_data:
    labels:
      - "adtlas.volume.type=tool-data"
      - "adtlas.volume.service=pgadmin"
