# Multi-stage build: produce a small image with just the runtime.
#
# Build args:
#   PORT          (default 3001) — server listen port
#   HEZO_DATA (default /data) — where SQLite files + blobs live
#
# Volume: /data should be mounted persistently on Railway etc.

FROM node:20-bookworm-slim AS build
WORKDIR /app

# Install all deps, including dev (needed to build).
COPY package.json package-lock.json ./
RUN npm ci

# Source + build.
COPY tsconfig.json tsconfig.server.json tsconfig.web.json vite.config.ts ./
COPY index.html ./
COPY src ./src
COPY server ./server
RUN npm run build

# Prune dev deps for the runtime image.
RUN npm prune --omit=dev


FROM node:20-bookworm-slim AS runtime
WORKDIR /app

# better-sqlite3 has a native module; the build stage's node_modules already
# contains a Linux-built binary, so copy it as-is.
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/dist-web ./dist-web
COPY --from=build /app/dist-server ./dist-server

ENV NODE_ENV=production
ENV PORT=3001
ENV HOST=0.0.0.0
ENV HEZO_DATA=/data

EXPOSE 3001
# On Railway: mount a volume at /data via `railway volume add --mount-path /data`.
# (Don't use the Dockerfile VOLUME directive — Railway rejects it.)

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