# Multi-stage build - using slim for better compatibility
FROM node:20-slim AS builder
WORKDIR /app

# Skip browser downloads
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
ENV CYPRESS_INSTALL_BINARY=0

# Install build dependencies
RUN apt-get update && apt-get install -y python3 make g++ git && rm -rf /var/lib/apt/lists/*

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install --legacy-peer-deps --prefer-offline --no-audit

# Copy source
COPY . .

# Build with memory optimization (3GB for slim image)
ENV NODE_OPTIONS="--max-old-space-size=3072"
RUN npm run build

# Runtime stage - use alpine for smaller size
FROM node:20-alpine
WORKDIR /app

# Copy built output
COPY --from=builder /app/.output ./.output

ENV PORT=9202
ENV HOST=0.0.0.0
EXPOSE 9102

CMD ["node", ".output/server/index.mjs"]
