# syntax=docker/dockerfile:1

# ---------- Stage 1: build the static site ----------
FROM node:22-alpine AS build

WORKDIR /app

# Copy manifests first so Docker can cache the dependency install layer
COPY package.json package-lock.json ./
COPY apps/web/package.json apps/web/package.json

RUN npm ci --no-audit --no-fund

# Copy the rest of the source and build
COPY . .

RUN npm run build --workspace apps/web

# ---------- Stage 2: serve with Nginx ----------
FROM nginx:1.27-alpine

# Remove the default nginx config (the one that shows "Welcome to nginx!")
RUN rm -f /etc/nginx/conf.d/default.conf

COPY nginx.conf /etc/nginx/conf.d/mazzei.conf
COPY --from=build /app/dist/apps/web /usr/share/nginx/html

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget -qO- http://127.0.0.1/ >/dev/null 2>&1 || exit 1

CMD ["nginx", "-g", "daemon off;"]
