# See https://testdriven.io/blog/docker-best-practices/ for recommendations
# on writing dockerfiles for python projects.

# We use a build stage to build wheels which we then copy and install
# in the second stage to minimize image size. This is mostly needed
# because setuptools_scm needs the full version info from git and git
# itself but including that in the final image would bloat its size.

# we are using the official python image with just a version tag here
# it comes with many tools needed to build and compile python packages
# which increases it's size but is helpful for building
FROM harbor.cta-observatory.org/proxy_cache/python:3.12 AS builder
SHELL ["/bin/bash", "-c"]

WORKDIR /repo

ARG SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CTAO_QUALPIPE_WEBAPP=0.0.0
ENV SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CTAO_QUALPIPE_WEBAPP=$SETUPTOOLS_SCM_PRETEND_VERSION_FOR_CTAO_QUALPIPE_WEBAPP

# Add requirements to build dependency wheels
ARG REQUIREMENTS=src/qualpipe_webapp/frontend/requirements.txt
COPY ${REQUIREMENTS} /repo/${REQUIREMENTS}
# Copy source to run codegen during build
COPY . /repo/

# build the application wheel and dependency wheels
# hadolint ignore=DL3008
RUN apt-get update && apt-get install -y --no-install-recommends git \
    && git config --global submodule.aiv-toolkit.update none \
    && python -m pip wheel --no-cache-dir --wheel-dir ./dist -r ${REQUIREMENTS} \
    && python -m pip install --no-cache-dir --no-index --find-links ./dist -r ${REQUIREMENTS} \
    && python -m pip install --no-cache-dir --no-deps /repo/ \
    && rm -rf /var/lib/apt/lists/*

# Install libsass wheel and compile SCSS to CSS at build time
COPY src/qualpipe_webapp/frontend/static/css /repo/static/css
# hadolint ignore=DL3013
RUN python -m pip install --no-cache-dir --no-index --find-links ./dist libsass \
    && python -c "import sass; sass.compile(dirname=('/repo/static/css', '/repo/static/css'))"

# second stage, copy and install wheels
# We are using the official python 3.12 image
# as base image in the slim variant to reduce image size.
FROM harbor.cta-observatory.org/proxy_cache/python:3.12-slim
COPY --from=builder /repo/dist /tmp/dist

# Install all wheels (application and dependencies)
# Since the builder stage already resolved and built all dependencies (including from git),
# we can use --no-deps to install all wheels and prevent pip from parsing git URLs in package metadata.
RUN python -m pip install --no-cache-dir --no-deps /tmp/dist/*.whl \
    && rm -rf /tmp/dist \
    && addgroup --system ctao \
    && adduser --system --group ctao

USER ctao

WORKDIR /app
# Copy application code
COPY --chown=root:root --chmod=755 src/qualpipe_webapp/frontend /app/frontend/
# Overwrite with generated frontend schemas from builder stage
COPY --from=builder --chown=root:root --chmod=755 /repo/src/qualpipe_webapp/frontend/static /app/frontend/static
# Overwrite with compiled CSS from builder stage (must be last)
COPY --from=builder --chown=root:root --chmod=755 /repo/static/css /app/frontend/static/css

ENV PORT=8001
CMD ["sh", "-c", "python -m uvicorn frontend.main:app --host 0.0.0.0 --port $PORT --reload"]
