# ============================================================================
# Codeflash Unified Container - Self-Contained Build
# ============================================================================
# This Dockerfile builds everything from source without depending on pre-built
# images. Use this for client deployments and teammate onboarding.
#
# Build command:
#   docker build -f deployment/onprem-simple/Dockerfile.unified-selfcontained \
#                -t codeflash/unified:latest .
#
# ============================================================================

# ============================================================================
# Stage 1: Build aiservice
# ============================================================================
FROM --platform=$BUILDPLATFORM python:3.12-slim AS aiservice-builder

WORKDIR /app

# Install system dependencies for aiservice
RUN apt-get update && apt-get install -y \
    build-essential \
    libpq-dev \
    && rm -rf /var/lib/apt/lists/*

# Install uv
RUN pip install uv

# Copy aiservice source
COPY django/aiservice ./

# Install dependencies
RUN uv sync

# ============================================================================
# Stage 2: Build cf-api
# ============================================================================
FROM node:20-alpine AS cfapi-builder

WORKDIR /build

# Copy cf-api and common source
COPY js/cf-api ./cf-api
COPY js/common ./common

# Build common package first (cf-api depends on it)
WORKDIR /build/common
RUN npm ci && npm run build

# Install ALL dependencies for cf-api (matching Dockerfile.cfapi exactly)
WORKDIR /build/cf-api
RUN npm ci

# Replace the common package from registry with local build
RUN rm -rf node_modules/@codeflash-ai/common && \
    cp -r /build/common node_modules/@codeflash-ai/

# Clean dist folder before build
RUN rm -rf dist

# Build TypeScript (npm run build does: npm install && prisma generate && tsc && copy-assets)
RUN npm run build

# ============================================================================
# Stage 3: Build cf-webapp
# ============================================================================
FROM node:20-alpine AS webapp-builder

WORKDIR /build

# Copy cf-webapp and common source
COPY js/cf-webapp ./cf-webapp
COPY js/common ./common

# Build common package first (webapp depends on it)
WORKDIR /build/common
RUN npm ci && npm run build

# Install ALL dependencies for webapp
WORKDIR /build/cf-webapp
RUN npm ci

# Replace the common package from registry with local build
RUN rm -rf node_modules/@codeflash-ai/common && \
    cp -r /build/common node_modules/@codeflash-ai/

# Build Next.js application
RUN npm run build

# ============================================================================
# Stage 4: Final unified container
# ============================================================================
FROM node:20-alpine

# Install system dependencies
RUN apk add --no-cache \
    python3 \
    py3-pip \
    postgresql15 \
    postgresql15-client \
    supervisor \
    bash \
    openssl \
    build-base \
    libpq-dev \
    && rm -rf /var/cache/apk/*

# Install Python uv
RUN pip3 install --break-system-packages uv

WORKDIR /app

# ============================================================================
# Copy cf-api from builder
# ============================================================================
COPY --from=cfapi-builder /build/cf-api/dist ./cf-api/dist
COPY --from=cfapi-builder /build/cf-api/package.json ./cf-api/package.json
COPY --from=cfapi-builder /build/cf-api/package-lock.json ./cf-api/package-lock.json
COPY --from=cfapi-builder /build/cf-api/resend ./cf-api/resend
COPY --from=cfapi-builder /build/cf-api/github ./cf-api/github
COPY --from=cfapi-builder /build/cf-api/node_modules ./cf-api/node_modules
COPY --from=cfapi-builder /build/common /common

# Copy node_modules into dist (Azure deployment structure)
RUN cp -rL /app/cf-api/node_modules /app/cf-api/dist/ 2>/dev/null || cp -r /app/cf-api/node_modules /app/cf-api/dist/

# ============================================================================
# Copy aiservice from builder
# ============================================================================
COPY --from=aiservice-builder /app ./aiservice

# ============================================================================
# Copy cf-webapp from builder
# ============================================================================
COPY --from=webapp-builder /build/cf-webapp/.next ./cf-webapp/.next
COPY --from=webapp-builder /build/cf-webapp/public ./cf-webapp/public
COPY --from=webapp-builder /build/cf-webapp/package.json ./cf-webapp/package.json
COPY --from=webapp-builder /build/cf-webapp/package-lock.json ./cf-webapp/package-lock.json
COPY --from=webapp-builder /build/cf-webapp/next.config.mjs ./cf-webapp/next.config.mjs
COPY --from=webapp-builder /build/cf-webapp/node_modules ./cf-webapp/node_modules

# ============================================================================
# Configure PostgreSQL
# ============================================================================
RUN mkdir -p /var/lib/postgresql/data /var/run/postgresql && \
    chown -R postgres:postgres /var/lib/postgresql /var/run/postgresql

# ============================================================================
# Copy configuration files
# ============================================================================
COPY deployment/onprem-simple/supervisord.conf /etc/supervisord.conf
COPY deployment/onprem-simple/init-db.sh /app/init-db.sh
COPY deployment/onprem-simple/startup.sh /app/startup.sh

RUN chmod +x /app/init-db.sh /app/startup.sh

# ============================================================================
# Expose ports
# ============================================================================
EXPOSE 5432 8000 3001 3000

# ============================================================================
# Environment variables
# ============================================================================
ENV PGDATA=/var/lib/postgresql/data
ENV PATH="/var/lib/postgresql/bin:${PATH}"

# ============================================================================
# Start services
# ============================================================================
CMD ["/app/startup.sh"]