# ── Stage 1: compile the TypeScript server ────────────────────────────────
FROM node:24-slim AS server-build

# better-sqlite3 has a native addon with no prebuilt binary for this
# platform/Node combo yet — build it from source.
RUN apt-get update -q && apt-get install -y --no-install-recommends python3 make g++ \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY server/package*.json server/
COPY server/tsconfig.json server/
RUN npm install --prefix server

COPY server/src/ server/src/
RUN npm run build --prefix server

# ── Stage 2: runtime ───────────────────────────────────────────────────────
FROM node:24-slim

WORKDIR /app

RUN apt-get update -q && apt-get install -y --no-install-recommends python3 make g++ \
  && rm -rf /var/lib/apt/lists/*

COPY server/package*.json server/
RUN npm install --prefix server --omit=dev \
  && apt-get purge -y python3 make g++ && apt-get autoremove -y

COPY --from=server-build /app/server/dist server/dist
COPY server/public/ server/public/

# Do NOT copy data/ — mutable runtime volume holding the SQLite DB and blob
# bytes. Baking it into the image would clobber it on every redeploy.
ENV NODE_ENV=production
ENV PORT=3000
ENV DATA_DIR=/app/data

EXPOSE 3000

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