# Stage 1: Build the React application
FROM node:22-alpine AS builder

WORKDIR /app

# Copy package.json and lockfile
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your application code
COPY . .

# Build the Vite application (this creates the /app/dist folder inside the container)
RUN npm run build

# Stage 2: Serve the application with Nginx
FROM nginx:alpine

# Move to Nginx's public folder
WORKDIR /usr/share/nginx/html

# CRUCIAL: Remove Nginx's default "Welcome" page files completely
RUN rm -rf ./*

# Copy the compiled static assets FROM THE BUILDER STAGE
COPY --from=builder /app/dist/ .

# Copy the Nginx configuration as a template so env variables (like HASURA_ADMIN_SECRET) are substituted at runtime
COPY nginx.conf /etc/nginx/templates/default.conf.template

EXPOSE 80

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