# Use an official Node.js image as the base
FROM node:22.14.0-alpine

# Add build argument for cache busting
ARG CACHEBUST=1

# Set the working directory in the container
WORKDIR /app

# Set environment variable to skip Chromium download
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

# Install git and Python
RUN apk update && apk add --no-cache git python3 make g++ 

# Create a symlink for python3 as python 
# This might not be strictly necessary for your current issue but good to keep if Python scripts are involved.
# RUN ln -sf /usr/bin/python3 /usr/bin/python 

# Copy package.json and package-lock.json to the container 
# COPY package*.json ./ 
COPY /src/Managing.WebApp/package.json ./ 

# Use cache busting argument to force cache invalidation
RUN echo "Cache bust: $CACHEBUST"

# Install dependencies with the --legacy-peer-deps flag to bypass peer dependency conflicts 
# Add --no-cache flag to prevent npm cache usage
RUN npm install --legacy-peer-deps --loglevel verbose --no-cache
RUN npm install -g tailwindcss postcss autoprefixer @tailwindcss/typography 

# Copy the rest of the app's source code to the container 
# COPY . . 
RUN ls -la 
COPY src/Managing.WebApp/ . 
RUN node --max-old-space-size=8192 ./node_modules/.bin/vite build 

# Build the app 
RUN npm run build 

# Use NGINX as the web server 
FROM nginx:alpine 

# Copy the built app to the NGINX web server directory 
# COPY --from=0 /app/build /usr/share/nginx/html 
COPY --from=0 /app/dist /usr/share/nginx/html 

# Expose port 80 for the NGINX web server 
EXPOSE 80 

# Start the NGINX web server 
CMD ["nginx", "-g", "daemon off;"]