FROM python:3.13.12-bookworm AS base

# Install non-Python dependencies
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update --yes \
    && apt-get upgrade --yes \
    && apt-get install --yes --no-install-recommends \
        libnetcdf-c++4-dev \
        libgdal-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Update pip and build tools for potential bug fixes and performance improvements
RUN pip install --no-cache-dir --root-user-action=ignore --upgrade pip build setuptools wheel

# Create a non-root user to run as
RUN useradd --create-home --home-dir "/home/pipuser" --user-group --uid 1000 pipuser
USER pipuser:pipuser
ENV PATH=/home/pipuser/.local/bin:$PATH
WORKDIR /home/pipuser/

# Build stage
FROM base AS build

COPY --chown=pipuser:pipuser . /home/pipuser/l2o-gpp

# Install pinned runtime dependencies for reproducible environment
RUN pip install --user --no-cache-dir -r /home/pipuser/l2o-gpp/requirements.txt

# Install l2o-gpp from source
RUN pip install --user --no-cache-dir --no-deps "/home/pipuser/l2o-gpp[all]"

# Final stage
FROM base AS final

# Copy installed Python packages into the final image
COPY --from=build --chown=pipuser:pipuser /home/pipuser/.local /home/pipuser/.local

# OCI annotations
# See https://github.com/opencontainers/image-spec/blob/main/annotations.md#pre-defined-annotation-keys
ARG CI_COMMIT_SHA
LABEL org.opencontainers.image.title="ESA RL2Ocean Ground Prototype Processor"
LABEL org.opencontainers.image.description="ESA ROSE-L Level-2 Ocean Ground Prototype Processor"
LABEL org.opencontainers.image.source="https://gitlab.dlr.de/rl2ocean/l2o-gpp"
LABEL org.opencontainers.image.url="https://gitlab.dlr.de/rl2ocean/l2o-gpp/-/blob/main/Dockerfile"
LABEL org.opencontainers.image.revision="$CI_COMMIT_SHA"
LABEL org.opencontainers.image.vendor="ESA"
LABEL org.opencontainers.image.authors="RL2Ocean Team"
LABEL org.opencontainers.image.base.name="python:3.13.12-bookworm"
