# Production-grade Dockerfile for React Frontend with Vite
# Build: docker build -t trust-tax-frontend .
# Run: docker run -p 3000:3000 trust-tax-frontend

# Stage 1: Build
FROM node:18-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm ci

# Copy source code
COPY . .

# Build application
RUN npm run build

# Verify build output
RUN ls -la dist/

# Stage 2: Production
FROM node:18-alpine

WORKDIR /app

# Install dumb-init and serve
RUN apk add --no-cache dumb-init curl && \
    npm install -g serve@14

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

# Create non-root user for security
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nodejs -u 1001
USER nodejs

# Expose port
EXPOSE 3000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
  CMD curl -f http://localhost:3000 || exit 1

# Use dumb-init to handle signals properly
ENTRYPOINT ["/sbin/dumb-init", "--"]

# Serve the application
CMD ["serve", "-s", "dist", "-l", "3000"]
