# Stage 1: Build the Angular application
FROM oven/bun:alpine AS builder

# Install nodejs to support Angular prerendering worker threads
RUN apk add --no-cache nodejs

# Set working directory
WORKDIR /app

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

# Install dependencies
RUN bun install

# Copy source code
COPY . .

# Build the application for production
RUN bun run build

# Stage 2: Production image
FROM oven/bun:alpine AS production

# Set working directory
WORKDIR /app

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

# Install only production dependencies
RUN bun install --production

# Copy built application from builder stage
COPY --from=builder /app/dist ./dist

# Set environment to production
ENV NODE_ENV=production

# Expose port (default Express port)
EXPOSE 4000

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
  CMD bun -e "require('http').get('http://localhost:4000', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"

# Start the application
CMD ["bun", "run", "dist/vac-optimizer/server/server.mjs"]
