# syntax=docker/dockerfile:1.6
# ──────────────────────────────────────────────────────────────────────────
# Stage 0: Build codegraphcontext-rust wheel (Rust + maturin)
# Uses --build-context cgc=<path> to inject CGC source without moving files.
# ──────────────────────────────────────────────────────────────────────────
# Pin to bookworm (GLIBC 2.36) to match python:3.11-slim runtime.
FROM rust:1.85-slim-bookworm AS cgc-rust-builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 python3-pip python3-dev python3-venv \
    build-essential pkg-config libssl-dev git curl \
    && rm -rf /var/lib/apt/lists/*

# Create venv for maturin + build tools
RUN python3 -m venv /opt/venv && \
    /opt/venv/bin/pip install --no-cache-dir maturin patchelf
ENV PATH="/opt/venv/bin:${PATH}"

WORKDIR /build

# Copy CodeGraphContext source via --build-context cgc=<path>
COPY --from=cgc . /build/

# Build wheel (release mode). Use --compatibility linux since wheel only
# runs inside this image (not shared via PyPI — no manylinux needed).
RUN maturin build --release --out /build/dist --compatibility linux

# ──────────────────────────────────────────────────────────────────────────
# Stage 1: Build React frontend
# ──────────────────────────────────────────────────────────────────────────
FROM node:22-alpine AS frontend-builder

WORKDIR /app/web

# Install dependencies
COPY web/package.json web/package-lock.json* ./
RUN npm ci --no-audit --no-fund

# Build frontend — outputs to /app/src/llm_wiki/static (per vite.config outDir: "../src/llm_wiki/static")
COPY web/ ./
RUN mkdir -p /app/src/llm_wiki/static && npx vite build

# ──────────────────────────────────────────────────────────────────────────
# Stage 2: Python backend
# ──────────────────────────────────────────────────────────────────────────
FROM python:3.11-slim AS backend

# System deps for PDF processing + git
RUN apt-get update && apt-get install -y --no-install-recommends \
    git \
    curl \
    libmupdf-dev \
    mupdf-tools \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Install Python deps first (cached layer)
COPY pyproject.toml README.md ./
COPY src/ ./src/
# Copy prebuilt codegraphcontext-rust wheel from Stage 0
COPY --from=cgc-rust-builder /build/dist/*.whl /tmp/wheels/

# Install llm-wiki + codegraphcontext-rust (Rust-accelerated, MIT)
# + document converters: pdf_oxide (MIT), pdfplumber (MIT), markitdown (MIT)
# + pyarrow for CGC Parquet bulk loader
# + kuzu (embedded graph DB for CGC)
# + mcp (for MCP server: query_wiki / get_context / search_pages / read_page)
RUN pip install --no-cache-dir "." && \
    pip install --no-cache-dir /tmp/wheels/*.whl && \
    pip install --no-cache-dir \
      "markitdown[docx,pptx,xlsx,pdf,outlook]" \
      pdf_oxide \
      pdfplumber \
      pyarrow \
      "kuzu>=0.4" \
      "mcp>=1.0" && \
    rm -rf /tmp/wheels

# Copy built frontend from Stage 1 (vite outputs to /app/src/llm_wiki/static/)
COPY --from=frontend-builder /app/src/llm_wiki/static/ ./src/llm_wiki/static/

# Create workspaces dir (will be mounted)
RUN mkdir -p /workspaces

# Expose port
EXPOSE 5757

# Default environment (override via docker-compose)
ENV LLM_WIKI_PROVIDER=gemini \
    LLM_WIKI_FAST_MODEL=gemini-3.1-flash-lite-preview \
    LLM_WIKI_HEAVY_MODEL=gemini-3.1-flash-lite-preview \
    PYTHONUNBUFFERED=1

# Default vault (can be overridden)
ENV LLM_WIKI_VAULT=/workspaces/demo-unified

# Health check
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD curl -f http://localhost:5757/api/status || exit 1

# Entrypoint — bind 0.0.0.0 so port forward from host works
CMD ["sh", "-c", "python -m llm_wiki.cli serve --vault ${LLM_WIKI_VAULT} --host 0.0.0.0 --port 5757"]
