# ---------------------------------------------------------
#  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 first (for caching)
COPY package.json package-lock.json* ./

# Install dependencies (use npm install instead of npm ci for compatibility)
RUN npm install

# Copy remaining files
COPY . .

# Build the production bundle - swipe 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

# Quiet mode prevents Vite's build warnings from failing the build
RUN npm run build

# DEBUG: Verify build output files exist
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 static files
# ---------------------------------------------------------
FROM nginx:alpine

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

# Copy built assets into nginx's web root
COPY --from=builder /app/dist /usr/share/nginx/html

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