# Stage 1: Builder
FROM python:3.11-slim AS builder

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    VCPKG_FORCE_SYSTEM_BINARIES=1

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        ninja-build \
        pkg-config \
        git \
        fonts-liberation \
        libjpeg-dev \
        zlib1g-dev \
        libpng-dev \
        curl \
        zip \
        unzip \
        tar \
        autoconf \
        autoconf-archive \
        automake \
        libtool \
        lld \
    && rm -rf /var/lib/apt/lists/* \
    && ln -sf /usr/bin/ld.lld /usr/bin/ld

WORKDIR /build

# Install vcpkg
RUN git clone https://github.com/microsoft/vcpkg.git /opt/vcpkg \
    && /opt/vcpkg/bootstrap-vcpkg.sh -disableMetrics
ENV VCPKG_ROOT=/opt/vcpkg

# Cache vcpkg dependencies in a separate layer
COPY vcpkg.json ./
RUN --mount=type=cache,target=/root/.cache \
    --mount=type=cache,target=/opt/vcpkg/downloads \
    /opt/vcpkg/vcpkg install --triplet x64-linux

# Install depot_tools globally with caching
RUN --mount=type=cache,target=/opt/depot_tools \
    if [ ! -d /opt/depot_tools/.git ]; then \
        git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git /opt/depot_tools; \
    fi
ENV PATH="/opt/depot_tools:${PATH}"

# Build PDFium for Linux (heavily cached, keeping the huge source tree out of the image layer)
COPY third_party/pdfium/ ./third_party/pdfium/
RUN --mount=type=cache,target=/build/third_party/pdfium/checkout \
    ./third_party/pdfium/build_pdfium.sh

# Copy everything needed for the engine and bindings build
COPY CMakeLists.txt CMakePresets.json ./
COPY cmake/ ./cmake/
COPY engine/ ./engine/
COPY bindings/ ./bindings/
COPY gateway/ ./gateway/
COPY corpus/ ./corpus/

# Configure CMake with tests enabled
RUN --mount=type=cache,target=/root/.cache \
    --mount=type=cache,target=/opt/vcpkg/downloads \
    cmake --preset linux-release \
    -DPDFENGINE_BUILD_TESTS=ON \
    -DPDFENGINE_WITH_PDFIUM=ON \
    -DPDFENGINE_WITH_SKIA=OFF \
    -DPDFENGINE_WITH_QPDF=ON \
    -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake

# Build all targets including tests and pdfengine_py
RUN cmake --build out/build/linux-release

# Stage 2: Tester
FROM builder AS tester
RUN ctest --test-dir out/build/linux-release --output-on-failure

# Stage 3: Runtime
FROM python:3.11-slim

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1 \
    PIP_NO_CACHE_DIR=1 \
    PIP_DISABLE_PIP_VERSION_CHECK=1 \
    PORT=8765

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libjpeg62-turbo \
        zlib1g \
        libpng16-16 \
        fonts-liberation \
    && rm -rf /var/lib/apt/lists/*

# Install the extension globally so it's not shadowed by the volume mount ./gateway:/home/app
COPY --from=tester /build/gateway/pdfengine*.so /usr/local/lib/python3.11/site-packages/

RUN groupadd --system app \
    && useradd --system --gid app --create-home --home-dir /home/app app

WORKDIR /home/app

# Copy gateway Python code
COPY gateway/pyproject.toml gateway/README.md ./
RUN python -m pip install --upgrade pip \
    && pip install --no-cache-dir -e ".[dev]"

COPY gateway/app ./app
COPY gateway/tests ./tests

RUN chown -R app:app /home/app
USER app

EXPOSE 8765

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8765"]
