# ---- Stage 1: build static assets ----
# The active frontend lives under web/ (Vite + React + TypeScript). The legacy
# root-level Webpack application has been removed; production must only build web/.
FROM node:22-alpine AS build

WORKDIR /app/web
COPY web/package.json web/package-lock.json ./
RUN npm ci
COPY web/. .

# VITE_TINODE_API_KEY is read by src/lib/config.ts (import.meta.env.VITE_TINODE_API_KEY) at build
# time -- Vite only exposes env vars prefixed VITE_ to client code, so the ARG is renamed on its
# way into the build's environment rather than passed through under its old webpack-era name.
ARG TINODE_API_KEY=
ENV VITE_TINODE_API_KEY=$TINODE_API_KEY

RUN npm run build

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

COPY docker/nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR /usr/share/nginx/html

# Vite's build output is fully self-contained: index.html, public assets and
# content-hashed JS/CSS chunks.
COPY --from=build /app/web/dist/. ./

HEALTHCHECK --interval=1m --timeout=3s --start-period=10s \
  CMD wget -q -O /dev/null http://localhost/ || exit 1

EXPOSE 80
