# syntax=docker/dockerfile:1.6
# Highly optimized multi-stage Dockerfile for Next.js 15 + Playwright + Enhanced Screenshot Service
# Uses BuildKit 1.6+ features, full CPU utilization, and aggressive caching

ARG NODE_VERSION=20.11.1
ARG PNPM_VERSION=8.15.1

FROM node:${NODE_VERSION}-slim AS base

# Set environment for maximum performance and parallelization
ENV DEBIAN_FRONTEND=noninteractive
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NPM_CONFIG_FUND=false
ENV NPM_CONFIG_AUDIT=false
ENV CI=true
ENV MAKEFLAGS="-j$(nproc)"
ENV UV_THREADPOOL_SIZE=128

# Enable pnpm with corepack for faster package management
RUN corepack enable && corepack prepare pnpm@8.15.1 --activate

WORKDIR /app

# Install system dependencies with maximum parallelization and aggressive caching
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    --mount=type=cache,target=/tmp,sharing=locked \
    set -eux && \
    # Update package lists
    apt-get update && \
    # Install all dependencies in a single transaction with parallel processing
    apt-get install -y --no-install-recommends --parallel=$(nproc) \
        # Core system tools
        wget ca-certificates curl gnupg lsb-release \
        # Build tools for native compilation (enables faster installs)
        build-essential python3 make g++ pkg-config \
        # Chromium and browser dependencies (optimized order for dependency resolution)
        chromium fonts-liberation libappindicator3-1 libasound2 \
        libatk-bridge2.0-0 libatk1.0-0 libcups2 libdbus-1-3 libdrm2 \
        libgbm1 libnspr4 libnss3 libx11-xcb1 libxcomposite1 libxdamage1 \
        libxrandr2 xdg-utils libxss1 libgconf-2-4 libxtst6 \
        libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libgtk-3-0 libxshmfence1 && \
    # Aggressive cleanup in same layer
    apt-get autoremove -y && apt-get autoclean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* /usr/share/man/*

# High-performance dependency installation stage
FROM base AS deps

# Copy package files for optimal layer caching
COPY package.json pnpm-lock.yaml* package-lock.json* yarn.lock* .npmrc* ./

# Install dependencies with maximum parallelization and caching
RUN --mount=type=cache,id=pnpm,target=/pnpm/store,sharing=locked \
    --mount=type=cache,id=npm,target=/root/.npm,sharing=locked \
    --mount=type=cache,id=node-gyp,target=/root/.node-gyp,sharing=locked \
    set -eux && \
    # Configure optimal settings for parallel builds
    export JOBS=$(nproc) && \
    export NPM_CONFIG_JOBS=$(nproc) && \
    export PNPM_CONFIG_NETWORK_CONCURRENCY=$(nproc) && \
    # Install with the fastest available package manager
    if [ -f pnpm-lock.yaml ]; then \
        echo "🚀 Installing with pnpm ($(nproc) parallel jobs)" && \
        pnpm config set network-concurrency $(nproc) && \
        pnpm config set child-concurrency $(nproc) && \
        pnpm install --frozen-lockfile --prefer-offline --reporter=silent; \
    elif [ -f package-lock.json ]; then \
        echo "📦 Installing with npm ($(nproc) parallel jobs)" && \
        npm ci --prefer-offline --no-audit --no-fund --maxsockets=$(nproc) --silent; \
    elif [ -f yarn.lock ]; then \
        echo "🧶 Installing with yarn ($(nproc) parallel jobs)" && \
        yarn install --frozen-lockfile --prefer-offline --silent --network-concurrency $(nproc); \
    else \
        echo "📦 Installing with npm fallback" && \
        npm install --prefer-offline --no-audit --no-fund --maxsockets=$(nproc) --silent; \
    fi

# Install Playwright with aggressive caching and parallel downloads
RUN --mount=type=cache,id=playwright,target=/root/.cache/ms-playwright,sharing=locked \
    --mount=type=cache,id=playwright-deps,target=/tmp/playwright-deps,sharing=locked \
    set -eux && \
    echo "🎭 Installing Playwright with parallel downloads" && \
    # Use parallel installation for Playwright browsers
    npx playwright install --with-deps chromium --force && \
    npx playwright install-deps --force

# High-performance build stage with maximum parallelization
FROM deps AS builder

# Copy source code (optimize for layer caching)
COPY --link . .

# Generate Prisma client with caching
RUN --mount=type=cache,id=prisma,target=/app/.prisma,sharing=locked \
    echo "🔧 Generating Prisma client" && \
    npx prisma generate

# Build Next.js with maximum optimization and parallelization
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ENV NODE_OPTIONS="--max-old-space-size=4096"
ENV NEXT_BUILD_WORKERS=$(nproc)

RUN --mount=type=cache,id=nextjs,target=/app/.next/cache,sharing=locked \
    --mount=type=cache,id=turbopack,target=/app/.turbo,sharing=locked \
    set -eux && \
    echo "🔨 Building Next.js with $(nproc) workers" && \
    # Build with the fastest available package manager
    if [ -f pnpm-lock.yaml ]; then \
        echo "� Building with pnpm" && \
        pnpm build; \
    elif [ -f yarn.lock ]; then \
        echo "🧶 Building with yarn" && \
        yarn build; \
    else \
        echo "� Building with npm" && \
        npm run build; \
    fi && \
    # Optimize build artifacts
    echo "🧹 Optimizing build artifacts" && \
    find .next -name '*.map' -delete && \
    find .next -name '*.d.ts' -delete

# Ultra-optimized production runtime stage
FROM base AS runner

# Create application user for security
RUN groupadd --gid 1001 nodejs && \
    useradd --uid 1001 --gid nodejs --shell /bin/bash --create-home nextjs

# Copy built application with optimal linking
COPY --from=builder --chown=nextjs:nodejs --link /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs --link /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs --link /app/public ./public
COPY --from=builder --chown=nextjs:nodejs --link /app/prisma ./prisma
COPY --from=builder --chown=nextjs:nodejs --link /app/package.json ./package.json

# Copy essential runtime dependencies (optimized for dual-session screenshots)
COPY --from=deps --chown=nextjs:nodejs --link /app/node_modules ./node_modules

# Copy service files for enhanced screenshot functionality
COPY --from=builder --chown=nextjs:nodejs --link /app/lib ./lib
COPY --from=builder --chown=nextjs:nodejs --link /app/components ./components
COPY --from=builder --chown=nextjs:nodejs --link /app/app ./app

# Create and optimize directories for screenshots and sessions
RUN mkdir -p screenshots videos .tradingview-session .tradingview-session-ai .tradingview-session-diy && \
    chown -R nextjs:nodejs /app && \
    # Optimize file permissions for better performance
    find /app -type f -name "*.js" -exec chmod 644 {} \; && \
    find /app -type f -name "*.json" -exec chmod 644 {} \; && \
    find /app -type d -exec chmod 755 {} \; && \
    chmod +x node_modules/.bin/* 2>/dev/null || true

# Switch to non-root user
USER nextjs

# Expose port
EXPOSE 3000

# Production environment variables for maximum performance
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV NODE_OPTIONS="--max-old-space-size=2048 --optimize-for-size"
ENV UV_THREADPOOL_SIZE=64
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
ENV CHROMIUM_PATH=/usr/bin/chromium
ENV DISABLE_CHROME_SANDBOX=true
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
# Enhanced screenshot service specific settings
ENV SCREENSHOT_PARALLEL_SESSIONS=true
ENV SCREENSHOT_MAX_WORKERS=4
ENV BROWSER_POOL_SIZE=2

# Enhanced health check for dual-session screenshot service
HEALTHCHECK --interval=15s --timeout=5s --start-period=30s --retries=5 \
    CMD curl -f http://localhost:3000/api/health || \
        curl -f http://localhost:3000/ || exit 1

# Start the optimized application
CMD ["node", "server.js"]

# Development stage for faster dev builds
FROM deps AS development

WORKDIR /app

# Copy source for development (with file watching optimization)
COPY --link . .

# Generate Prisma client for development
RUN npx prisma generate

# Create development directories
RUN mkdir -p screenshots videos .tradingview-session .tradingview-session-ai .tradingview-session-diy

# Development environment optimizations
ENV NODE_ENV=development
ENV NEXT_TELEMETRY_DISABLED=1
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
ENV CHROMIUM_PATH=/usr/bin/chromium
ENV DISABLE_CHROME_SANDBOX=true
ENV CHOKIDAR_USEPOLLING=false
ENV WATCHPACK_POLLING=false

# Expose port
EXPOSE 3000

# Start development server with optimizations
CMD ["npm", "run", "dev:docker"]
