services:
  # ========================
  # Backend Service
  # ========================
  backend:
    build:
      context: ./backend
      dockerfile: Dockerfile
    container_name: picanalysis-backend
    restart: unless-stopped
    environment:
      NODE_ENV: production
      DATABASE_URL: file:./prod.db
      PORT: 13057
      CORS_ORIGIN: http://localhost:13056
      JWT_SECRET: ${JWT_SECRET}
      JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-24h}
      OCR_PROVIDER: ${OCR_PROVIDER:-tesseract}
      OCR_CONFIDENCE_THRESHOLD: ${OCR_CONFIDENCE_THRESHOLD:-0.3}
      BAIDU_OCR_API_KEY: ${BAIDU_OCR_API_KEY:-}
      BAIDU_OCR_SECRET_KEY: ${BAIDU_OCR_SECRET_KEY:-}
      GLM_API_KEY: ${GLM_API_KEY:-}
      GLM_API_URL: ${GLM_API_URL:-https://open.bigmodel.cn/api/paas/v4/chat/completions}
      GLM_MODEL: ${GLM_MODEL:-glm-4-flash}
      MINIMAX_API_KEY: ${MINIMAX_API_KEY:-}
      DEEPSEEK_API_KEY: ${DEEPSEEK_API_KEY:-}
      UPLOAD_MAX_SIZE: ${UPLOAD_MAX_SIZE:-10485760}
      UPLOAD_ALLOWED_TYPES: ${UPLOAD_ALLOWED_TYPES:-image/jpeg,image/png,image/webp}
      # OCR Services URLs
      RAPIDOCR_API_URL: ${RAPIDOCR_API_URL:-http://rapidocr:9004}
      PADDLEOCR_API_URL: ${PADDLEOCR_API_URL:-http://paddleocr:8866}
    volumes:
      # Persist database and uploads
      - backend-data:/app/data
      - backend-uploads:/app/uploads
    networks:
      - picanalysis-network
    depends_on:
      - rapidocr
      - paddleocr
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:13057/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # ========================
  # RapidOCR Service (本地快速 OCR)
  # ========================
  rapidocr:
    image: volador/rapidocr:latest
    container_name: picanalysis-rapidocr
    restart: unless-stopped
    ports:
      - "13058:9004"
    networks:
      - picanalysis-network
    healthcheck:
      test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:9004"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 20s

  # ========================
  # PaddleOCR Service (本地高精度 OCR - 使用官方预构建镜像)
  # ========================
  paddleocr:
    image: 987846/paddleocr:latest
    container_name: picanalysis-paddleocr
    restart: unless-stopped
    ports:
      - "13059:8866"
    environment:
      # 修复 protobuf 兼容性问题
      PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION: python
    networks:
      - picanalysis-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8866/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # ========================
  # Frontend Service
  # ========================
  frontend:
    build:
      context: ./frontend
      dockerfile: Dockerfile
    container_name: picanalysis-frontend
    restart: unless-stopped
    ports:
      - "${FRONTEND_PORT:-13056}:80"
    networks:
      - picanalysis-network
    depends_on:
      - backend

# ========================
# Volumes
# ========================
volumes:
  backend-data:
    driver: local
  backend-uploads:
    driver: local

# ========================
# Networks
# ========================
networks:
  picanalysis-network:
    driver: bridge
