# ---------------------------------------------------------
#  Build stage
# ---------------------------------------------------------
FROM node:18-alpine AS builder

# Install git (needed by some npm packages)
RUN apk add --no-cache git

WORKDIR /app

# Copy only package files first (leverages caching)
COPY package.json package-lock.json* ./

# Install dependencies (npm ci uses lockfile, but we don't have it)
# Using npm install instead for broader compatibility
RUN npm install

# Copy source files
COPY . .

# Build the production bundle (outputs to /app/dist)
ENV NODE_ENV=production
RUN npm run build

# DEBUG: Verify the build output exists before proceeding
RUN echo "=== BUILD OUTPUT ===" && \
    ls -la /app/dist && \
    cat /app/dist/index.html | head -n 10 || echo "ERROR: index.html missing"

# ---------------------------------------------------------
#  Production stage – nginx serving the static files
# ---------------------------------------------------------
FROM nginx:alpine

# Custom nginx config
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Copy the compiled React app into nginx's web root
COPY --from=builder /app/dist /usr/share/nginx/html

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