# Phase 04: Frontend Application - Frontend Dockerfile
# Multi-stage build for production-optimized React application
#
# Build: docker build -f Dockerfile.frontend -t swarmcare/frontend:latest .
# Run: docker run -p 80:80 swarmcare/frontend:latest

# ============================================================================
# STAGE 1: Build React Application
# ============================================================================
FROM node:18-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci --only=production && \
    npm cache clean --force

# Copy source code
COPY public/ ./public/
COPY src/ ./src/
COPY tsconfig.json ./
COPY tailwind.config.js ./
COPY .env.production ./

# Build application
RUN npm run build

# ============================================================================
# STAGE 2: Production Nginx Server
# ============================================================================
FROM nginx:alpine

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Copy built React app from builder stage
COPY --from=builder /app/build /usr/share/nginx/html

# Add healthcheck
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD wget --quiet --tries=1 --spider http://localhost:80/ || exit 1

# Expose port
EXPOSE 80

# Labels for metadata
LABEL maintainer="SwarmCare Engineering <engineering@swarmcare.io>"
LABEL description="SwarmCare Frontend - RAG UI, Dashboard, Podcast UI"
LABEL version="1.0.0"
LABEL phase="04"

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