# aiw-voice-marketing: Next.js 14 App Router site, served via Next.js standalone.
#
# Multi-stage build mirroring aiw-voice-app's pattern. Two key differences:
#   1. The runtime is `node:20-alpine` (not nginx) - Next.js standalone bundles
#      its own minimal Node server. This is the official Next.js recommendation
#      for Docker.
#   2. Build accepts MARKETING_MODE arg to pick between .env.staging (dev VPS)
#      and .env.production (prod VPS). Mirrors aiw-voice-app's VITE_MODE arg.
#
# Final image is ~120MB (vs ~1GB for a naive `next start` build). The
# standalone tracer copies ONLY the production deps actually imported by
# the build, not the full node_modules tree.

# ─── Builder ─────────────────────────────────────────────────────────────
FROM node:20-alpine AS builder
WORKDIR /app

# Install deps first so this layer caches independently of source.
# NOTE: uses `npm install` (not `npm ci`) because this package ships without a
# committed package-lock.json yet. `npm install` resolves within package.json's
# semver ranges and already honors a lockfile if one is present. Once a lockfile
# is generated + committed (matching aiw-voice-app/backend/widget), switch this
# back to `npm ci` for fully reproducible builds. The `package*.json` glob below
# already copies a lockfile too, so only this RUN line needs changing then.
COPY package*.json ./
RUN npm install

# Source.
COPY . .

# Build-time env selection. .env.staging vs .env.production is picked by
# copying the right file to .env.production.local (Next.js's standard
# file-precedence rule for build-time NEXT_PUBLIC_* values).
ARG MARKETING_MODE=production
# Pick the build-time NEXT_PUBLIC_* env file. BOTH branches are explicit so a
# future .dockerignore tightening that excludes .env.* can't silently fall the
# prod build back to localhost defaults (lib/urls.ts). .env.production.local
# takes precedence over .env.production per Next.js file-precedence rules.
RUN if [ "$MARKETING_MODE" = "staging" ]; then \
      cp .env.staging .env.production.local; \
    else \
      cp .env.production .env.production.local; \
    fi

# Standalone build. Outputs:
#   .next/standalone/   - minimal Node server + only the deps it uses
#   .next/static/       - hashed JS/CSS assets
#   public/             - copied as-is
ENV NEXT_TELEMETRY_DISABLED=1
RUN npm run build

# ─── Runtime ─────────────────────────────────────────────────────────────
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV PORT=3001
ENV HOSTNAME=0.0.0.0

# Non-root user (defense in depth - Next standalone doesn't need root).
RUN addgroup --system --gid 1001 nodejs && \
    adduser --system --uid 1001 nextjs

# Copy ONLY the standalone output + public + static.
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3001

# server.js is generated by Next at the root of .next/standalone.
CMD ["node", "server.js"]
