#pragma once

#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 "calibration_common_interfaces/msg/job_state.hpp"
#include "calibration_workshop_orchestration_interfaces/msg/stage_plan.hpp"
#include "calibration_workshop_orchestration_interfaces/msg/stage_result_summary.hpp"
#include "calibration_workshop_orchestration_interfaces/msg/workshop_session.hpp"

namespace workshop_orchestrator_v2
{

// 底盘 gateway 薄 client。
// 职责：
//   1) 调 /chassis/get_readiness 确认底盘可执行；
//   2) 向 /chassis/execute_motion_primitive 发送动作原语 goal；
//   3) 把 ChassisJobResult 翻译成 StageResultSummary 返回给主控。
//
// 底盘专项实现细节保留在车端代理侧，此处只负责调用和结果转换。
class ChassisGatewayClient
{
public:
  using ReadinessSrv =
    calibration_chassis_interfaces::srv::GetChassisReadiness;
  using ExecuteTask =
    calibration_chassis_interfaces::action::ExecuteMotionPrimitive;
  using GoalHandleExecuteTask = rclcpp_action::ClientGoalHandle<ExecuteTask>;

  explicit ChassisGatewayClient(rclcpp::Node * node);

  // 对主控暴露的唯一入口：执行底盘标定阶段（动作原语序列）。
  bool execute_chassis_stage(
    const calibration_workshop_orchestration_interfaces::msg::WorkshopSession & session,
    const calibration_workshop_orchestration_interfaces::msg::StagePlan & stage,
    calibration_workshop_orchestration_interfaces::msg::StageResultSummary & result,
    std::string & failure_reason);

private:
  // 检查底盘 agent 是否就绪。
  bool check_ready(std::string & failure_reason);

  // 根据 session/stage 构造底盘动作 goal。
  bool build_goal(
    const calibration_workshop_orchestration_interfaces::msg::WorkshopSession & session,
    const calibration_workshop_orchestration_interfaces::msg::StagePlan & stage,
    ExecuteTask::Goal & goal,
    std::string & failure_reason) const;

  std::string make_request_id() const;

  rclcpp::Node * node_{nullptr};
  rclcpp::Client<ReadinessSrv>::SharedPtr readiness_client_;
  rclcpp_action::Client<ExecuteTask>::SharedPtr execute_task_client_;
};

}  // namespace workshop_orchestrator_v2
