# Multi-stage build for Next.js static export
# Stage 1: Build the app
FROM node:20-alpine AS builder

WORKDIR /app

# Install dependencies (including sharp for icon generation)
COPY package.json package-lock.json ./
RUN npm ci

# Copy source code
COPY . .

# Build argument for PocketBase URL
ARG NEXT_PUBLIC_POCKETBASE_URL
ENV NEXT_PUBLIC_POCKETBASE_URL=${NEXT_PUBLIC_POCKETBASE_URL}

# WFDASH address the invoice match calls (overridable per phone in Settings)
ARG NEXT_PUBLIC_WFDASH_URL=https://wfdash.apphub.co.za
ENV NEXT_PUBLIC_WFDASH_URL=${NEXT_PUBLIC_WFDASH_URL}

# Generate PWA icons
RUN node scripts/generate-icons.js

# Build the static export (sets NEXT_EXPORT=true)
RUN npm run export

# Stage 2: Serve with nginx
FROM nginx:alpine

# Remove default nginx static assets
RUN rm -rf /usr/share/nginx/html/*

# Copy built static files from builder (Next.js export outputs to /out)
COPY --from=builder /app/out /usr/share/nginx/html

# Copy custom nginx config
COPY nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

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