# Machines that cannot run Node 22 still need an agent. Windows Server 2008 R2
# and Windows 7 top out at Node 13, so the same agent.js is bundled with a Node
# 12 runtime into a single .exe the hub serves from /download/agent.exe.
FROM node:22-alpine AS agent-exe
WORKDIR /build
COPY agent ./agent
# `ws` stands in for the global WebSocket the old runtime does not have; pkg
# follows the literal require in agent.js and bundles it.
RUN npm install --no-save --no-audit --no-fund ws@8.18.0 \
 && npx --yes pkg@5.8.1 agent/agent.js \
      --targets node12-win-x64 \
      --output dist/rcs-agent.exe

FROM node:22-alpine

# node:sqlite is built into Node 22, so there are no native modules to compile
# and no build stage to carry around.
ENV NODE_ENV=production \
    PORT=8080 \
    DB_PATH=/data/rcs.db

WORKDIR /app

RUN corepack enable
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml* ./
RUN pnpm install --prod --frozen-lockfile

COPY server ./server
COPY public ./public
COPY agent ./agent
COPY --from=agent-exe /build/dist/rcs-agent.exe ./dist/rcs-agent.exe

VOLUME ["/data"]
EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s \
  CMD wget -qO- http://127.0.0.1:8080/api/health || exit 1

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