cmake_minimum_required(VERSION 3.16)

# ── Python interpreter / development files ─────────
# Needed by pybind11's new tools mode (python_add_library).
find_package(Python COMPONENTS Interpreter Development.Module QUIET)
if(NOT Python_FOUND)
    message(FATAL_ERROR
        "Python development files not found (need Python.h + import library). "
        "Install CPython development package or set Python_ROOT_DIR.")
endif()
message(STATUS "VDE Python: interpreter ${Python_VERSION} (${Python_EXECUTABLE})")

# ── pybind11 (offline-first resolution) ─────────────
# 1) -DVDE_PYBIND11_DIR=<dir containing pybind11Config.cmake>
# 2) find_package(pybind11 CONFIG) – system / CMAKE_PREFIX_PATH
# 3) local pip install: `python -c "import pybind11;print(pybind11.get_cmake_dir())"`
#    (works fully offline; verified with pybind11 3.0.4 + CPython 3.14)
# 4) FetchContent network fallback (only when nothing local is available)
if(DEFINED VDE_PYBIND11_DIR)
    list(APPEND CMAKE_PREFIX_PATH "${VDE_PYBIND11_DIR}")
endif()

find_package(pybind11 CONFIG QUIET)

if(NOT pybind11_FOUND)
    find_package(Python COMPONENTS Interpreter QUIET)
    if(Python_EXECUTABLE)
        execute_process(
            COMMAND "${Python_EXECUTABLE}" -c
                    "import pybind11;print(pybind11.get_cmake_dir())"
            OUTPUT_VARIABLE _vde_pybind11_cmake_dir
            OUTPUT_STRIP_TRAILING_WHITESPACE
            ERROR_QUIET)
        if(_vde_pybind11_cmake_dir AND EXISTS "${_vde_pybind11_cmake_dir}")
            message(STATUS "VDE Python: using local pip pybind11 at ${_vde_pybind11_cmake_dir}")
            find_package(pybind11 CONFIG QUIET
                         PATHS "${_vde_pybind11_cmake_dir}" NO_DEFAULT_PATH)
        endif()
    endif()
endif()

if(NOT pybind11_FOUND)
    message(STATUS "VDE Python: local pybind11 not found; falling back to FetchContent (needs network)")
    include(FetchContent)
    FetchContent_Declare(
        pybind11
        URL https://github.com/pybind/pybind11/archive/refs/tags/v2.11.1.tar.gz
        URL_HASH SHA256=d475978da0cdc2d43dd73e309f0f53263607dd08f31d5facb02c8110e4a1ebd8
    )
    FetchContent_MakeAvailable(pybind11)
endif()

# ── _vde native module ─────────────────────────────
pybind11_add_module(_vde
    src/bind_module.cpp
    src/bind_core.cpp
    src/bind_curves.cpp
    src/bind_mesh.cpp
    src/vde_brep.cpp
    src/vde_sdf.cpp
    src/vde_cam.cpp
    src/vde_assembly.cpp
)

target_include_directories(_vde
    PRIVATE ../include
)

target_link_libraries(_vde PRIVATE vde)

# Python 3.8+ compatibility
target_compile_features(_vde PRIVATE cxx_std_17)
