services:
  backend:
    image: ${OCR_BACKEND_IMAGE:-ocr-platform-backend:latest}
    build:
      context: ../backend
    container_name: ocr-platform-backend
    restart: unless-stopped
    environment:
      SPRING_DATASOURCE_URL: ${SPRING_DATASOURCE_URL:-jdbc:mysql://${MYSQL_HOST:-192.168.1.222}:${MYSQL_PORT:-3306}/${MYSQL_DATABASE:-ocrplatform}?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true}
      SPRING_DATASOURCE_USERNAME: ${SPRING_DATASOURCE_USERNAME:-${MYSQL_USER:-ocrplatform_app}}
      SPRING_DATASOURCE_PASSWORD: ${SPRING_DATASOURCE_PASSWORD:-${MYSQL_PASSWORD:-change-me}}
      OCR_STORAGE_ROOT: ${OCR_STORAGE_ROOT:-/data/ocr-platform/documents}
      OCR_ENGINE_DEFAULT_CODE: ${OCR_ENGINE_DEFAULT_CODE:-paddle-layout}
      PADDLE_LAYOUT_ENABLED: ${PADDLE_LAYOUT_ENABLED:-false}
      PADDLE_LAYOUT_ENDPOINT: ${PADDLE_LAYOUT_ENDPOINT:-http://192.168.1.13:18080/layout-parsing}
      PADDLE_LAYOUT_TIMEOUT: ${PADDLE_LAYOUT_TIMEOUT:-60s}
      PP_CHAT_OCR_ENABLED: ${PP_CHAT_OCR_ENABLED:-false}
      PP_CHAT_OCR_VISUAL_ENDPOINT: ${PP_CHAT_OCR_VISUAL_ENDPOINT:-http://192.168.1.13:18083/chatocr-visual}
      PP_CHAT_OCR_CHAT_ENDPOINT: ${PP_CHAT_OCR_CHAT_ENDPOINT:-http://192.168.1.13:18083/chatocr-chat}
      PP_CHAT_OCR_TIMEOUT: ${PP_CHAT_OCR_TIMEOUT:-120s}
      PP_CHAT_OCR_EXTRACTION_KEYS: ${PP_CHAT_OCR_EXTRACTION_KEYS:-}
      OCR_PLUGIN_API_ENABLED: ${OCR_PLUGIN_API_ENABLED:-true}
      OCR_PLUGIN_CLIENT_CODE_HEADER: ${OCR_PLUGIN_CLIENT_CODE_HEADER:-X-API-CLIENT-CODE}
      OCR_PLUGIN_CLIENT_SECRET_HEADER: ${OCR_PLUGIN_CLIENT_SECRET_HEADER:-X-API-CLIENT-SECRET}
      OCR_PLUGIN_RATE_LIMIT_PER_MINUTE: ${OCR_PLUGIN_RATE_LIMIT_PER_MINUTE:-60}
      OCR_PLUGIN_UNAUTH_RATE_LIMIT_PER_MINUTE: ${OCR_PLUGIN_UNAUTH_RATE_LIMIT_PER_MINUTE:-20}
      OCR_PLUGIN_AUDIT_ENABLED: ${OCR_PLUGIN_AUDIT_ENABLED:-true}
      OCR_LOG_FILE: ${OCR_LOG_FILE:-/var/log/ocr-platform/backend.log}
      OCR_LOG_FILE_HISTORY: ${OCR_LOG_FILE_HISTORY:-14}
    ports:
      - "18090:18090"
    volumes:
      - ocr-platform-documents:/data/ocr-platform/documents
      - ocr-platform-backend-logs:/var/log/ocr-platform
    networks:
      - ocr-platform
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "14"

  frontend:
    image: ${OCR_FRONTEND_IMAGE:-ocr-platform-frontend:latest}
    build:
      context: ../frontend
    container_name: ocr-platform-frontend
    restart: unless-stopped
    expose:
      - "80"
    networks:
      - ocr-platform
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "14"

  gateway:
    image: nginx:1.27-alpine
    container_name: ocr-platform-gateway
    restart: unless-stopped
    depends_on:
      - backend
      - frontend
    ports:
      - "18091:80"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./nginx/logs:/var/log/nginx
    networks:
      - ocr-platform
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "14"

  prometheus:
    image: prom/prometheus:v2.54.1
    container_name: ocr-platform-prometheus
    restart: unless-stopped
    command:
      - --config.file=/etc/prometheus/prometheus.yml
      - --storage.tsdb.path=/prometheus
      - --storage.tsdb.retention.time=14d
      - --web.enable-lifecycle
    ports:
      - "19090:9090"
    volumes:
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml:ro
      - ocr-platform-prometheus-data:/prometheus
    networks:
      - ocr-platform
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "7"

  grafana:
    image: grafana/grafana:11.2.0
    container_name: ocr-platform-grafana
    restart: unless-stopped
    environment:
      GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER:-admin}
      GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD:-ocr-admin}
      GF_USERS_ALLOW_SIGN_UP: "false"
    ports:
      - "13000:3000"
    volumes:
      - ocr-platform-grafana-data:/var/lib/grafana
      - ./grafana/datasources:/etc/grafana/provisioning/datasources:ro
      - ./grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
    depends_on:
      - prometheus
    networks:
      - ocr-platform
    logging:
      driver: json-file
      options:
        max-size: "20m"
        max-file: "7"

volumes:
  ocr-platform-documents:
  ocr-platform-backend-logs:
  ocr-platform-prometheus-data:
  ocr-platform-grafana-data:
  ocr-platform-proxy:

networks:
  ocr-platform:
    name: ocr-platform-prod
