# =============================================================================
# AI Growth Technologies — Dockerfile (one-click deploy)
# Multi-stage build: Node 22 build -> Nginx serve static SPA
# =============================================================================

# ---------- Stage 1: Build the Vite React app ----------
FROM node:22-bullseye-slim AS builder

WORKDIR /app

# Copy workspace manifests first for better layer caching
COPY package.json package-lock.json ./
COPY apps/web/package.json ./apps/web/package.json

# Install all workspace dependencies (root + apps/web).
#
# Workaround for npm's optional-dependencies bug (npm/cli#4828): the committed
# lockfile does not contain the musl-native rollup binary, so `npm ci` inside
# this Alpine (musl) image skips it and `vite build` fails with
# "Cannot find module @rollup/rollup-linux-x64-musl". After installing, verify
# that rollup (and its platform-native binary) actually loads; if it does not,
# fall back to a reconciling `npm install`, which resolves and installs the
# correct platform binaries from the registry.
RUN npm ci --no-audit --no-fund \
    && node -e "require('rollup')" \
    || (rm -rf node_modules apps/web/node_modules \
        && npm install --no-audit --no-fund \
        && node -e "require('rollup')")

# Copy the rest of the source
COPY . .

# Build the frontend -> outputs to /app/dist/apps/web
RUN npm run build

# ---------- Stage 2: Serve with Nginx ----------
FROM nginx:1.27-alpine AS runner

# Clean default nginx static site
RUN rm -rf /usr/share/nginx/html/*

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

# Custom nginx config (SPA fallback, gzip, cache headers)
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

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