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

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

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

# 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 Vite and its plugins)
RUN npm install --legacy-peer-deps

# Copy the rest of the application code
COPY . .

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

# Stage 2: Serve with Nginx
FROM nginx:alpine

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

# Optionally copy a custom Nginx config (if needed)
COPY nginx.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;"]