# Stage 1: Build web frontend
FROM node:20-alpine AS builder
WORKDIR /app

# Copy dependency manifests for workspace caching
COPY package.json package-lock.json ./
COPY server/package.json ./server/
COPY web/package.json ./web/

# Install all dependencies (including devDependencies for Vite)
RUN npm ci

# Copy application source
COPY . .

# Build frontend to web/dist
RUN npm run build

# Stage 2: Production runner
FROM node:20-alpine AS runner
WORKDIR /app

ENV NODE_ENV=production
ENV PORT=8787

# Copy app files from builder
COPY --from=builder /app ./

EXPOSE 8787

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