# 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 \
        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

# Ensure a recent CMake is available (system cmake in slim images can be too old for vcpkg)
RUN python -m pip install --upgrade pip cmake

# 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

# 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/depot_tools \
    --mount=type=cache,target=/build/third_party/pdfium/checkout \
    sed -i 's/\r$//' ./third_party/pdfium/build_pdfium.sh && bash ./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/

# Configure and build in the same layer. Keeping these operations together
# avoids Docker/overlayfs timestamp skew causing Ninja to regenerate
# build.ninja indefinitely ("manifest still dirty after 100 tries"). The
# source tree is immutable for this image, so automatic build-system
# regeneration is unnecessary once configure has completed.
RUN --mount=type=cache,target=/root/.cache \
    --mount=type=cache,target=/opt/vcpkg/downloads \
    cmake --preset linux-release \
    -DPDFENGINE_BUILD_TESTS=OFF \
    -DPDFENGINE_WITH_PDFIUM=ON \
    -DPDFENGINE_WITH_SKIA=OFF \
    -DPDFENGINE_WITH_QPDF=ON \
    -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake \
    -DCMAKE_SUPPRESS_REGENERATION=ON \
    && cmake --build out/build/linux-release

# Stage 2: 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 \
        libgl1 \
        libglib2.0-0 \
        libgomp1 \
        libsm6 \
        libxext6 \
    && rm -rf /var/lib/apt/lists/*

# Install the extension globally so it's not shadowed by the volume mount ./gateway:/home/app
COPY --from=builder /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"]
