# The build stage runs on the builder's own platform ($BUILDPLATFORM). The Nitro output is plain JS and the final
# stage has no RUN, so a linux/amd64 + linux/arm64 build needs no emulation (the CI runner has no QEMU).
FROM --platform=$BUILDPLATFORM node:26.13.1-alpine@sha256:6bd9ab43c0ec0190ca7b2d193906cb8b7edf6e7f4afa43ddeccd6452710bffb5 AS build
WORKDIR /app
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./
# Node 26 images have no corepack: install the pnpm version from package.json's packageManager field.
RUN npm install -g "pnpm@$(node -p "require('./package.json').packageManager.split('@')[1]")" \
 && pnpm install --frozen-lockfile
COPY . .
RUN pnpm build

# Self-contained Nitro output (dependencies are bundled): no node_modules install in the runtime image.
FROM node:26.13.1-alpine@sha256:6bd9ab43c0ec0190ca7b2d193906cb8b7edf6e7f4afa43ddeccd6452710bffb5
ENV NODE_ENV=production PORT=3000
WORKDIR /app
COPY --from=build --chown=node:node /app/.output ./
# The server applies these on start (src/db/index.ts).
COPY --chown=node:node drizzle ./drizzle
USER node
EXPOSE 3000
# /login needs the database, so healthy also means migrated.
HEALTHCHECK --interval=15s --timeout=5s --start-period=20s \
  CMD node -e "fetch('http://127.0.0.1:3000/login').then(r=>process.exit(r.ok?0:1),()=>process.exit(1))"
CMD ["node", "server/index.mjs"]
