FROM docker.io/library/node:24-bookworm-slim

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
       bash ca-certificates curl git ripgrep fd-find less procps python3 python-is-python3 \
       jq file tree unzip xz-utils \
  && ln -s /usr/bin/fdfind /usr/local/bin/fd \
  && rm -rf /var/lib/apt/lists/*

# Pinned to the host's pi version by the containerize skill.
ARG PI_VERSION=latest
RUN npm install -g --ignore-scripts @earendil-works/pi-coding-agent@${PI_VERSION}

# --- uv (Python) ---
COPY --from=ghcr.io/astral-sh/uv:0.13 /uv /uvx /usr/local/bin/
ENV UV_PYTHON_INSTALL_DIR=/opt/uv-python \
    UV_PROJECT_ENVIRONMENT=/opt/venv \
    UV_CACHE_DIR=/opt/uv-cache \
    UV_LINK_MODE=copy \
    VIRTUAL_ENV=/opt/venv \
    PATH=/opt/venv/bin:$PATH
RUN uv python install 3.15 && uv venv --python 3.15 /opt/venv

# --- Rust + native build deps for dir-shuffle ---
# No C/C++ PDF engine is compiled here: PDF previews use pdfium-render, which loads
# a prebuilt libpdfium at runtime (see below). build-essential covers crates with small C parts.
RUN apt-get update \
  && apt-get install -y --no-install-recommends \
       build-essential pkg-config musl-tools \
  && rm -rf /var/lib/apt/lists/*
ENV RUSTUP_HOME=/opt/rustup \
    CARGO_HOME=/opt/cargo \
    CARGO_TARGET_DIR=/opt/cargo-target \
    PATH=/opt/cargo/bin:$PATH
RUN curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable \
       --component rustfmt,clippy \
  && rustup target add x86_64-unknown-linux-musl aarch64-unknown-linux-musl \
  && chmod -R a+rwX /opt/rustup /opt/cargo
# maturin for wheel builds (bindings = "bin")
ENV UV_TOOL_BIN_DIR=/usr/local/bin UV_TOOL_DIR=/opt/uv-tools
RUN uv tool install "maturin>=1.5,<2"

# --- Prebuilt PDFium (bblanchon/pdfium-binaries) for pdfium-render ---
# PDFIUM_BUILD must match the `pdfium_<N>` feature pinned in Cargo.toml (DESIGN.md §6.1).
ARG PDFIUM_BUILD=8125
RUN case "$(uname -m)" in \
       x86_64) arch=x64 ;; aarch64) arch=arm64 ;; *) echo "unsupported arch" >&2; exit 1 ;; \
     esac \
  && mkdir -p /opt/pdfium \
  && curl -fsSL "https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${PDFIUM_BUILD}/pdfium-linux-${arch}.tgz" \
     | tar xz -C /opt/pdfium \
  && test -f /opt/pdfium/lib/libpdfium.so
# dir-shuffle looks here for libpdfium (DESIGN.md §6.1); LD_LIBRARY_PATH covers tests/examples.
ENV DIR_SHUFFLE_PDFIUM_DIR=/opt/pdfium/lib \
    LD_LIBRARY_PATH=/opt/pdfium/lib

WORKDIR /workdir
ENTRYPOINT ["pi"]
