# Multi-stage build for Playwright Web Proxy

# Global build-time arguments — declared before any FROM so defaults apply to all stages
ARG NEXT_PUBLIC_API_URL=https://cahootravel.com
ARG NEXT_PUBLIC_WS_URL=wss://cahootravel.com
ARG NEXT_PUBLIC_GOOGLE_CLIENT_ID=1044280434135-pggpbqn85aamn6hhpqoff2hildl5o8t0.apps.googleusercontent.com

# Stage 1: Build backend
FROM node:24.13-alpine AS backend-builder

WORKDIR /app

# Install dependencies (including dev for tsc)
# Skip Playwright browser download — browsers are installed in the production stage
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
COPY package*.json ./
RUN npm ci

# Copy source code and build
COPY tsconfig.json ./
COPY src ./src
RUN npm run build

# Stage 2: Build frontend (client)
FROM node:24.13-alpine AS frontend-builder

ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_WS_URL
ARG NEXT_PUBLIC_GOOGLE_CLIENT_ID

WORKDIR /app/client

ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
# Install dependencies
COPY client/package*.json ./
RUN npm ci

# Copy source code
COPY client ./

# Bake public vars into the Next.js build
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_WS_URL=$NEXT_PUBLIC_WS_URL
ENV NEXT_PUBLIC_GOOGLE_CLIENT_ID=$NEXT_PUBLIC_GOOGLE_CLIENT_ID

# Build Next.js app
RUN npm run build

# Stage 2b: Build frontend (client-cahoo)
FROM node:24.13-alpine AS frontend-cahoo-builder

ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_WS_URL
ARG NEXT_PUBLIC_GOOGLE_CLIENT_ID

WORKDIR /app/client-cahoo

# Install dependencies
COPY client-cahoo/package*.json ./
RUN npm ci

# Copy source code
COPY client-cahoo ./

# Bake public vars into the Next.js build
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
ENV NEXT_PUBLIC_WS_URL=$NEXT_PUBLIC_WS_URL
ENV NEXT_PUBLIC_GOOGLE_CLIENT_ID=$NEXT_PUBLIC_GOOGLE_CLIENT_ID

# Build Next.js app
RUN npm run build

# Stage 3: Production image
FROM mcr.microsoft.com/playwright:v1.40.0-jammy

WORKDIR /app

# Install Node.js 24
RUN curl -fsSL https://deb.nodesource.com/setup_24.x | bash - && \
    apt-get install -y nodejs && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Copy backend — production deps only
COPY package*.json ./
RUN npm ci --only=production
COPY --from=backend-builder /app/dist ./dist

# Copy frontend build (client)
COPY --from=frontend-builder /app/client/.next ./client/.next
COPY --from=frontend-builder /app/client/node_modules ./client/node_modules
COPY --from=frontend-builder /app/client/package.json ./client/
COPY --from=frontend-builder /app/client/public ./client/public

# Copy frontend build (client-cahoo)
COPY --from=frontend-cahoo-builder /app/client-cahoo/.next ./client-cahoo/.next
COPY --from=frontend-cahoo-builder /app/client-cahoo/node_modules ./client-cahoo/node_modules
COPY --from=frontend-cahoo-builder /app/client-cahoo/package.json ./client-cahoo/
COPY --from=frontend-cahoo-builder /app/client-cahoo/public ./client-cahoo/public

# Create logs directory
RUN mkdir -p /app/logs

# Install Playwright browsers
RUN npx playwright install chromium && \
    npx playwright install-deps chromium

# Set environment variables
ENV NODE_ENV=production
ENV PORT=3008
ENV HOST=0.0.0.0

# Expose ports
EXPOSE 3008 3009 3010

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

# Start script
COPY docker-entrypoint.sh /app/
RUN chmod +x /app/docker-entrypoint.sh

ENTRYPOINT ["/app/docker-entrypoint.sh"]
