# Build stage
FROM quay.io/fedora/nodejs-18:latest as build

# Stay as root during the build stage to avoid permission issues
USER root

# Set up working directory
WORKDIR /opt/app-root/src

# Copy package.json files
COPY package*.json ./

# Install dependencies with a simpler approach to avoid permission issues
ENV NODE_ENV=production
ENV DISABLE_ESLINT_PLUGIN=true
ENV ESLINT_NO_DEV_ERRORS=true

# Install dependencies
RUN npm install --production=false --no-package-lock

# Copy the rest of the application
COPY . ./

# Build the application with ESLint disabled
RUN DISABLE_ESLINT_PLUGIN=true npm run build

# Production stage
FROM quay.io/fedora/nginx-126

# Copy build output directly to NGINX html directory
COPY --from=build /opt/app-root/src/build/ /usr/share/nginx/html/

# Copy NGINX configuration
COPY nginx.conf "${NGINX_CONF_PATH}"

# Copy startup script that will generate env-config.js at runtime
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

# Set correct permissions for NGINX
USER root
RUN chmod -R a+rwX /usr/share/nginx/html/
USER 1001

# Run the startup script instead of nginx directly
ENTRYPOINT ["/docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]
