# Stage 1: Build
FROM node:20-alpine AS builder

WORKDIR /app

# Native module build deps (better-sqlite3)
RUN apk add --no-cache python3 make g++

COPY package*.json ./
RUN npm ci

COPY . .
RUN npm run build && npm prune --omit=dev

# Stage 2: Runtime
FROM node:20-alpine AS runtime

WORKDIR /app

RUN mkdir -p /data

COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY package*.json ./

ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=3000
ENV DB_PATH=/data/guestbook.db

EXPOSE 3000

CMD ["node", "dist/server/entry.mjs"]
