# ---- Stage 1: build the static bundle -------------------------------------
FROM node:20-alpine AS build
WORKDIR /app

# Install deps first for better layer caching.
COPY package.json ./
RUN npm install

# Build the production bundle.
COPY . .
RUN npm run build

# ---- Stage 2: serve with nginx --------------------------------------------
FROM nginx:1.27-alpine AS runtime

# SPA-aware server config.
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Static assets produced by Vite.
COPY --from=build /app/dist /usr/share/nginx/html

EXPOSE 80

# Lightweight container healthcheck against the served index.
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget -qO- http://localhost/ >/dev/null 2>&1 || exit 1

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