# BUILD STAGE
FROM node:20-alpine AS build

WORKDIR /app

# R10.8: Optimized build layer order
COPY package*.json ./

# scripts/ must land BEFORE npm install: package.json has a `postinstall` hook
# running scripts/patch-livekit.mjs (the livekit-client codec patch), so an
# install without it fails with "Cannot find module". That is what silently
# broke image builds from 2026-07-09 onward — the image went stale, deploys
# fell back to docker cp into the running container, and a container recreate
# then reverted the site to whatever the last buildable image contained.
# scripts/ changes rarely, so this costs nothing in layer caching.
COPY scripts ./scripts

# R10.8: Use persistent NPM Registry Cache
RUN --mount=type=cache,target=/root/.npm \
    npm install

# Copy source after dependencies are installed
COPY . .

# R10.8: Multi-Stage Cache Shield (Vite & TSC)
# Caches node_modules/.cache for ultra-fast incremental builds
RUN --mount=type=cache,target=/app/node_modules/.cache \
    npm run build

# RUN STAGE
FROM nginx:alpine

# Copy only the compiled assets
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf.hydrated /etc/nginx/conf.d/default.conf

EXPOSE 80 443

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