# -------------------------------------------------
# Builder – install deps, compile the CRA app
# -------------------------------------------------
FROM node:20-alpine AS builder

ARG UID=1001
ARG GID=1001
RUN addgroup -g ${GID} appgroup && \
    adduser -D -u ${UID} -G appgroup appuser

WORKDIR /app

# Copy only the manifest files – maximises Docker cache reuse
COPY package*.json ./
RUN npm ci          # keep if you have a lockfile
# RUN npm install --omit=dev   # fallback if you don’t have a lockfile

# Copy the rest of the source code
COPY . .

# Build the production bundle (CRA creates ./build)
RUN npm run build   # CRA creates ./build

# -------------------------------------------------
# Runtime – tiny static server (serve)
# -------------------------------------------------
FROM node:20-alpine AS runner
WORKDIR /app

# Install the tiny static‑file server
RUN npm install -g serve

# Copy the compiled assets from the builder stage.
# We rename the CRA “build” folder to “dist” so the CMD can stay the same.
COPY --from=builder /app/build ./dist

# Backend URL – the Rust API service is called “regulatory_api” in compose
ENV VITE_BACKEND_URL=http://regulatory_api:3000

# Expose the port that `serve` will listen on (default 80)
EXPOSE 80

# Exec‑form command – proper JSON array, guarantees correct signal handling
CMD ["serve", "-s", "dist", "-l", "80"]
