# Python bindings (nanobind, stable ABI from Python 3.12). Configure with the
# interpreter that has nanobind installed, for example:
#   -DJBA_BUILD_PYTHON=ON -DPython_EXECUTABLE=bindings/python/.venv/bin/python
# The package is assembled in <build>/bindings/python/just_ba; the tests run from there.
# pip and the wheel builds go through pyproject.toml (scikit-build-core), which sets
# SKBUILD and installs the same files into the wheel.
find_package(Python 3.12 REQUIRED COMPONENTS Interpreter Development.Module Development.SABIModule)
execute_process(COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
                OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT RESULT_VARIABLE _nb)
if(NOT _nb EQUAL 0)
  message(FATAL_ERROR "nanobind not found for ${Python_EXECUTABLE}; install it (see README) or set Python_EXECUTABLE")
endif()
find_package(nanobind CONFIG REQUIRED)

# Which package this build is (pyproject.toml and packaging/variant.py set it): "cpu"
# for just-ba, "cu12" or "cu13" for the CUDA packages, "source" for a CMake build.
set(JBA_PACKAGE_VARIANT source CACHE STRING "Package variant recorded in just_ba._build_info")
option(JBA_PYTHON_STUBS "Generate the type stubs (_core.pyi) by importing the built module" ON)

set(_pkg ${CMAKE_CURRENT_BINARY_DIR}/just_ba)
nanobind_add_module(_core STABLE_ABI NB_STATIC src/module.cpp)
target_link_libraries(_core PRIVATE jba)
set_target_properties(_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${_pkg})
foreach(_cfg ${CMAKE_CONFIGURATION_TYPES})
  string(TOUPPER ${_cfg} _CFG)
  set_target_properties(_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${_CFG} ${_pkg})
endforeach()

set(JBA_CUDA_VERSION "")
if(JBA_WITH_CUDA)
  set(JBA_CUDA_VERSION ${CMAKE_CUDA_COMPILER_VERSION})
endif()
configure_file(build_info.py.in ${_pkg}/_build_info.py @ONLY)

file(GLOB _py CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/just_ba/*.py ${CMAKE_CURRENT_SOURCE_DIR}/just_ba/py.typed)
foreach(_f ${_py})
  get_filename_component(_n ${_f} NAME)
  add_custom_command(TARGET _core POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${_f} ${_pkg}/${_n})
endforeach()

if(JBA_PYTHON_STUBS)
  # Imports just_ba._core from the build directory, so the package's own files must
  # be there first (the copies above run after _core is linked).
  nanobind_add_stub(_core_stub MODULE just_ba._core OUTPUT ${_pkg}/_core.pyi
                    PYTHON_PATH ${CMAKE_CURRENT_BINARY_DIR} DEPENDS _core)
endif()

if(SKBUILD)
  # The Python files come from wheel.packages in pyproject.toml; CMake adds what it builds.
  install(TARGETS _core LIBRARY DESTINATION just_ba)
  install(FILES ${_pkg}/_build_info.py DESTINATION just_ba)
  if(JBA_PYTHON_STUBS)
    install(FILES ${_pkg}/_core.pyi DESTINATION just_ba)
  endif()
endif()

if(JBA_BUILD_TESTS)
  add_test(NAME python_tests COMMAND ${Python_EXECUTABLE} -m pytest -q ${CMAKE_CURRENT_SOURCE_DIR}/tests)
  set_tests_properties(python_tests PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}" LABELS python)
  # Real sessions, when the private capture files are present (tests/real); skipped otherwise.
  add_test(NAME real_captures COMMAND ${Python_EXECUTABLE} -m pytest -q -s ${PROJECT_SOURCE_DIR}/tests/real)
  set_tests_properties(real_captures PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}"
                       LABELS "python;real")
  # The live demo's pipeline from Python (tests/pipeline, D162): skipped without
  # just-keypoints, just-matching, poselib-gpu, Pillow and LightGlue's weights, and on
  # Windows. Minutes on the CPU (all 44 images: 7 on an M5 Pro, 40 on an i7-9750H), so
  # "slow", with an hour's timeout instead of ctest's 25 minutes: CI runs it in a job of
  # its own, on fewer images (JBA_PIPELINE_IMAGES).
  add_test(NAME python_pipeline COMMAND ${Python_EXECUTABLE} -m pytest -q -s ${PROJECT_SOURCE_DIR}/tests/pipeline)
  set_tests_properties(python_pipeline PROPERTIES ENVIRONMENT "PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}"
                       LABELS "python;pipeline;slow" TIMEOUT 3600)
endif()
