# Mobile UI Comparator — full build (pHash + SSIM + OCR + DINOv2-small)
# Expected image size: ~3.5 GB (torch + easyocr + dinov2-small weights)

FROM python:3.10-slim

# ── System libraries ──────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 \
        libglib2.0-0 \
        libgomp1 \
        curl \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# ── Core deps — pHash + SSIM pipeline ────────────────────────────────────────
RUN pip install --no-cache-dir \
        "imagehash>=4.3.1" \
        "scikit-image>=0.21.0" \
        "opencv-python-headless>=4.8.0" \
        "Pillow>=10.0.0" \
        "numpy>=1.24.0" \
        "click>=8.1.0" \
        "rich>=13.0.0" \
        "fastapi>=0.110.0" \
        "uvicorn[standard]>=0.29.0" \
        "python-multipart>=0.0.9" \
        "certifi"

# ── OCR — torch + EasyOCR ────────────────────────────────────────────────────
RUN pip install --no-cache-dir \
        "torch>=2.2.0,<2.3.0" \
        --extra-index-url https://download.pytorch.org/whl/cpu && \
    pip install --no-cache-dir "easyocr>=1.7.0"

# ── transformers (DINOv2) ─────────────────────────────────────────────────────
RUN pip install --no-cache-dir "transformers>=4.35.0"

# ── Application source ────────────────────────────────────────────────────────
COPY mobile_ui_comparator/ ./mobile_ui_comparator/

# ── Bake EasyOCR models into image ───────────────────────────────────────────
ENV EASYOCR_MODULE_PATH=/root/.EasyOCR

RUN python -c "import warnings; warnings.filterwarnings('ignore'); print('Caching EasyOCR English models ...'); import easyocr; easyocr.Reader(['en'], gpu=False, verbose=False); print('EasyOCR OK')"

# ── Bake DINOv2-small model into image ────────────────────────────────────────
ENV HF_HOME=/root/.cache/huggingface
ENV TRANSFORMERS_OFFLINE=0
# Disable HF xet protocol — forces standard HTTP download (avoids CAS decode errors)
ENV HF_HUB_DISABLE_XET=1

RUN python -c "import warnings; warnings.filterwarnings('ignore'); print('Caching DINOv2 facebook/dinov2-small ...'); from transformers import AutoImageProcessor, AutoModel; mid='facebook/dinov2-small'; AutoImageProcessor.from_pretrained(mid); AutoModel.from_pretrained(mid); print('DINOv2 OK')"

# Lock HF offline at runtime
ENV TRANSFORMERS_OFFLINE=1
ENV HF_HUB_OFFLINE=1
ENV TOKENIZERS_PARALLELISM=false

# ── Report storage ────────────────────────────────────────────────────────────
RUN mkdir -p /app/ui-diff-reports
VOLUME ["/app/ui-diff-reports"]

EXPOSE 8000

CMD ["uvicorn", "mobile_ui_comparator.server:app", \
     "--host", "0.0.0.0", \
     "--port", "8000", \
     "--workers", "1"]
