# The envlib-api image: the refresher (`envlib-api refresher`) and the API (`envlib-api-server`, as the
# service's entrypoint): one image, a service per command.
# Installs from uv.lock (--frozen), so the image runs exactly the locked stack, all from PyPI. To develop
# against an unreleased ebooklet, add a [tool.uv.sources] path entry locally, and remove it and re-lock
# (uv lock --upgrade-package ebooklet --refresh-package ebooklet) before building.
# The healthcheck is set per service in docker-swarm.yml (it differs between the refresher and the API).
#
# The docs stage builds the docs site from the same commit, so the API serves the docs of exactly the
# code it runs, at /docs/ (plan EDR plan 7). Its Reference page is generated from the route specs,
# so the stage installs the package with the `docs` group (and not the dev group).
FROM python:3.12-slim AS docs

COPY --from=ghcr.io/astral-sh/uv:0.8.17 /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock README.md CHANGELOG.md mkdocs.yml ./
COPY envlib_api ./envlib_api
COPY docs ./docs
COPY docs_hooks ./docs_hooks
RUN uv sync --frozen --no-dev --group docs && uv run --no-sync mkdocs build --strict --site-dir /app/site-build

FROM python:3.12-slim

COPY --from=ghcr.io/astral-sh/uv:0.8.17 /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock README.md ./
COPY envlib_api ./envlib_api
RUN uv sync --frozen --no-dev && rm -rf /root/.cache
COPY --from=docs /app/site-build /app/site

RUN groupadd --gid 1000 appgroup && \
    useradd --uid 1000 --gid appgroup --shell /bin/bash --no-create-home appuser

USER appuser
ENV PYTHONUNBUFFERED=1 PATH="/app/.venv/bin:$PATH"

ENTRYPOINT ["envlib-api"]
CMD ["refresher"]
