services:
  candle-annotator:
    build: .
    ports:
      - "3000:3000"
    volumes:
      - ml-data:/app/ml-data
    environment:
      - NODE_ENV=production
      - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      - INFERENCE_API_URL=http://ml-service:8001
      - INFERENCE_API_TIMEOUT=30000
      - INFERENCE_BATCH_TIMEOUT=120000
      - NEXT_PUBLIC_PREDICTIONS_ENABLED=true
      - API_KEY=${API_KEY}
      - AUTH_SECRET=${AUTH_SECRET}
      - AUTH_GOOGLE_ID=${AUTH_GOOGLE_ID}
      - AUTH_GOOGLE_SECRET=${AUTH_GOOGLE_SECRET}
      - AUTH_TRUST_HOST=${AUTH_TRUST_HOST}
      - DEFAULT_ADMIN_EMAIL=${DEFAULT_ADMIN_EMAIL}
      - DEFAULT_ADMIN_PASSWORD=${DEFAULT_ADMIN_PASSWORD}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      ml-service:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000/api/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  ml-service:
    build: ./services/ml
    ports:
      - "127.0.0.1:8001:8001"
    volumes:
      - ml-data:/app/data
      - mlflow-data:/app/mlruns
    environment:
      - MLFLOW_TRACKING_URI=http://mlflow:5000
      - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@postgres:5432/${POSTGRES_DB}
      - PYTHONUNBUFFERED=1
      - API_KEY=${API_KEY}
    restart: unless-stopped
    depends_on:
      postgres:
        condition: service_healthy
      mlflow:
        condition: service_started
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8001/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  mlflow:
    image: ghcr.io/mlflow/mlflow:v2.10.0
    ports:
      - "127.0.0.1:5000:5000"
    volumes:
      - mlflow-data:/mlflow
    command: >
      mlflow server
      --backend-store-uri /mlflow
      --default-artifact-root /mlflow/artifacts
      --host 0.0.0.0
      --port 5000
    restart: unless-stopped

  postgres:
    image: postgres:16-alpine
    ports:
      - "127.0.0.1:5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  ml-data:
  mlflow-data:
  postgres-data:
