# Multi-stage Dockerfile for the email-editor microservice.
# Stage 1: Vite build into dist/
# Stage 2: nginx serving the static files

# ── Stage 1: build ──
FROM node:20-alpine AS builder
WORKDIR /app

# Install only what's needed for build (no native deps required)
COPY package.json package-lock.json* ./
RUN npm install --silent

COPY tsconfig.json vite.config.ts index.html ./
COPY src ./src

# Inline the prod hub URL at build time. Override via --build-arg on docker
# build if running against a different hub (e.g. staging).
ARG VITE_HUB_URL=https://msg.gigafibre.ca
ENV VITE_HUB_URL=$VITE_HUB_URL

RUN npm run build

# ── Stage 2: nginx serve ──
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html

# Drop the default nginx config and inject a minimal SPA-friendly one
# (all routes serve index.html — easy-email is a SPA, query params drive
# which template to load).
RUN rm /etc/nginx/conf.d/default.conf
COPY <<EOF /etc/nginx/conf.d/default.conf
server {
  listen 80;
  server_name _;
  root /usr/share/nginx/html;
  index index.html;

  # Long cache for hashed assets (vite outputs them with content hash in name)
  location /assets/ {
    expires 1y;
    add_header Cache-Control "public, immutable";
  }

  # SPA fallback — every request returns index.html so the React app handles
  # routing client-side based on ?name=... query
  location / {
    try_files \$uri \$uri/ /index.html;
  }
}
EOF

EXPOSE 80
