# Two-stage build for the tidalpy documentation.
# Stage 1 renders the static site with Sphinx; stage 2 serves it with
# nginx. Deploy with Coolify (Dockerfile service, port 80).

ARG PYTHON_VERSION=3.13

FROM python:${PYTHON_VERSION}-slim AS build
# The version the footer shows (empty by default: the build then
# falls back to pyproject.toml, which the release flow aligns with
# the tag before tagging). Override per deploy
# (--build-arg TIDALPY_VERSION=0.1.0).
ARG TIDALPY_VERSION=
ENV TIDALPY_VERSION=${TIDALPY_VERSION}
# The short hash of the commit the site was built from, shown in
# the footer after the version. The build stage has no .git
# directory, so Coolify wires it from the webhook's CI_COMMIT_SHA
# (--build-arg GIT_COMMIT=...). When absent the footer shows the
# version only.
ARG GIT_COMMIT=
ENV GIT_COMMIT=${GIT_COMMIT}
WORKDIR /repo

# Mirror the repository layout (docs/ and src/ as siblings): conf.py
# resolves `../src` relative to itself for the autodoc imports.
COPY pyproject.toml README.md /repo/
COPY docs/ /repo/docs/
COPY src/ /repo/src/

# The project itself plus the doc tooling. Installing the package
# pulls the runtime dependencies exactly as pyproject.toml declares
# them — autodoc imports every module, so the docs build needs them
# (a hand-maintained mirror here had drifted: it missed pydantic and
# still installed the old sphinx-rtd-theme instead of shibuya).
RUN pip install --no-cache-dir \
    /repo \
    "sphinx>=7,<9" \
    "myst-parser>=3,<5" \
    "shibuya>=2026.7.12" \
    "sphinx-copybutton>=0.5,<1"

RUN TIDALPY_VERSION=${TIDALPY_VERSION} GIT_COMMIT=${GIT_COMMIT} \
    sphinx-build -b html -W --keep-going /repo/docs /repo/docs/_build/html

FROM nginx:1.27-alpine AS runtime
COPY --from=build /repo/docs/_build/html /usr/share/nginx/html
# Friendly root: / -> index.html (nginx's default already does this),
# and no directory listing surprises.
EXPOSE 80
HEALTHCHECK --interval=30s --timeout=3s \
    CMD wget -q -O /dev/null http://127.0.0.1/ || exit 1
