# Use the argument for node version (defaults to 'lts' if not provided)
ARG NODE_VERSION=lts

# Stage 1: Build React App
FROM node:${NODE_VERSION}-alpine AS development

# Set the Node.js environment to development
ENV NODE_ENV=development

# Define build arguments for the API base URLs
ARG REACT_APP_SERVER_SOCKET_API_BASE_URL
ARG REACT_APP_SERVER_REST_API_BASE_URL
ARG REACT_APP_SERVER_MARKETPLACE_URL

# Set environment variables for the API base URLs using the build arguments
ENV REACT_APP_SERVER_SOCKET_API_BASE_URL=${REACT_APP_SERVER_SOCKET_API_BASE_URL}
ENV REACT_APP_SERVER_REST_API_BASE_URL=${REACT_APP_SERVER_REST_API_BASE_URL}
ENV REACT_APP_SERVER_MARKETPLACE_URL=${REACT_APP_SERVER_MARKETPLACE_URL}

# Set working directory for frontend code
WORKDIR /frontend

# Copy package.json and package-lock.json for npm install
COPY package*.json ./

# Install the latest npm version
RUN npm install -g npm

# Install dependencies (this includes react-scripts)
RUN npm install --legacy-peer-deps

# Copy the rest of the application code
COPY . .

# Run the build command (build the React app)
RUN npm run build

# Stage 2: Serve with Nginx
FROM nginx:alpine

# Copy the built React files from the build stage into Nginx's default HTML folder
COPY --from=development /frontend/build /usr/share/nginx/html

# Optionally copy a custom Nginx config (if needed)
COPY nginx-container.conf /etc/nginx/conf.d/default.conf

# Expose port 80 for Nginx (default HTTP port)
EXPOSE 80

# Start Nginx in the foreground (this is required to keep the container running)
CMD ["nginx", "-g", "daemon off;"]
