# initial stage, build wheel
FROM harbor.cta-observatory.org/proxy_cache/python:3.12 AS builder

# install git for setuptools_scm
RUN apt update \
    && apt install -y --no-install-recommends git \
    && rm -rf /var/lib/apt/lists/*

# add necessary sources, including .git for version info
COPY ./pyproject.toml MANIFEST.in /repo/
COPY ./src ./repo/src/
COPY ./.git ./repo/.git/

# build the wheel
RUN python -m pip install --no-cache-dir build \
    && cd repo \
    && python -m build --wheel


# second stage, copy and install wheel
FROM harbor.cta-observatory.org/proxy_cache/python:3.12-slim
COPY --from=builder /repo/dist /tmp/dist

RUN apt update \
    && apt install -y --no-install-recommends git \
    && rm -rf /var/lib/apt/lists/* \
    && python -m pip install --no-cache-dir /tmp/dist/* \
    && rm -r /tmp/dist

RUN addgroup --system ctao && adduser --system --group ctao
USER ctao
