# syntax=docker/dockerfile:1
#
# Legyen Nyugta landing — Astro 7, fully static output served by nginx.
#
# The image is built once with IS_DEV=false (production robots.txt) and promoted unchanged from
# DEV to TST to PRD; at container start docker/40-legyennyugta-env.sh swaps in the disallow-all
# robots.txt when the environment sets IS_DEV=true, and fills in NEW_MESSAGE_API_URL.
#
#   docker build -t legyennyugta:local .
#   docker run -p 8080:8080 -e IS_DEV=false -e NEW_MESSAGE_API_URL=https://... legyennyugta:local

ARG NODE_IMAGE=node:24-alpine
ARG NGINX_IMAGE=nginxinc/nginx-unprivileged:stable-alpine-slim


# --- build -------------------------------------------------------------------
FROM ${NODE_IMAGE} AS build
WORKDIR /app

COPY package.json package-lock.json ./

RUN --mount=type=cache,target=/root/.npm \
    npm ci

COPY . .

# Keep in sync with the env schema in astro.config.mjs and docker/40-legyennyugta-env.sh.
ENV IS_DEV=false
# Baked into every absolute URL (canonical, og:*, sitemap, robots.txt); the image is promoted
# unchanged to PRD, so it is always built with the production origin.
ENV APP_URL=https://legyennyugta.hu
# Differs per environment, so the bundle gets a placeholder that docker/40-legyennyugta-env.sh
# replaces with the container's NEW_MESSAGE_API_URL at start.
ENV NEW_MESSAGE_API_URL=__NEW_MESSAGE_API_URL__

RUN npm run build


# --- runtime -----------------------------------------------------------------
FROM ${NGINX_IMAGE} AS runtime

COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY --chmod=0755 docker/40-legyennyugta-env.sh /docker-entrypoint.d/40-legyennyugta-env.sh

# Owned by the unprivileged nginx user (101): the entrypoint script rewrites robots.txt in place.
COPY --from=build --chown=101:101 /app/dist /usr/share/nginx/html

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD wget -q -O /dev/null http://127.0.0.1:8080/healthz || exit 1

# Last, so provenance never invalidates the layers above.
ARG BUILD_REVISION=unknown
ARG BUILD_CREATED=unknown
ARG BUILD_VERSION=unknown
ARG NGINX_IMAGE
LABEL org.opencontainers.image.title="legyennyugta" \
      org.opencontainers.image.description="Legyen Nyugta landing — static Astro site served by nginx" \
      org.opencontainers.image.revision="${BUILD_REVISION}" \
      org.opencontainers.image.created="${BUILD_CREATED}" \
      org.opencontainers.image.version="${BUILD_VERSION}" \
      org.opencontainers.image.base.name="${NGINX_IMAGE}"
