FROM node:22-alpine AS base

WORKDIR /app

# Install deps (build from repo root so shared/ resolves)
COPY package.json package-lock.json* ./
COPY shared/package.json shared/
COPY server/package.json server/
RUN npm install --ignore-scripts

# Rebuild native modules (better-sqlite3)
RUN npm rebuild better-sqlite3

# Copy source
COPY shared ./shared
COPY server ./server
COPY site-context.json ./

# Copy content twice:
#   /app/content       — the live directory (will be overlaid by a volume mount)
#   /app/content-seed  — preserved in the image, used to seed empty volumes
COPY content ./content
COPY content ./content-seed

COPY config ./config

# Entrypoint seeds the volume on first deploy
COPY server/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh

WORKDIR /app/server

ENV NODE_ENV=production
ENV REPO_ROOT=/app
EXPOSE 3001

ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["npx", "tsx", "src/index.ts"]
