# Multi-stage build for the CRDC Arrests API Demo app.
# Stage 1: Build static files with Vite + React
FROM node:22-alpine AS builder

WORKDIR /app
COPY package*.json ./
RUN npm ci --prefer-offline

COPY . .
ARG VITE_API_BASE=https://crdc-api.civilytics.org/api/v1
ENV VITE_API_BASE=${VITE_API_BASE}
ARG VITE_PROXY_URL=""
ENV VITE_PROXY_URL=${VITE_PROXY_URL}
RUN npm run build

# Stage 2: Serve with nginx (tiny image, ~5MB) + reverse proxy for API CORS bypass
FROM nginx:alpine AS runtime

# Copy static site files
COPY --from=builder /app/dist/ /usr/share/nginx/html/

# Custom nginx config — serves static files AND proxies /api/v1/ to the CRDC API
# with CORS headers, so browser-based requests work without preflight issues.
COPY nginx.conf /etc/nginx/conf.d/default.conf

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