# Multi-stage build for Vue.js application
FROM node:18-alpine AS builder

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install all dependencies (including dev dependencies for build)
RUN npm ci

# Copy source code
COPY . .

# Accept build-time environment variables
ARG VITE_API_BASE_URL
ARG VITE_API_TIMEOUT
ARG VITE_APP_TITLE
ARG VITE_APP_VERSION
ARG VITE_AUTH_TOKEN_KEY
ARG VITE_REFRESH_TOKEN_KEY
ARG VITE_ENABLE_ANALYTICS
ARG VITE_ENABLE_DEBUG
ARG VITE_GOOGLE_ANALYTICS_ID
ARG VITE_SENTRY_DSN

# Set environment variables for the build
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
ENV VITE_API_TIMEOUT=$VITE_API_TIMEOUT
ENV VITE_APP_TITLE=$VITE_APP_TITLE
ENV VITE_APP_VERSION=$VITE_APP_VERSION
ENV VITE_AUTH_TOKEN_KEY=$VITE_AUTH_TOKEN_KEY
ENV VITE_REFRESH_TOKEN_KEY=$VITE_REFRESH_TOKEN_KEY
ENV VITE_ENABLE_ANALYTICS=$VITE_ENABLE_ANALYTICS
ENV VITE_ENABLE_DEBUG=$VITE_ENABLE_DEBUG
ENV VITE_GOOGLE_ANALYTICS_ID=$VITE_GOOGLE_ANALYTICS_ID
ENV VITE_SENTRY_DSN=$VITE_SENTRY_DSN

# Build the application
RUN npm run build

# Clean up dev dependencies to reduce image size
RUN npm prune --production

# Production stage
FROM nginx:alpine AS production

# Copy built assets from builder stage
COPY --from=builder /app/dist /usr/share/nginx/html

# Copy custom nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Expose port 80
EXPOSE 80

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