# Build stage
FROM harbor.dvirlabs.com/base-images/node:18-alpine as build

WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application code
COPY . .

# Build the application
RUN npm run build

# Production stage
FROM harbor.dvirlabs.com/base-images/nginx:alpine

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

# Copy nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Create entrypoint script to inject env variables
RUN echo '#!/bin/sh' > /docker-entrypoint.d/40-generate-env.sh && \
    echo 'echo "window.ENV = { VITE_API_URL: \"${VITE_API_URL}\" };" > /usr/share/nginx/html/env.js' >> /docker-entrypoint.d/40-generate-env.sh && \
    chmod +x /docker-entrypoint.d/40-generate-env.sh

# Expose port
EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
