# syntax=docker/dockerfile:1

# Multi-stage build: install workspace deps, build the web UI, run the server
# (which serves the built UI and connects to the shared Postgres). The agent
# workspace is NOT in the image — it runs on the player's PC.
FROM node:22-bookworm-slim AS build
WORKDIR /app

# Install deps using the workspace manifests (better layer caching). No native
# toolchain needed — the data layer is pure-JS `pg`.
COPY package.json package-lock.json ./
COPY shared/package.json ./shared/
COPY server/package.json ./server/
COPY web/package.json ./web/
COPY agent/package.json ./agent/
RUN npm ci

# Copy sources and build the web UI.
COPY shared ./shared
COPY server ./server
COPY web ./web
RUN npm run build -w web

# ---- runtime image ----
FROM node:22-bookworm-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production \
    HOST=0.0.0.0 \
    PORT=4310 \
    ALBION_DATA_DIR=/data \
    WEB_DIST=/app/web/dist

COPY --from=build /app/node_modules ./node_modules
COPY --from=build /app/package.json ./package.json
COPY --from=build /app/shared ./shared
COPY --from=build /app/server ./server
COPY --from=build /app/web/dist ./web/dist

# Downloaded game-data JSON cache lives here (the DB is external Postgres).
RUN mkdir -p /data
VOLUME /data
EXPOSE 4310

# The server creates its schema and, on first boot with an empty DB, downloads
# and parses game data in the background.
CMD ["node", "--import", "tsx", "server/src/index.ts"]
