# Base images are build arguments so that make -C deploy release-build can pin
# each one to the digest it resolved; the defaults keep the agreed tags.
ARG NODE_IMAGE=node:alpine
ARG NGINX_IMAGE=nginxinc/nginx-unprivileged:alpine
# Build-time mode of the static bundle. Only the overlays that serve synthetic
# demonstration data set it to "true" (the jury guide and its banner); the
# default build leaves those out.
ARG VITE_DEMO_STAND=false

FROM ${NODE_IMAGE} AS build

WORKDIR /workspace/frontend
RUN npm install --global pnpm@12.9.1

COPY frontend/package.json frontend/pnpm-lock.yaml frontend/pnpm-workspace.yaml ./
RUN pnpm install --frozen-lockfile

COPY frontend/ ./
ARG VITE_DEMO_STAND
ENV VITE_DEMO_STAND=${VITE_DEMO_STAND}
RUN pnpm run build

FROM ${NGINX_IMAGE}

# The floating base tag can lag behind Alpine security fixes (for example
# libtiff), so the runtime packages are upgraded at build time; the image runs
# as the unprivileged user again right after.
USER root
RUN apk upgrade --no-cache
USER 101

COPY --from=build /workspace/frontend/dist/ /usr/share/nginx/html/
COPY deploy/nginx/nginx.conf /etc/nginx/nginx.conf
COPY deploy/nginx/conf.d/app.conf /etc/nginx/conf.d/default.conf
COPY deploy/nginx/snippets/ /etc/nginx/snippets/

EXPOSE 8080
USER 101

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 CMD ["wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/healthz"]
