# Build Stage
FROM node:20-alpine AS build

WORKDIR /app

# Copy package files and install dependencies
COPY package*.json ./
RUN npm ci

# Copy the rest of the app and build it
COPY . .
RUN npm run build

# Production Stage
FROM nginx:alpine

# Copy the built app from the build stage
COPY --from=build /app/dist /usr/share/nginx/html

# Copy custom nginx config and environment script
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY env.sh /docker-entrypoint.d/40-env.sh

# Make the environment script executable
RUN chmod +x /docker-entrypoint.d/40-env.sh

# Create a place for the index.html to include the env-config.js script
RUN sed -i '/<head>/a \    <script src="/env-config.js"></script>' /usr/share/nginx/html/index.html || echo "Failed to inject env-config script tag"

# Expose port 3800 (changed from 80)
EXPOSE 3800

# Start Nginx server (the entrypoint scripts will run first)
CMD ["nginx", "-g", "daemon off;"]
