# syntax=docker/dockerfile:1
# `dist` only contains static assets, so build it once on the native builder.
# The final nginx stage remains target-platform aware for amd64 and arm64.
FROM --platform=$BUILDPLATFORM node:22-alpine AS build

WORKDIR /app
ENV ELECTRON_SKIP_BINARY_DOWNLOAD=1

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --no-audit --no-fund

# Copy source code
COPY . .

# Build the application. --ignore-scripts skips the prebuild hook: sync-version
# rewrites server/package.json, which is excluded from this frontend-only context.
RUN npm run build --ignore-scripts

# Production stage
FROM nginx:alpine

# Remove default nginx config that conflicts with ours
RUN rm -f /etc/nginx/conf.d/default.conf

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

# Copy custom nginx configuration template (rendered at startup with BACKEND_HOST)
COPY nginx.conf.template /etc/nginx/nginx.conf.template

# Expose port
EXPOSE 80

# Render the nginx config (substituting BACKEND_HOST and RESOLVER) and start nginx
CMD ["sh", "-c", "export BACKEND_HOST=${BACKEND_HOST:-backend:3000}; export RESOLVER=${RESOLVER:-127.0.0.11}; envsubst '${BACKEND_HOST} ${RESOLVER}' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"]
