# ATS fidelity comparison image — Chromium PDF vs WeasyPrint PDF.
#
# Self-contained: bundles the project source + node_modules + both PDF
# engines + poppler. Build once, run anywhere. Output lands under
# scripts/ats-fidelity/build/ — copy it out via `docker cp` or a volume.
#
# Image size lands ~1.2-1.5 GB (Chromium ~350 MB, WeasyPrint + deps ~250 MB,
# fonts ~200 MB, Node slim + node_modules ~250 MB). One-time build cost; the
# TeX Live base image this replaces was ~1.5 GB on its own.
#
# Build:
#   docker build -f scripts/ats-fidelity/Dockerfile -t ats-fidelity:latest .
#
# Run (write results to a host directory):
#   docker run --rm -v "$PWD/scripts/ats-fidelity/build:/work/scripts/ats-fidelity/build" \
#     ats-fidelity:latest

FROM node:22-slim

# Avoid interactive apt prompts during font/TZ configuration.
ENV DEBIAN_FRONTEND=noninteractive

# System deps: Python + pip for WeasyPrint; poppler-utils for pdftotext;
# fonts-liberation (Arial/Times/Courier metric substitutes), fonts-noto +
# fonts-noto-cjk for Unicode coverage; Chromium runtime deps for Playwright.
RUN apt-get update && apt-get install -y --no-install-recommends \
      python3 python3-pip python3-venv \
      poppler-utils \
      fonts-liberation \
      fonts-noto fonts-noto-cjk \
      libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 \
      libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 \
      libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2 \
      libpangocairo-1.0-0 \
    && rm -rf /var/lib/apt/lists/*

# WeasyPrint via pip. Pin a known-good major version so the comparison is
# reproducible — WeasyPrint's PDF output evolves between releases.
RUN pip3 install --no-cache-dir --break-system-packages \
      weasyprint==63.0 \
 && weasyprint --version

WORKDIR /work

# Copy lockfile + install deps first (cache layer).
COPY package.json package-lock.json* pnpm-lock.yaml* ./
RUN npm ci || npm install

# Copy the rest of the source.
COPY . .

# Install the Chromium binary Playwright will drive. --with-deps installs any
# remaining OS packages the browser needs (the apt list above covers the
# common ones, but Playwright knows the exact set for its bundled build).
RUN npx playwright install chromium

# Default entrypoint: run the comparison. Override with `tsx <path>` to run
# a single piece (e.g. the renderer smoke test).
CMD ["npx", "tsx", "scripts/ats-fidelity.ts"]
