cmake_minimum_required(VERSION 3.21)

# Build an example and register it as a CTest smoke test.
# Examples that are self-terminating (fixed sleep → net.stop()) pass when
# they exit 0 within TIMEOUT seconds. OpenCV/UI examples are excluded.
function(kpn_example name)
    add_executable(${name} ${name}/main.cpp)
    target_link_libraries(${name} PRIVATE kpn)
    add_test(NAME example_${name} COMMAND ${name})
    set_tests_properties(example_${name} PROPERTIES
        TIMEOUT 15
        LABELS  examples
    )
endfunction()

kpn_example(01_hello_pipeline)
kpn_example(02_named_ports)
kpn_example(03_multi_output)
kpn_example(04_storage_policy)
kpn_example(05_error_handling)
kpn_example(06_watchdog)
set_tests_properties(example_06_watchdog PROPERTIES TIMEOUT 40)
kpn_example(10_static_hello_pipeline)
kpn_example(11_static_fanout)
kpn_example(15_node_error_handler)
kpn_example(16_event_callbacks)
if(KPN_WEB_DEBUG)
    kpn_target_enable_web_debug(06_watchdog)

    add_executable(14_debug_hub 14_debug_hub/main.cpp)
    target_link_libraries(14_debug_hub PRIVATE kpn)
    kpn_target_enable_web_debug(14_debug_hub)
endif()
# 07 and 08 are Python scripts — no compiled target needed.

# 09 requires OpenCV — only build if found
find_package(OpenCV QUIET COMPONENTS core imgproc highgui videoio)
if(OpenCV_FOUND)
    # Hybrid Python example: kpn_opencv module (requires both OpenCV and nanobind)
    if(KPN_BUILD_PYTHON)
        nanobind_add_module(kpn_opencv 09_opencv_cellshade/kpn_opencv.cpp)
        target_link_libraries(kpn_opencv PRIVATE kpn ${OpenCV_LIBS})
        target_compile_definitions(kpn_opencv PRIVATE KPN_BUILD_PYTHON)
        message(STATUS "KPN++ kpn_opencv Python module: building (OpenCV ${OpenCV_VERSION})")
    endif()
    add_executable(09_opencv_cellshade 09_opencv_cellshade/main.cpp)
    target_link_libraries(09_opencv_cellshade PRIVATE kpn ${OpenCV_LIBS})

    add_executable(12_static_cellshade 12_static_cellshade/main.cpp)
    target_link_libraries(12_static_cellshade PRIVATE kpn ${OpenCV_LIBS})

    add_executable(13_debug_cellshade 13_debug_cellshade/main.cpp)
    target_link_libraries(13_debug_cellshade PRIVATE kpn ${OpenCV_LIBS})

    if(KPN_WEB_DEBUG)
        kpn_target_enable_web_debug(09_opencv_cellshade)
        kpn_target_enable_web_debug(13_debug_cellshade)
    endif()
    message(STATUS "KPN++ example 09_opencv_cellshade: OpenCV ${OpenCV_VERSION} found — building")
else()
    message(STATUS "KPN++ example 09_opencv_cellshade: OpenCV not found — skipping")
endif()
