# Build stage
FROM node:18-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci

# Copy source code
COPY . .

# Build for production
RUN npm run build

# Production stage
FROM nginx:alpine

# Pass WEB_API_BASIC_B64 into nginx for private API auth
ARG WEB_API_BASIC_B64
ENV WEB_API_BASIC_B64=${WEB_API_BASIC_B64}

# Copy built assets from builder
COPY --from=builder /app/dist /usr/share/nginx/html

# Copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Copy SSL certificates (mount volume or copy from build args)
# These should be provided at runtime via Docker volumes or secret management
# Example: docker run -v /path/to/certs:/etc/nginx/ssl ...

# Expose ports 80 (HTTP redirect) and 443 (HTTPS)
EXPOSE 80 443

# Health check (check HTTPS port)
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --no-verbose --tries=1 --spider http://127.0.0.1/health || exit 1

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