# ---- stage 1: build ----
FROM node:22-alpine AS build
WORKDIR /app

# Install deps (cache layer)
COPY package.json package-lock.json* ./
RUN npm ci

# Copy source and build
COPY tsconfig.json vite.config.ts index.html ./
COPY src/ src/

# Run tests first — gates the build
RUN npm test

RUN npm run build

# ---- stage 2: serve ----
FROM nginx:alpine-slim AS serve

# curl needed for the healthcheck in docker-compose
RUN apk add --no-cache curl

COPY --from=build /app/dist /usr/share/nginx/html

# Wipe any default server blocks that could conflict with our config.
RUN rm -f /etc/nginx/conf.d/default.conf

# Deploy our custom nginx config and validate it at build time
COPY nginx.default.conf /etc/nginx/conf.d/default.conf
RUN nginx -t

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