# Build stage
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files first for better caching
COPY package*.json ./

# Install dependencies (this layer will be cached if package*.json don't change)
RUN npm ci --only=production=false && \
    npm cache clean --force

# Copy source code
COPY . .

# Build arguments
ARG VITE_API_URL
ARG VITE_WS_URL
ARG VITE_STREAM_URL

# Build the application with error checking
RUN npm run build && \
    # Verify build output exists
    test -f dist/index.html || { echo "ERROR: dist/index.html not found after build!"; exit 1; } && \
    ls -lh dist/ && \
    echo "✅ Build successful - dist/ contains $(find dist -type f | wc -l) files"

# Production stage
FROM nginx:alpine

# Install dependencies for healthcheck
RUN apk add --no-cache wget

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

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

# Add health check script
RUN echo '#!/bin/sh' > /usr/share/nginx/html/health && \
    echo 'exit 0' >> /usr/share/nginx/html/health && \
    chmod +x /usr/share/nginx/html/health

# Create non-root user for security (nginx runs as nginx user by default)
# Nginx alpine image already runs as non-root user

# Expose port
EXPOSE 80

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
    CMD wget --no-verbose --tries=1 --spider http://localhost/health || exit 1

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

