# Order Tracker (CLAUDE.md §15).
#
# Two processes, one image: the Next.js server and the poller. §3 keeps the
# poller a standalone process rather than a route, so compose runs it as its
# own service against the same volume.
#
# NOT BUILT OR RUN on the machine this was written on — Docker is not
# installed there, and nothing here has been verified end to end. What HAS
# been verified, on the same Node version this image uses, is everything the
# image depends on: `npm ci --omit=dev` installs a tree that can run
# `migrate`, `backup` and the poller; `npm run build` succeeds; and the
# production server serves every route. Treat the npm path in README.md as
# the tested one until someone has actually built this.

FROM node:22-slim AS deps
WORKDIR /app
# better-sqlite3 is a native module (§15). Prebuilds normally cover
# linux/amd64 and linux/arm64, but the toolchain is kept here so a platform
# without one still builds rather than failing at install.
RUN apt-get update \
 && apt-get install -y --no-install-recommends python3 make g++ ca-certificates \
 && rm -rf /var/lib/apt/lists/*
COPY package.json package-lock.json ./
RUN npm ci

FROM deps AS build
WORKDIR /app
COPY . .
RUN npm run build

FROM node:22-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
# Hostname matters: Next binds to localhost by default, which inside a
# container means "unreachable from the host".
ENV HOSTNAME=0.0.0.0
ENV PORT=3000

# Production tree only. `tsx` is a RUNTIME dependency here, not a dev one —
# every operational command (poll, parse, migrate, backup, replay) is
# `tsx src/...`, so omitting it leaves a web server that cannot ingest
# anything. That was a real bug in package.json, found by doing exactly this.
COPY package.json package-lock.json ./
RUN npm ci --omit=dev && npm cache clean --force

COPY --from=build /app/.next ./.next
COPY --from=build /app/public ./public
COPY next.config.mjs tsconfig.json ./
# The poller and every CLI run from TypeScript source via tsx, so src/ ships.
COPY src ./src

# Everything mutable lives under these, and compose mounts them as volumes.
# They must NOT be baked into the image or an upgrade would discard the
# database (§12: the only record of a decade of purchases).
RUN mkdir -p data mail media backups logs
VOLUME ["/app/data", "/app/mail", "/app/media", "/app/backups", "/app/logs"]

EXPOSE 3000
CMD ["npm", "start"]
