# ── Kerf App — multi-stage Docker build ──
# Stage 1: Build the Vite/React app
# Stage 2: Serve via nginx with reverse-proxy to engine

# ── Stage 1: Build ──
FROM node:22-alpine AS builder

WORKDIR /build

# Copy root workspace config first (npm ci needs it for workspace resolution)
COPY package.json package-lock.json ./

# Copy the app workspace
COPY app/ ./app/

# Install dependencies from the workspace root
RUN npm ci --workspace=app

# Build the app (tsc -b && vite build)
RUN npm run build --workspace=app

# ── Stage 2: Runtime — nginx serving static files ──
FROM nginx:1.27-alpine AS runtime

# Remove default nginx site
RUN rm /etc/nginx/conf.d/default.conf

# Copy custom nginx config
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf

# Copy built assets from builder
COPY --from=builder /build/app/dist /usr/share/nginx/html

EXPOSE 80

HEALTHCHECK --interval=15s --timeout=5s --start-period=5s --retries=3 \
    CMD wget -qO- http://127.0.0.1:80/ || exit 1

CMD ["nginx", "-g", "daemon off;"]
