version: '3.8'

# =============================================================================
# MockupAWS Scheduler Service - Docker Compose
# =============================================================================
# This file provides a separate scheduler service for running cron jobs.
# 
# Usage:
#   # Run scheduler alongside main services
#   docker-compose -f docker-compose.yml -f docker-compose.scheduler.yml up -d
#
#   # Run only scheduler
#   docker-compose -f docker-compose.scheduler.yml up -d scheduler
#
#   # View scheduler logs
#   docker-compose logs -f scheduler
# =============================================================================

services:
  # Redis (required for Celery - Option 3)
  redis:
    image: redis:7-alpine
    container_name: mockupaws-redis
    restart: unless-stopped
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - mockupaws-network

  # =============================================================================
  # OPTION 1: Standalone Scheduler Service (Recommended for v0.5.0)
  # Uses APScheduler running in a separate container
  # =============================================================================
  scheduler:
    build:
      context: .
      dockerfile: Dockerfile.backend
    container_name: mockupaws-scheduler
    restart: unless-stopped
    command: >
      sh -c "python -m src.jobs.report_scheduler"
    environment:
      - DATABASE_URL=${DATABASE_URL:-postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws}
      - REDIS_URL=${REDIS_URL:-redis://redis:6379/0}
      - SCHEDULER_ENABLED=true
      - SCHEDULER_INTERVAL_MINUTES=5
      # Email configuration
      - EMAIL_PROVIDER=${EMAIL_PROVIDER:-sendgrid}
      - SENDGRID_API_KEY=${SENDGRID_API_KEY}
      - EMAIL_FROM=${EMAIL_FROM:-noreply@mockupaws.com}
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
      - AWS_REGION=${AWS_REGION:-us-east-1}
      # JWT
      - JWT_SECRET_KEY=${JWT_SECRET_KEY}
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    networks:
      - mockupaws-network
    volumes:
      - ./storage/reports:/app/storage/reports
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

  # =============================================================================
  # OPTION 2: Celery Worker (For high-volume processing)
  # Uncomment to use Celery + Redis for distributed task processing
  # =============================================================================
  # celery-worker:
  #   build:
  #     context: .
  #     dockerfile: Dockerfile.backend
  #   container_name: mockupaws-celery-worker
  #   restart: unless-stopped
  #   command: >
  #     sh -c "celery -A src.jobs.celery_app worker --loglevel=info --concurrency=2"
  #   environment:
  #     - DATABASE_URL=${DATABASE_URL:-postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws}
  #     - CELERY_BROKER_URL=${REDIS_URL:-redis://redis:6379/0}
  #     - CELERY_RESULT_BACKEND=${REDIS_URL:-redis://redis:6379/0}
  #     - EMAIL_PROVIDER=${EMAIL_PROVIDER:-sendgrid}
  #     - SENDGRID_API_KEY=${SENDGRID_API_KEY}
  #     - EMAIL_FROM=${EMAIL_FROM:-noreply@mockupaws.com}
  #   depends_on:
  #     - redis
  #     - postgres
  #   networks:
  #     - mockupaws-network
  #   volumes:
  #     - ./storage/reports:/app/storage/reports

  # =============================================================================
  # OPTION 3: Celery Beat (Scheduler)
  # Uncomment to use Celery Beat for cron-like scheduling
  # =============================================================================
  # celery-beat:
  #   build:
  #     context: .
  #     dockerfile: Dockerfile.backend
  #   container_name: mockupaws-celery-beat
  #   restart: unless-stopped
  #   command: >
  #     sh -c "celery -A src.jobs.celery_app beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler"
  #   environment:
  #     - DATABASE_URL=${DATABASE_URL:-postgresql+asyncpg://postgres:postgres@postgres:5432/mockupaws}
  #     - CELERY_BROKER_URL=${REDIS_URL:-redis://redis:6379/0}
  #     - CELERY_RESULT_BACKEND=${REDIS_URL:-redis://redis:6379/0}
  #   depends_on:
  #     - redis
  #     - postgres
  #   networks:
  #     - mockupaws-network

# Reuse network from main docker-compose.yml
networks:
  mockupaws-network:
    external: true
    name: mockupaws_mockupaws-network

volumes:
  redis_data:
    driver: local
