# Multi-stage Dockerfile for Kamiwaza Frontend
# Supports amd64 and arm64 architectures

# Stage 1: Build the frontend application
FROM node:22-bookworm AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies (including dev dependencies for build)
RUN npm ci

# Copy source code
COPY . .

# Build the application with webpack
RUN npm run build

# Stage 2: Production runtime
FROM node:22-slim

WORKDIR /app

# Install serve package globally for serving static files
RUN npm install -g serve@14.2.4

# Copy built assets from builder stage
COPY --from=builder /app/dist ./dist

# Expose port 3000
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
  CMD node -e "require('http').get('http://localhost:3000', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)}).on('error', () => process.exit(1))"

# Run the application with serve
# -s: serve single page app (SPA mode - all routes go to index.html)
# -l: listen port
CMD ["serve", "-s", "dist", "-l", "3000"]
