# SiMS HQ console — production image (D20 hosting slice).
# Runs the Express server, which also serves the built React UI and the public
# /share links, on port 5182. Postgres is EXTERNAL (the shared `accurate-postgres`
# container), reached over the host gateway — see docker-compose.yml.
FROM node:20-bookworm

# Chromium runtime libraries for puppeteer (PDF letterhead rendering).
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates fonts-liberation \
      libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 \
      libgbm1 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 \
      libpango-1.0-0 libcairo2 libasound2 libatspi2.0-0 libgtk-3-0 \
      libx11-6 libxcb1 libxext6 libxi6 \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install all workspaces (also fetches puppeteer's Chromium + better-sqlite3 prebuilt
# binary for linux-x64), then build the web UI and bundle the server.
COPY . .
# package-lock.json is generated on Windows dev machines; npm's optional-dependency
# resolution has a known bug (npm/cli#4828) where a lockfile from one platform doesn't
# pull in another platform's native binaries (here, @rollup/rollup-linux-x64-gnu) even
# though npm install runs fresh in this (Linux) container. Dropping the lockfile before
# install forces a same-platform resolve.
RUN rm -f package-lock.json \
 && npm install \
 && npm run build --workspace @sims/hq-web \
 && npm run build --workspace @sims/hq

ENV NODE_ENV=production
EXPOSE 5182

# Launch from the repo root: server.cjs resolves ../../hq-web/dist for the UI and
# loads better-sqlite3/puppeteer (kept external from the bundle) from ./node_modules.
CMD ["node", "apps/hq/dist/server.cjs"]
