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

# Install git (only needed for npm postinstall scripts)
RUN apk add --no-cache git

WORKDIR /app

# Copy package files
COPY package.json ./
RUN npm install

# Copy the rest of the source tree
COPY . .

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

# DEBUG: Confirm build output exists before proceeding
RUN echo "=== BUILD OUTPUT CHECK ===" && \
    ls -la /app/dist && \
    cat /app/dist/index.html 2>/dev/null | head -n 5 || echo "ERROR: INDEX NOT FOUND - build may have failed silently"

# ---------------------------------------------------------
#  Production stage – tiny static server (nginx)
# ---------------------------------------------------------
FROM nginx:alpine

COPY nginx.conf /etc/nginx/conf.d/default.conf

COPY --from=builder /app/dist /usr/share/nginx/html

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