ARG BASE_IMAGE=debian:stable-slim
FROM ${BASE_IMAGE}

ENV DEBIAN_FRONTEND=noninteractive \
    LANG=C.UTF-8 \
    LANGUAGE=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    UV_INSTALL_DIR=/usr/local/bin \
    UV_PYTHON_INSTALL_DIR=/opt/uv/python \
    UV_LINK_MODE=copy \
    UV_PROJECT_ENVIRONMENT=/workspace/.venv \
    PATH=/workspace/.venv/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/sbin:/bin

LABEL org.opencontainers.image.title="python-uv-base" \
      org.opencontainers.image.description="CNB base development image with Python, uv, zsh, and VS Code extensions"

SHELL ["/bin/bash", "-lc"]

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        bash \
        ca-certificates \
        build-essential \
        cmake \
        curl \
        git \
        ninja-build \
        openssh-server \
        pkg-config \
        unzip \
        wget \
        zsh \
    && rm -rf /var/lib/apt/lists/*

RUN set -eux; \
    curl -fsSL https://astral.sh/uv/install.sh -o /tmp/install-uv.sh; \
    chmod +x /tmp/install-uv.sh; \
    /tmp/install-uv.sh > /tmp/install-uv.log 2>&1 & \
    pid=$!; \
    while kill -0 "$pid" 2>/dev/null; do \
      echo "[heartbeat] installing uv..."; \
      sleep 20; \
    done; \
    wait "$pid"; \
    cat /tmp/install-uv.log; \
    rm -f /tmp/install-uv.sh /tmp/install-uv.log

RUN set -eux; \
    curl -fsSL https://code-server.dev/install.sh -o /tmp/install-code-server.sh; \
    chmod +x /tmp/install-code-server.sh; \
    /tmp/install-code-server.sh > /tmp/install-code-server.log 2>&1 & \
    pid=$!; \
    while kill -0 "$pid" 2>/dev/null; do \
      echo "[heartbeat] installing code-server..."; \
      sleep 20; \
    done; \
    wait "$pid"; \
    cat /tmp/install-code-server.log; \
    rm -f /tmp/install-code-server.sh /tmp/install-code-server.log; \
    code-server --install-extension cnbcool.cnb-welcome; \
    code-server --install-extension ms-python.python; \
    code-server --install-extension ms-python.debugpy

COPY .python-version /tmp/.python-version

RUN set -eux; \
    PYTHON_VERSION="$(cat /tmp/.python-version)"; \
    uv python install "${PYTHON_VERSION}" > /tmp/install-python.log 2>&1 & \
    pid=$!; \
    while kill -0 "$pid" 2>/dev/null; do \
      echo "[heartbeat] installing python ${PYTHON_VERSION} with uv..."; \
      sleep 20; \
    done; \
    wait "$pid"; \
    cat /tmp/install-python.log; \
    rm -f /tmp/install-python.log; \
    PYTHON_BIN="$(uv python find "${PYTHON_VERSION}")"; \
    ln -sf "${PYTHON_BIN}" /usr/local/bin/python; \
    ln -sf "${PYTHON_BIN}" /usr/local/bin/python3; \
    python --version; \
    uv --version

RUN cat <<'EOF' >/root/.zshrc
autoload -Uz compinit colors vcs_info
compinit
colors

setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt HIST_IGNORE_DUPS
setopt SHARE_HISTORY
setopt PROMPT_SUBST

zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
zstyle ':vcs_info:git:*' formats ' git:(%b)'

precmd() {
  vcs_info
}

PROMPT='%F{magenta}➜%f  %F{blue}%~%f%F{green}${vcs_info_msg_0_}%f %# '
EOF

RUN chsh -s /bin/zsh root

ENV SHELL=/bin/zsh \
    CNB_WELCOME_CMD='printf "\033[35mWelcome to CNB Python+uv base workspace\033[0m\n"; printf "workspace uv extra: %s\n" "${UV_EXTRA:-unset}"; printf "workspace torch variant: %s\n" "${TORCH_VARIANT:-unset}"; printf "Python: "; python --version; printf "uv: "; uv --version'

WORKDIR /workspace
