services:
  frontend:
    build:
      context: .
      dockerfile: frontend/Dockerfile
    container_name: law-frontend
    restart: unless-stopped
    depends_on:
      backend:
        condition: service_healthy
      chat-service:
        condition: service_healthy
      email-service:
        condition: service_healthy
      minio:
        condition: service_started
    healthcheck:
      test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1/health >/dev/null 2>&1 && wget -q -O - http://127.0.0.1/chat-health >/dev/null 2>&1 && wget -q -O - http://127.0.0.1/email-health >/dev/null 2>&1"]
      interval: 20s
      timeout: 5s
      retries: 5
      start_period: 20s

  e2e:
    build:
      context: .
      dockerfile: e2e/Dockerfile
    image: law-e2e-playwright:1.58.2
    container_name: law-e2e
    working_dir: /src/e2e
    depends_on:
      frontend:
        condition: service_healthy
    volumes:
      - .:/src
      - /src/e2e/node_modules
    environment:
      NODE_PATH: /opt/e2e/node_modules
      PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: "1"
      E2E_BASE_URL: http://frontend

  backend:
    build: .
    container_name: law-backend
    restart: unless-stopped
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_started
      email-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8000/health', timeout=3)\""]
      interval: 20s
      timeout: 5s
      retries: 5
      start_period: 25s
    volumes: [".:/app"]

  chat-service:
    build: .
    container_name: law-chat-service
    restart: unless-stopped
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
    command: ["uvicorn", "app.chat_main:app", "--host", "0.0.0.0", "--port", "8001"]
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8001/health', timeout=3)\""]
      interval: 20s
      timeout: 5s
      retries: 5
      start_period: 25s
    volumes: [".:/app"]

  email-service:
    build: .
    container_name: law-email-service
    restart: unless-stopped
    env_file: .env
    depends_on:
      redis:
        condition: service_healthy
    command: ["uvicorn", "app.email_main:app", "--host", "0.0.0.0", "--port", "8010"]
    healthcheck:
      test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://localhost:8010/health', timeout=3)\""]
      interval: 20s
      timeout: 5s
      retries: 5
      start_period: 25s
    volumes: [".:/app"]

  worker:
    build: .
    container_name: law-worker
    restart: unless-stopped
    env_file: .env
    depends_on:
      db:
        condition: service_healthy
      redis:
        condition: service_healthy
      minio:
        condition: service_started
      clamav:
        condition: service_started
    command: ["celery","-A","app.workers.celery_app:celery_app","worker","-Q","notifications,maintenance,uploads","-l","INFO"]
    volumes: [".:/app"]

  beat:
    build: .
    container_name: law-beat
    restart: unless-stopped
    env_file: .env
    depends_on:
      redis:
        condition: service_healthy
    command: ["celery","-A","app.workers.celery_app:celery_app","beat","-l","INFO"]
    volumes: [".:/app"]

  security-scheduler:
    build: .
    container_name: law-security-scheduler
    restart: unless-stopped
    env_file: .env
    depends_on:
      frontend:
        condition: service_healthy
      backend:
        condition: service_healthy
      chat-service:
        condition: service_healthy
      email-service:
        condition: service_healthy
    command: ["bash", "-lc", "./scripts/ops/security_scheduler.sh"]
    healthcheck:
      test: ["CMD-SHELL", "test -f /tmp/security_scheduler_heartbeat && [ $(( $(date +%s) - $(cat /tmp/security_scheduler_heartbeat) )) -lt 1800 ]"]
      interval: 60s
      timeout: 5s
      retries: 3
      start_period: 60s
    volumes:
      - .:/app

  db:
    image: postgres:16
    container_name: law-db
    restart: unless-stopped
    environment:
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-postgres}
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_DB: ${POSTGRES_DB:-legal}
    volumes: ["pgdata:/var/lib/postgresql/data"]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres -d legal"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 15s

  redis:
    image: redis:7
    container_name: law-redis
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 10s

  minio:
    image: minio/minio:latest
    container_name: law-minio
    restart: unless-stopped
    command: server /data --console-address ":9001"
    environment:
      MINIO_ROOT_USER: ${MINIO_ROOT_USER:-minio_local_admin}
      MINIO_ROOT_PASSWORD: ${MINIO_ROOT_PASSWORD:-minio_local_password_change_me}
    volumes: ["miniodata:/data"]

  clamav:
    image: clamav/clamav:latest
    container_name: law-clamav
    restart: unless-stopped

volumes:
  pgdata:
  miniodata:
