services:
    # Docling
    docling:
        # https://github.com/docling-project/docling-serve
        # 5001: Web UI + API
        # OCR: EasyOCR with English + Chinese Simplified + Traditional (auto-downloaded on first run)

        image: ghcr.io/docling-project/docling-serve-cpu:v1.13.0
        container_name: jt-docling
        restart: unless-stopped
        environment:
            DOCLING_SERVE_ENABLE_UI: "true"
        volumes:
            - docling_models:/opt/app-root/src/.cache/docling/models
        ports:
            - "5001:5001"
        entrypoint: ["/bin/sh", "-c"]
        command:
            - |
                # Download Chinese OCR models if missing (first run only)
                # Note: ch_sim and ch_tra are mutually exclusive, must download separately
                if [ ! -f /opt/app-root/src/.cache/docling/models/EasyOcr/zh_sim_g2.pth ]; then
                  echo "Downloading Simplified Chinese OCR model..."
                  python3 -c "import easyocr; easyocr.Reader(['en','ch_sim'], gpu=False)"
                  cp -n /opt/app-root/src/.EasyOCR/model/*.pth /opt/app-root/src/.cache/docling/models/EasyOcr/ 2>/dev/null || true
                fi
                if [ ! -f /opt/app-root/src/.cache/docling/models/EasyOcr/zh_tra_g2.pth ]; then
                  echo "Downloading Traditional Chinese OCR model..."
                  python3 -c "import easyocr; easyocr.Reader(['en','ch_tra'], gpu=False)"
                  cp -n /opt/app-root/src/.EasyOCR/model/*.pth /opt/app-root/src/.cache/docling/models/EasyOcr/ 2>/dev/null || true
                fi
                exec docling-serve run --host 0.0.0.0 --port 5001
        healthcheck:
            test: ["CMD", "curl", "-sf", "http://localhost:5001/health"]
            interval: 30s
            timeout: 10s
            retries: 5
            start_period: 500s

    # Ollama
    ollama:
        # https://hub.docker.com/r/ollama/ollama
        # 11434: API port
        # Model: qwen3:1.7b (auto-pulled on startup)

        image: ollama/ollama:0.9.3
        container_name: jt-ollama
        restart: unless-stopped
        volumes:
            - ollama_data:/root/.ollama
        ports:
            - "11434:11434"
        entrypoint: ["/bin/sh", "-c", "ollama serve & sleep 5 && ollama pull qwen3:1.7b && wait"]
        healthcheck:
            test: ["CMD", "ollama", "list"]
            interval: 30s
            timeout: 10s
            retries: 5
            start_period: 120s

    # Tools (Python FastAPI - Generic Excel API)
    tools:
        build: ./Tools/Service
        container_name: jt-tools
        restart: unless-stopped
        environment:
            DATA_ROOT: /data
        volumes:
            - /data/jingtian/BenjaminTeam:/data:rw
        ports:
            - "8000:8000"
        healthcheck:
            test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
            interval: 30s
            timeout: 10s
            retries: 3
            start_period: 10s

    # Tracker (Go Orchestrator - Document Processing Pipeline)
    tracker:
        build: ./Tracker
        container_name: jt-tracker
        restart: unless-stopped
        volumes:
            - /data/jingtian/BenjaminTeam:/data:rw
            - ./Tracker/config.yaml.example:/app/config.yaml:ro
        depends_on:
            docling:
                condition: service_healthy
            ollama:
                condition: service_healthy
            tools:
                condition: service_healthy
        healthcheck:
            test: ["CMD", "pgrep", "-f", "tracker"]
            interval: 30s
            timeout: 5s
            retries: 3
            start_period: 10s

volumes:
    docling_models:
        name: jt-docling-models
    ollama_data:
        name: jt-ollama-data
