services:
  # Django Backend with ASGI (WebSocket support)
  backend:
    build:
      context: ./backend
      target: development
    ports:
      - "8000:8000"
    volumes:
      - ./backend:/app
      - ./media:/app/media
    # Linux webcam support (comment out on macOS/Windows if needed)
    devices:
      - /dev/video0:/dev/video0
    group_add:
      - video
    user: "${UID:-1000}:${GID:-1000}"
    environment:
      - DEBUG=1
      - DB_HOST=postgres
      - DB_NAME=media_analyzer
      - DB_USER=media_user
      - DB_PASSWORD=media_pass
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      # RTMP ingestion host reachable from backend container
      - RTMP_HOST=nginx-rtmp
      - HLS_BASE_URL=http://nginx-rtmp:8081
      - HLS_ENDPOINT_PATH=/
      - MEDIA_ROOT=/app/media
      - ALLOWED_HOSTS=localhost,127.0.0.1,backend
      - TRANSFORMERS_CACHE=/tmp/huggingface
      - HF_HOME=/tmp/huggingface  
      - TORCH_HOME=/tmp/torch
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    command: ./entrypoint-web.sh


  # Celery Worker - Logo Detection Queue
  celery-logo:
    build:
      context: ./backend
      target: development
    volumes:
      - ./backend:/app
      - ./media:/app/media
    environment:
      - DEBUG=1
      - DB_HOST=postgres
      - DB_NAME=media_analyzer
      - DB_USER=media_user
      - DB_PASSWORD=media_pass
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - TRANSFORMERS_CACHE=/tmp/huggingface
      - HF_HOME=/tmp/huggingface  
      - TORCH_HOME=/tmp/torch
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    command: ./entrypoint-celery.sh --queues=logo_detection --hostname=logo-worker@%h

  # Celery Worker - Visual Analysis Queue  
  # celery-visual:
  #   build:
  #     context: ./backend
  #     target: development
  #   volumes:
  #     - ./backend:/app
  #     - ./media:/app/media
  #   environment:
  #     - DEBUG=1
  #     - DB_HOST=postgres
  #     - DB_NAME=media_analyzer
  #     - DB_USER=media_user
  #     - DB_PASSWORD=media_pass
  #     - REDIS_HOST=redis
  #     - REDIS_PORT=6379
  #   depends_on:
  #     postgres:
  #       condition: service_healthy
  #     redis:
  #       condition: service_started
  #   command: ./entrypoint-celery.sh --queues=visual_analysis --hostname=visual-worker@%h

  # Celery Worker - Default Queue
  celery-default:
    build:
      context: ./backend
      target: development
    volumes:
      - ./backend:/app
      - ./media:/app/media
    environment:
      - DEBUG=1
      - DB_HOST=postgres
      - DB_NAME=media_analyzer
      - DB_USER=media_user
      - DB_PASSWORD=media_pass
      - REDIS_HOST=redis
      - REDIS_PORT=6379
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    command: ./entrypoint-celery.sh --queues=default,config_management --hostname=default-worker@%h

  # Event Source - Pluggable segment monitoring (file watcher, cloud events, etc)
  event-source:
    build:
      context: ./backend
      target: development
    volumes:
      - ./backend:/app
      - ./media:/app/media
    environment:
      - DEBUG=1
      - DB_HOST=postgres
      - DB_NAME=media_analyzer
      - DB_USER=media_user
      - DB_PASSWORD=media_pass
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - MEDIA_ROOT=/app/media
      # Event source configuration
      - SEGMENT_EVENT_SOURCE=filewatcher  # Options: filewatcher, cloud, webhook
      - FILE_WATCHER_POLL_INTERVAL=1.0
      - WEBHOOK_PORT=8001
      # Cloud configuration (for future use)
      - GCS_BUCKET_NAME=media-analyzer-segments
      - USE_CLOUD_STORAGE=false
      - USE_CLOUD_VISION=false
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    command: python manage.py start_event_source
  
  # Event Processor - Consumes segment events from Redis  
  event-processor:
    build:
      context: ./backend
      target: development
    volumes:
      - ./backend:/app
      - ./media:/app/media
    environment:
      - DEBUG=1
      - DB_HOST=postgres
      - DB_NAME=media_analyzer
      - DB_USER=media_user
      - DB_PASSWORD=media_pass
      - REDIS_HOST=redis
      - REDIS_PORT=6379
      - MEDIA_ROOT=/app/media
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    command: celery -A media_analyzer worker -l info -Q default -c 2 --hostname=event-processor@%h

  # Angular Frontend with NGINX (unified approach)
  frontend:
    build:
      context: ./frontend
      target: development  # Use development stage with NGINX
    ports:
      - "4200:80"  # Map host 4200 to container 80 (nginx)
    depends_on:
      - backend
  # PostgreSQL Database
  postgres:
    image: postgres:15
    environment:
      POSTGRES_DB: media_analyzer
      POSTGRES_USER: media_user
      POSTGRES_PASSWORD: media_pass
    ports:
      - "5432:5432"
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U media_user -d media_analyzer"]
      interval: 5s
      timeout: 5s
      retries: 5

  # Redis for Celery
  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
    volumes:
      - redis_data:/data

  # NGINX with RTMP module for stream ingestion
  nginx-rtmp:
    image: tiangolo/nginx-rtmp
    ports:
      - "0.0.0.0:1935:1935"  # RTMP port - bind to all interfaces
      - "0.0.0.0:8081:80"    # HTTP port for HLS - bind to all interfaces
    volumes:
      # Persist HLS segments and playlists to host media directory - FIXED: same path as event-source
      - ./media:/app/media
      # Custom nginx.conf for RTMP/HLS configuration
      - ./docker/nginx.conf:/etc/nginx/nginx.conf
    depends_on:
      - postgres

volumes:
  postgres_data:
  redis_data: