#pragma once

#include <memory>
#include <string>

#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"

#include "calibration_chassis_interfaces/action/execute_motion_primitive.hpp"
#include "calibration_chassis_interfaces/srv/get_chassis_readiness.hpp"

#include "vehicle_agent_gateway/tcp_client.hpp"

namespace vehicle_agent_gateway
{

// 底盘网关节点：
// 1. 暴露与 chassis_calibration_service 完全相同的 ROS2 接口，orchestrator 零感知。
// 2. 收到请求后通过 TcpClient 发帧到 Windows 车端，等待响应后回填 ROS2 结果。
// 3. 当前 payload 为空占位；后续接入 protobuf 序列化时只需修改本文件的序列化/反序列化部分。
class ChassisBridgeNode : public rclcpp::Node
{
public:
  explicit ChassisBridgeNode(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());

private:
  using ReadinessSrv = calibration_chassis_interfaces::srv::GetChassisReadiness;
  using ExecuteTask  = calibration_chassis_interfaces::action::ExecuteMotionPrimitive;
  using GoalHandle   = rclcpp_action::ServerGoalHandle<ExecuteTask>;

  rclcpp::Service<ReadinessSrv>::SharedPtr readiness_service_;
  rclcpp_action::Server<ExecuteTask>::SharedPtr execute_task_server_;

  std::string chassis_host_;
  uint16_t chassis_port_;
  int timeout_ms_;

  void handle_readiness(
    const std::shared_ptr<ReadinessSrv::Request> request,
    std::shared_ptr<ReadinessSrv::Response> response);

  rclcpp_action::GoalResponse handle_goal(
    const rclcpp_action::GoalUUID & uuid,
    std::shared_ptr<const ExecuteTask::Goal> goal);

  rclcpp_action::CancelResponse handle_cancel(
    const std::shared_ptr<GoalHandle> goal_handle);

  void handle_accepted(const std::shared_ptr<GoalHandle> goal_handle);

  void execute_goal(const std::shared_ptr<GoalHandle> goal_handle);
};

}  // namespace vehicle_agent_gateway
