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

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

WORKDIR /app

# Copy package files
COPY package.json package-lock.json* ./

# Install dependencies
RUN npm install

# Copy source files
COPY . .

# Build the production bundle (Vite substitutes env vars at build time)
ARG VITE_SUPABASE_URL
ARG VITE_SUPABASE_ANON_KEY
ENV VITE_SUPABASE_URL=${VITE_SUPABASE_URL}
ENV VITE_SUPABASE_ANON_KEY=${VITE_SUPABASE_ANON_KEY}
ENV NODE_ENV=production
RUN npm run build

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

# ---------------------------------------------------------
#  Production stage – nginx static server
# ---------------------------------------------------------
FROM nginx:alpine

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

# Copy the compiled React app
COPY --from=builder /app/dist /usr/share/nginx/html

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