ARG NODE_VERSION=24
ARG PYTHON_VERSION=3.14-slim

#############
# build api #
#############

ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS build-api

# env setup
WORKDIR /usr/local/src/advent22_api
ENV \
    PATH="/root/.local/bin:${PATH}"

# install poetry with export plugin
RUN set -ex; \
    \
    python -m pip --no-cache-dir install --upgrade pip wheel; \
    \
    apt-get update; apt-get install --yes --no-install-recommends \
      curl \
    ; rm -rf /var/lib/apt/lists/*; \
    \
    curl -sSL https://install.python-poetry.org | python3 -; \
    poetry self add poetry-plugin-export;

# build dependency wheels
COPY api/pyproject.toml api/poetry.lock ./
RUN set -ex; \
    \
    # # buildtime dependencies
    # apt-get update; apt-get install --yes --no-install-recommends \
    #   build-essential \
    # ; rm -rf /var/lib/apt/lists/*; \
    \
    # generate requirements.txt
    poetry export \
      --format requirements.txt \
      --output requirements.txt; \
    \
    python3 -m pip --no-cache-dir wheel \
      --wheel-dir ./dist \
      --requirement requirements.txt;

# build advent22_api wheel
COPY api ./
RUN poetry build --format wheel --output ./dist

############
# build ui #
############

ARG NODE_VERSION
FROM node:${NODE_VERSION} AS build-ui

# env setup
WORKDIR /usr/local/src/advent22_ui

# install advent22_ui dependencies
COPY ui/package*.json ui/yarn*.lock ./
RUN set -ex; \
    corepack enable; \
    yarn install;

# copy and build advent22_ui
COPY ui ./
RUN set -ex; \
    yarn dlx update-browserslist-db@latest; \
    yarn build --dest /tmp/advent22_ui/html; \
    # exclude webpack-bundle-analyzer output
    rm -f /tmp/advent22_ui/html/report.html;

######################
# python preparation #
######################

ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS uvicorn-gunicorn

# where credit is due ...
LABEL maintainer="Sebastián Ramirez <tiangolo@gmail.com>"
WORKDIR /usr/local/share/uvicorn-gunicorn

# install uvicorn-gunicorn
COPY ./scripts/mini-tiangolo ./

RUN set -ex; \
  chmod +x start.sh; \
  python3 -m pip --no-cache-dir install gunicorn;

CMD ["/usr/local/share/uvicorn-gunicorn/start.sh"]

###########
# web app #
###########

FROM uvicorn-gunicorn AS production

# env setup
ENV \  
    PRODUCTION_MODE="true" \
    PORT="8000" \
    MODULE_NAME="advent22_api.app"
EXPOSE 8000

WORKDIR /opt/advent22
VOLUME [ "/opt/advent22" ]

COPY --from=build-api /usr/local/src/advent22_api/dist /usr/local/share/advent22_api.dist
RUN set -ex; \
    # remove example app
    rm -rf /app; \
    \
    # # runtime dependencies
    # apt-get update; apt-get install --yes --no-install-recommends \
    # ; rm -rf /var/lib/apt/lists/*; \
    \
    # install advent22_api wheels
    python3 -m pip --no-cache-dir install --no-deps /usr/local/share/advent22_api.dist/*.whl; \
    \
    # prepare data directory
    chown nobody:nogroup ./

# add prepared advent22_ui
COPY --from=build-ui /tmp/advent22_ui /usr/local/share/advent22_ui

# run as unprivileged user
USER nobody
