cmake_minimum_required(VERSION 3.8)
project(brain)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_components REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

find_package(interfaces REQUIRED)
find_package(brain_interfaces REQUIRED)
find_package(std_msgs REQUIRED)
find_package(smacc2 REQUIRED)
find_package(smacc2_msgs REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(behaviortree_cpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(Boost REQUIRED COMPONENTS thread)
find_package(yaml-cpp REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(Doxygen QUIET)
find_package(std_srvs REQUIRED)

# 创建一个可执行文件，将两个节点编译到一个进程中
add_executable(brain_node
  src/brain_node.cpp
  src/cerebellum_node.cpp
  src/cerebrum_node.cpp
  src/skill_manager.cpp
)

target_include_directories(brain_node PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include>)

target_compile_features(brain_node PUBLIC c_std_99 cxx_std_17)  # Require C99 and C++17

ament_target_dependencies(brain_node
  rclcpp
  rclcpp_components
  rclcpp_action
  behaviortree_cpp
  interfaces
  brain_interfaces
  std_msgs
  smacc2
  smacc2_msgs
  rclcpp_lifecycle
  ament_index_cpp
  yaml-cpp
  std_srvs
)

target_link_libraries(brain_node
  Boost::thread
  yaml-cpp
)

install(TARGETS brain_node
  DESTINATION lib/${PROJECT_NAME})

install(DIRECTORY config/
  DESTINATION share/${PROJECT_NAME}/config)

if(BUILD_TESTING)
  if(NOT DEFINED ENV{SKIP_LINT})
    find_package(ament_lint_auto REQUIRED)
  endif()
  find_package(ament_cmake_gtest REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  if(NOT DEFINED ENV{SKIP_LINT})
    ament_lint_auto_find_test_dependencies()
  endif()

  # -------------------------
  # GoogleTest targets
  # -------------------------
  ament_add_gtest(test_action_registry
    test/test_action_registry.cpp
  )
  if(TARGET test_action_registry)
    ament_target_dependencies(test_action_registry
      rclcpp
      rclcpp_action
      interfaces
      brain_interfaces
      std_srvs
    )
    target_include_directories(test_action_registry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
  endif()

  ament_add_gtest(test_bt_registry
    test/test_bt_registry.cpp
  )
  if(TARGET test_bt_registry)
    ament_target_dependencies(test_bt_registry
      rclcpp
      behaviortree_cpp
      std_srvs
    )
    target_include_directories(test_bt_registry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
  endif()

  ament_add_gtest(test_cerebellum_node
    test/test_cerebellum_node.cpp
    src/cerebellum_node.cpp
    src/skill_manager.cpp
  )
  if(TARGET test_cerebellum_node)
    target_compile_definitions(test_cerebellum_node PRIVATE BRAIN_DISABLE_SM=1)
    ament_target_dependencies(test_cerebellum_node
      rclcpp
      rclcpp_action
      behaviortree_cpp
      smacc2
      smacc2_msgs
      ament_index_cpp
      interfaces
      brain_interfaces
      std_srvs
    )
    target_include_directories(test_cerebellum_node PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
    target_link_libraries(test_cerebellum_node Boost::thread)
    target_link_libraries(test_cerebellum_node yaml-cpp)
  endif()

  ament_add_gtest(test_cerebrum_node
    test/test_cerebrum_node.cpp
    src/cerebrum_node.cpp
    src/cerebellum_node.cpp
    src/skill_manager.cpp
  )
  if(TARGET test_cerebrum_node)
    target_compile_definitions(test_cerebrum_node PRIVATE BRAIN_DISABLE_SM=1)
    ament_target_dependencies(test_cerebrum_node
      rclcpp
      rclcpp_action
      behaviortree_cpp
      interfaces
      brain_interfaces
      smacc2
      smacc2_msgs
      ament_index_cpp
      std_srvs
    )
    target_include_directories(test_cerebrum_node PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
    target_link_libraries(test_cerebrum_node Boost::thread)
    target_link_libraries(test_cerebrum_node yaml-cpp)
  endif()

  ament_add_gtest(test_sm_cerebellum
    test/test_sm_cerebellum.cpp
  )
  if(TARGET test_sm_cerebellum)
    ament_target_dependencies(test_sm_cerebellum
      rclcpp
      rclcpp_action
      smacc2
      smacc2_msgs
      interfaces
      brain_interfaces
      std_srvs
    )
    target_include_directories(test_sm_cerebellum PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
  endif()

  # ExecuteBtAction end-to-end single skill test
  ament_add_gtest(test_execute_bt_action
    test/test_execute_bt_action.cpp
    src/cerebellum_node.cpp
    src/skill_manager.cpp
  )
  if(TARGET test_execute_bt_action)
    target_compile_definitions(test_execute_bt_action PRIVATE BRAIN_DISABLE_SM=1)
    ament_target_dependencies(test_execute_bt_action
      rclcpp
      rclcpp_action
      behaviortree_cpp
      interfaces
      brain_interfaces
      smacc2
      smacc2_msgs
      ament_index_cpp
      std_srvs
    )
    target_include_directories(test_execute_bt_action PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
    target_link_libraries(test_execute_bt_action Boost::thread yaml-cpp)
  endif()

  # Extended ExecuteBtAction scenario tests (multi-skill, failure, RUN interpolation, timeout, stats, cancel)
  ament_add_gtest(test_execute_bt_action_extended
    test/test_execute_bt_action_extended.cpp
    src/cerebellum_node.cpp
    src/skill_manager.cpp
  )
  if(TARGET test_execute_bt_action_extended)
    target_compile_definitions(test_execute_bt_action_extended PRIVATE BRAIN_DISABLE_SM=1)
    ament_target_dependencies(test_execute_bt_action_extended
      rclcpp
      rclcpp_action
      behaviortree_cpp
      interfaces
      brain_interfaces
      smacc2
      smacc2_msgs
      ament_index_cpp
      std_srvs
    )
    target_include_directories(test_execute_bt_action_extended PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
    target_link_libraries(test_execute_bt_action_extended Boost::thread yaml-cpp)
  endif()

  # Additional isolated ExecuteBtAction scenario tests
  foreach(extra_test IN ITEMS unsupported run_interp timeout_continue stats cancel)
    set(test_name test_execute_bt_action_${extra_test})
    ament_add_gtest(${test_name}
      test/test_execute_bt_action_${extra_test}.cpp
      src/cerebellum_node.cpp
      src/skill_manager.cpp
    )
    if(TARGET ${test_name})
      target_compile_definitions(${test_name} PRIVATE BRAIN_DISABLE_SM=1)
      ament_target_dependencies(${test_name}
        rclcpp rclcpp_action behaviortree_cpp interfaces brain_interfaces smacc2 smacc2_msgs ament_index_cpp std_srvs)
      target_include_directories(${test_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
      target_link_libraries(${test_name} Boost::thread yaml-cpp)
    endif()
  endforeach()

  ament_add_gtest(test_cerebrum_utils
    test/test_cerebrum_utils.cpp
    src/cerebrum_node.cpp
    src/cerebellum_node.cpp
    src/skill_manager.cpp
  )
  if(TARGET test_cerebrum_utils)
    target_compile_definitions(test_cerebrum_utils PRIVATE BRAIN_DISABLE_SM=1)
    ament_target_dependencies(test_cerebrum_utils
      rclcpp rclcpp_action behaviortree_cpp interfaces brain_interfaces smacc2 smacc2_msgs ament_index_cpp std_srvs)
    target_include_directories(test_cerebrum_utils PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
    target_link_libraries(test_cerebrum_utils Boost::thread yaml-cpp)
  endif()
endif()

# --------------------------------------------------
# Doxygen documentation target (optional)
# Usage: colcon build --packages-select brain --cmake-args -DBUILD_DOCS=ON
# Generated at build/brain/docs/html
# --------------------------------------------------
option(BUILD_DOCS "Build API documentation with Doxygen" OFF)
if(BUILD_DOCS AND DOXYGEN_FOUND)
  set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile)
  if(EXISTS ${DOXYGEN_IN})
    set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.gen)
    configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
    add_custom_target(brain_docs
      COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
      COMMENT "Generating API documentation with Doxygen" VERBATIM)
  endif()
endif()

ament_package()