# ============================================================
# SmartStack Frontend - Multi-stage build
# Image: smartstack-{project}-frontend:{tag}
# ============================================================

# --- Stage 1: Build ---
FROM node:22-alpine AS build
WORKDIR /app

# Build arguments for API URLs (injected at build time)
ARG VITE_API_URL
ARG VITE_WS_URL

# Copy package file for npm cache (context = project root)
COPY web/{{ProjectNameLower}}-web/package.json ./

# The package.json `postinstall` hook runs `node scripts/patch-smartstack-theme.cjs`
# (a Tailwind v4 workaround that patches node_modules/@atlashub/smartstack), so the
# scripts/ dir must be present BEFORE npm install fires it. Copied here (scripts
# change rarely) to keep the dependency layer cached.
COPY web/{{ProjectNameLower}}-web/scripts ./scripts

# Install dependencies (cached layer)
RUN npm install

# Copy source code
COPY web/{{ProjectNameLower}}-web/ .

# Build for production
RUN npm run build:prod

# --- Stage 2: Runtime ---
FROM nginx:alpine AS runtime

# Copy built files
COPY --from=build /app/dist /usr/share/nginx/html

# Nginx config as an envsubst TEMPLATE: the official nginx entrypoint runs envsubst
# over /etc/nginx/templates/*.template at startup, substitutes ${API_FQDN} (injected
# as an env var on the web Container App) -> /etc/nginx/conf.d/default.conf. Nginx's
# own runtime vars ($uri, $host, ...) are preserved (envsubst only touches defined env).
COPY docker-images/nginx-frontend.conf /etc/nginx/templates/default.conf.template

EXPOSE 80

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
    CMD wget -qO- http://localhost/ || exit 1

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