# =========================================================
# Stage 1: Build React/Vite
# =========================================================
FROM node:22-alpine AS builder

WORKDIR /app

COPY frontend-prep/package.json frontend-prep/package-lock.json* ./
RUN npm ci --ignore-scripts

COPY frontend-prep/ .
RUN npm run build

# =========================================================
# Stage 2: Caddy — SPA static server (TLS terminé par Traefik)
# =========================================================
FROM caddy:alpine AS runtime

# Retire la file capability CAP_NET_BIND_SERVICE — incompatible avec
# allowPrivilegeEscalation: false. On écoute sur 8080 à la place.
RUN apk add --no-cache libcap && setcap -r /usr/bin/caddy && apk del libcap

COPY helm/frontend/Caddyfile /etc/caddy/Caddyfile
COPY --from=builder /app/dist /srv

EXPOSE 8080

CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
