FROM {{ base_image }}

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
    python3-full \
    python3-pip \
    && rm -rf /var/lib/apt/lists/*

# Set the working directory to /geneva, where the code will be placed
WORKDIR /geneva

# Setup UV to use local caching, which is outside of the build context
# so that it can be shared between builds. So we need to copy.
RUN pip install --no-cache-dir uv || pip install --no-cache-dir uv --break-system-packages
ENV UV_LINK_MODE=copy

RUN uv venv create --python=3.12

# Install dependencies -- this is done first so that the cache can be used
# if the code changes
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    --mount=type=bind,source=wheelhouse,target=wheelhouse \
    uv sync --frozen --no-install-project --no-editable --extra dataflow --python=3.12

# activate the virtual environment
ENV PATH=/geneva/.venv/bin:$PATH
# Set the environment variable to run the Python SDK in the default environment.
# BEAM will try to create a new environment if this is not set.
ENV RUN_PYTHON_SDK_IN_DEFAULT_ENVIRONMENT=1

# Copy files from official SDK image, including script/dependencies.
COPY --from=apache/beam_python3.12_sdk:2.62.0 /opt/apache/beam /opt/apache/beam

# *** DO NOT COPY CODE UNTILL DEPENDENCIES ARE INSTALLED ***
# so changing code does not invalidate the cache
COPY . /geneva

# finally sync the local code
RUN --mount=type=bind,source=uv.lock,target=uv.lock \
    uv sync --frozen --no-editable --extra dataflow --python=3.12

{{ post_install_command }}

# Set the entrypoint to Apache Beam SDK launcher.
ENTRYPOINT ["uv", "run", "--python=3.12", "/opt/apache/beam/boot"]
