FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    UV_LINK_MODE=copy \
    PLAYWRIGHT_BROWSERS_PATH=/ms-playwright \
    API_EVERYTHING__SERVER__HOST=0.0.0.0 \
    API_EVERYTHING__SERVER__PORT=8750 \
    API_EVERYTHING__BROWSER=chromium

WORKDIR /app

# Install Python dependencies first so BuildKit caches them across rebuilds.
COPY pyproject.toml uv.lock README.md LICENSE ./
COPY src ./src

RUN uv sync --frozen --no-dev --no-editable \
    && rm -rf /root/.cache

# Playwright's bundled node driver requires libatomic1 before --with-deps can run.
RUN apt-get update \
    && apt-get install -y --no-install-recommends libatomic1 \
    && .venv/bin/playwright install --with-deps --only-shell chromium \
    && rm -rf /var/lib/apt/lists/* /root/.cache

# Run as an unprivileged user; Chromium and /app remain readable.
RUN useradd --create-home --shell /bin/bash api \
    && chown -R api:api /app /ms-playwright
USER api

EXPOSE 8750

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD [".venv/bin/python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8750/healthz')"]

ENTRYPOINT ["/app/.venv/bin/api-everything"]
CMD ["serve"]
