# Caelum production image.
#
# Multi-stage so the runtime layer doesn't carry the Vite toolchain. The
# build stage installs every workspace's deps, builds the client SPA into
# client/dist, then we copy server source + built client + the Nox workspace
# into a clean Node 22 image.

# ---------- build stage ----------
FROM node:22-alpine AS build

WORKDIR /app

# Copy the workspace manifests first so Docker can cache `npm install` when
# only source files change.
COPY package.json package-lock.json* ./
COPY server/package.json server/package-lock.json* ./server/
COPY client/package.json client/package-lock.json* ./client/
COPY nox/parser/package.json ./nox/parser/
COPY nox/runtime/package.json ./nox/runtime/

# Install dependencies for every workspace.
# Order matters because of the file: dependency chain:
#   server  → @caelum/nox-runtime (file:../nox/runtime)
#   nox-runtime → @caelum/nox-parser (file:../parser)
# Install nox-parser first so the symlink resolves cleanly through the chain.
RUN npm install --no-audit --no-fund \
  && cd nox/parser  && npm install --no-audit --no-fund && cd ../.. \
  && cd nox/runtime && npm install --no-audit --no-fund && cd ../.. \
  && cd server      && npm install --no-audit --no-fund && cd ..   \
  && cd client      && npm install --no-audit --no-fund && cd ..

# Copy the rest of the source.
COPY nox ./nox
COPY server ./server
COPY client ./client
COPY scripts ./scripts
COPY server.js ./

# Build the Vue SPA, then move the sourcemaps out of the served tree.
# express.static serves everything under client/dist, so a map left there is
# public (caelum.network/assets/*.js.map answered 200 before this). Vite
# writes every map to dist/assets. /app/sourcemaps is not served; the
# sourcemaps-upload CI job copies it out of the image and sends it to
# GlitchTip.
RUN cd client && npm run build \
  && mkdir -p /app/sourcemaps/assets \
  && mv dist/assets/*.map /app/sourcemaps/assets/

# ---------- runtime stage ----------
FROM node:22-alpine AS runtime

# Run as a non-root user. Signal handling is done by the app itself
# (SIGTERM/SIGINT handlers in server/src/index.js) so we don't need tini.
RUN addgroup -S caelum && adduser -S caelum -G caelum

WORKDIR /app

# Only what the runtime needs. We carry the build-stage node_modules trees
# as-is rather than pruning, because the workspace uses file: deps that form
# a symlink chain (server → @caelum/nox-runtime → @caelum/nox-parser) and
# `npm prune` can dismantle it. devDependencies are server-side small.
COPY --from=build /app/server ./server
COPY --from=build /app/nox ./nox
COPY --from=build /app/client/dist ./client/dist
COPY --from=build /app/sourcemaps ./sourcemaps
COPY --from=build /app/package.json ./
COPY --from=build /app/server.js ./
COPY --from=build /app/node_modules ./node_modules

# Sanity test: the nox runtime can resolve through the file: chain.
# Run from the server dir because that's where server.js chdirs to at boot.
RUN cd server && node -e "import('@caelum/nox-runtime').then(m => console.log('nox runtime resolves:', typeof m.run))" --input-type=module

# Pre-create the upload directory the archive route mkdirs on import, and
# hand ownership of /app to the non-root user so writable paths work.
RUN mkdir -p /app/server/data/uploads \
  && chown -R caelum:caelum /app

USER caelum

ENV NODE_ENV=production
ENV PORT=3002
EXPOSE 3002

CMD ["node", "server.js"]
