# Stage 1: Build the React application
FROM node:20-alpine AS build
WORKDIR /app

# Copy package management files first for better caching
COPY package.json yarn.lock* package-lock.json* ./

# Install dependencies (using npm to respect package-lock.json if it exists, with legacy-peer-deps for compatibility)
RUN npm install --legacy-peer-deps

# Copy the source code
COPY . .

# Build the application
RUN npm run build

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

WORKDIR /usr/share/nginx/html

# Remove default Nginx welcome page
RUN rm -rf ./*

# Copy built assets from the previous stage
COPY --from=build /app/build/ .

# Copy the custom Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

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