# Stage 1: Build the React application
FROM node:18-alpine AS build_stage

WORKDIR /app

# Copy dependency files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application source
COPY . .

# Set the API base URL for the production build
# Since everything is unified under Nginx on the same host, we use a relative path.
ENV REACT_APP_API_BASE_URL=/api/v1

# Build the production-ready static files
RUN npm run build

# Stage 2: Serve the static files using Nginx
FROM nginx:alpine

# Remove default Nginx static files
RUN rm -rf /usr/share/nginx/html/*

# Copy the build output from the first stage
COPY --from=build_stage /app/build /usr/share/nginx/html

# Expose port 80 (Nginx default)
EXPOSE 80

# Start Nginx
CMD ["nginx", "-g", "daemon off;"]