version: "3.9"

x-common-env: &common-env
  env_file: .env
  environment:
    DATABASE_URL: postgresql+asyncpg://emailuser:emailpass@postgres:5432/emaildb
    REDIS_URL: redis://redis:6379/0
    CELERY_BROKER_URL: redis://redis:6379/0
    CELERY_RESULT_BACKEND: redis://redis:6379/1
    MINIO_ENDPOINT: minio:9000

x-worker-base: &worker-base
  build:
    context: .
    dockerfile: docker/Dockerfile.worker
  <<: *common-env
  depends_on:
    postgres:
      condition: service_healthy
    redis:
      condition: service_healthy
  restart: unless-stopped

services:
  # ─── Infrastructure ──────────────────────────────────────────────────────────
  postgres:
    image: ankane/pgvector:latest
    environment:
      POSTGRES_USER: emailuser
      POSTGRES_PASSWORD: emailpass
      POSTGRES_DB: emaildb
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U emailuser -d emaildb"]
      interval: 10s
      timeout: 5s
      retries: 5

  redis:
    image: redis:7-alpine
    command: redis-server --save 60 1 --loglevel warning
    volumes:
      - redis_data:/data
    ports:
      - "6379:6379"
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 5

  minio:
    image: minio/minio:latest
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin
    volumes:
      - minio_data:/data
    ports:
      - "9000:9000"
      - "9001:9001"
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:9000/minio/health/live"]
      interval: 30s
      timeout: 10s
      retries: 3

  # ─── Database migrations ─────────────────────────────────────────────────────
  migrate:
    build:
      context: .
      dockerfile: docker/Dockerfile.api
    <<: *common-env
    command: alembic upgrade head
    depends_on:
      postgres:
        condition: service_healthy
    restart: "no"

  # ─── API ─────────────────────────────────────────────────────────────────────
  api:
    build:
      context: .
      dockerfile: docker/Dockerfile.api
    <<: *common-env
    ports:
      - "8000:8000"
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      migrate:
        condition: service_completed_successfully
    restart: unless-stopped
    deploy:
      replicas: 2

  # ─── Workers ─────────────────────────────────────────────────────────────────
  ingestion_worker:
    <<: *worker-base
    environment:
      WORKER_TYPE: ingestion
      INGESTION_WORKER_CONCURRENCY: 4
    deploy:
      replicas: 3

  ai_worker:
    <<: *worker-base
    environment:
      WORKER_TYPE: ai
      AI_WORKER_CONCURRENCY: 2
    deploy:
      replicas: 2

  beat:
    <<: *worker-base
    environment:
      WORKER_TYPE: beat
    deploy:
      replicas: 1   # beat must run as a singleton

  # ─── Reverse Proxy ───────────────────────────────────────────────────────────
  nginx:
    image: nginx:alpine
    volumes:
      - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - "80:80"
    depends_on:
      - api
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:
  minio_data:
