// Disable SMACC2 state machine for this unit test to avoid service initialization issues
#ifndef BRAIN_DISABLE_SM
#define BRAIN_DISABLE_SM 1
#endif

#include <gtest/gtest.h>
#include <rclcpp/rclcpp.hpp>
#include <rclcpp_action/rclcpp_action.hpp>
#include "brain/cerebellum_node.hpp"
#include "brain/constants.hpp"
// Legacy MoveToPosition action was removed from automatic server startup.
// Adapt test to validate that CerebellumNode brings up the unified ExecuteBtAction server.
#include "brain_interfaces/action/execute_bt_action.hpp"

using ExecuteBtAction = brain_interfaces::action::ExecuteBtAction;

TEST(CerebellumNode, StartsAndProvidesActionServer)
{
  rclcpp::init(0, nullptr);
  auto options = rclcpp::NodeOptions();
  auto cerebellum = std::make_shared<brain::CerebellumNode>(options);

  auto client = rclcpp_action::create_client<ExecuteBtAction>(cerebellum, brain::kExecuteBtActionName);
  rclcpp::executors::SingleThreadedExecutor exec;
  exec.add_node(cerebellum);

  // Wait a bit for server availability
  auto start = std::chrono::steady_clock::now();
  bool available = false;
  while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) {
    exec.spin_some();
    available = client->action_server_is_ready();
    if (available) {break;}
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
  }
  EXPECT_TRUE(available) << "ExecuteBtAction server not ready within timeout";

  rclcpp::shutdown();
}
