# Phase 4 stack: API + Celery worker + Redis (broker/result backend) +
# Postgres (job state). Object storage is intentionally NOT here — the
# `BlobStorage` interface uses the local filesystem mounted at /app/storage.
services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    image: ocr-sprint-service:dev
    container_name: ocr-sprint-api
    command:
      [
        "sh",
        "-c",
        "alembic upgrade head && uvicorn ocr_sprint.main:app --host 0.0.0.0 --port 8000",
      ]
    ports:
      - "8000:8000"
    environment:
      APP_ENV: docker
      APP_LOG_LEVEL: INFO
      OCR_USE_GPU: "false"
      STORAGE_LOCAL_DIR: /app/storage
      BLOB_STORAGE_DIR: /app/storage/blobs
      REDIS_URL: redis://redis:6379/0
      DATABASE_URL: postgresql+psycopg://ocr:ocr@postgres:5432/ocr_sprint
      QUEUE_ENABLED: "true"
    volumes:
      - blob-storage:/app/storage/blobs
      - paddle-models:/home/app/.paddleocr
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
    restart: unless-stopped

  worker:
    image: ocr-sprint-service:dev
    container_name: ocr-sprint-worker
    command: ["celery", "-A", "ocr_sprint.worker.celery_app", "worker", "-l", "info", "--concurrency=1"]
    environment:
      APP_ENV: docker
      APP_LOG_LEVEL: INFO
      OCR_USE_GPU: "false"
      BLOB_STORAGE_DIR: /app/storage/blobs
      REDIS_URL: redis://redis:6379/0
      DATABASE_URL: postgresql+psycopg://ocr:ocr@postgres:5432/ocr_sprint
    volumes:
      - blob-storage:/app/storage/blobs
      - paddle-models:/home/app/.paddleocr
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_healthy
      api:
        condition: service_started
    restart: unless-stopped

  redis:
    image: redis:7-alpine
    container_name: ocr-sprint-redis
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 5s
      timeout: 3s
      retries: 5
    restart: unless-stopped

  postgres:
    image: postgres:16-alpine
    container_name: ocr-sprint-postgres
    environment:
      POSTGRES_USER: ocr
      POSTGRES_PASSWORD: ocr
      POSTGRES_DB: ocr_sprint
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ocr -d ocr_sprint"]
      interval: 5s
      timeout: 3s
      retries: 10
    restart: unless-stopped

volumes:
  blob-storage:
  paddle-models:
  postgres-data:
