ARG BASE_IMAGE=node:22-bookworm
ARG UV_VERSION=0.12.7
FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv-runtime
FROM rust:1.85.1-bookworm AS rust-runtime
FROM ${BASE_IMAGE}

ARG CLAUDE_CODE_VERSION=2.1.227
ARG CODEX_VERSION=0.159.1
ARG OPENCODE_VERSION=1.18.14
ARG INSTALL_SYSTEM_DEPENDENCIES=1
ARG RUNTIME_DEVICE=cuda

COPY --from=uv-runtime /uv /uvx /usr/local/bin/
COPY --from=rust-runtime /usr/local/cargo /opt/citeark/cargo
COPY --from=rust-runtime /usr/local/rustup /opt/citeark/rustup

RUN if [ "${INSTALL_SYSTEM_DEPENDENCIES}" = "1" ]; then \
        apt-get update \
        && apt-get install -y --no-install-recommends \
        build-essential \
        ca-certificates \
        cmake \
        curl \
        ffmpeg \
        git \
        git-lfs \
        jq \
        libgl1 \
        libglib2.0-0 \
        libgomp1 \
        libsndfile1 \
        ninja-build \
        openssh-client \
        pkg-config \
        poppler-utils \
        python3 \
        python3-pip \
        python3-venv \
        rsync \
        tar \
        util-linux \
        unzip \
        wget \
        && rm -rf /var/lib/apt/lists/*; \
    fi \
    && npm install --global \
        "@anthropic-ai/claude-code@${CLAUDE_CODE_VERSION}" \
        "@openai/codex@${CODEX_VERSION}" \
        "opencode-ai@${OPENCODE_VERSION}" \
    && claude --version \
    && codex --version \
    && opencode --version

ENV DEBIAN_FRONTEND=noninteractive
ENV PIP_DISABLE_PIP_VERSION_CHECK=1
ENV PYTHONUNBUFFERED=1
ENV UV_PYTHON_INSTALL_DIR=/opt/citeark/managed-python
ENV UV_PYTHON_BIN_DIR=/usr/local/bin
ENV CARGO_HOME=/opt/citeark/cargo
ENV RUSTUP_HOME=/opt/citeark/rustup
ENV PATH=/opt/citeark/venv/bin:/opt/citeark/cargo/bin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV PIP_INDEX_URL=https://pypi.org/simple
ENV HF_ENDPOINT=https://huggingface.co

RUN uv python install 3.10 \
    && uv python install 3.12 --default \
    && uv --version \
    && python --version \
    && python3.10 --version \
    && python3.12 --version \
    && rustc --version \
    && cargo --version

COPY docker/claude-code/codex-pro-relay.mjs docker/claude-code/runtime-contract-check.mjs /opt/citeark/
COPY runtime/requirements-baseline.txt /opt/citeark/requirements-baseline.txt
COPY runtime/create-runtime-venv.sh /opt/citeark/create-runtime-venv.source.sh
COPY runtime/install-scientific-runtime.sh /opt/citeark/install-scientific-runtime.sh
COPY runtime/install-local-cpu-runtime.sh /opt/citeark/install-local-cpu-runtime.sh

RUN chmod 0555 /opt/citeark/install-scientific-runtime.sh \
    && if [ "$RUNTIME_DEVICE" = cpu ]; then \
       bash /opt/citeark/install-local-cpu-runtime.sh; \
       elif [ "$RUNTIME_DEVICE" = cuda ]; then \
       CITEARK_BASELINE_REQUIREMENTS=/opt/citeark/requirements-baseline.txt \
       CITEARK_RUNTIME_VENV_SOURCE=/opt/citeark/create-runtime-venv.source.sh \
       CITEARK_RUNTIME_PROFILE=gcp-scientific-runtime \
       CITEARK_PYTHON_PACKAGE_INDEX=https://pypi.org/simple \
       CITEARK_HUGGINGFACE_ENDPOINT=https://huggingface.co \
       /opt/citeark/install-scientific-runtime.sh; \
       else echo "RUNTIME_DEVICE must be cpu or cuda" >&2; exit 1; fi \
    && rm -f /opt/citeark/create-runtime-venv.source.sh

RUN node /opt/citeark/runtime-contract-check.mjs

WORKDIR /job/workspace/repository

CMD ["sh", "-lc", "claude --version && codex --version && opencode --version"]
