#pragma once
// control_handler.hpp
// 运控处理器虚基类。
// 子类覆盖各方法后即可接入真实运控驱动；默认实现返回 stub 成功响应。

#include <chrono>
#include "data_types.hpp"
#include "external_pose_store.hpp"

namespace vaw
{

class ControlHandler
{
public:
    virtual ~ControlHandler() = default;

    // 注入外部定位位姿缓存，用于运控就绪检查和后续轨迹跟踪。
    void set_external_pose_store(const ExternalPoseStore* store)
    {
        external_pose_store_ = store;
    }

    // 查询运控就绪状态
    virtual ControlReadinessResponse get_readiness(const nlohmann::json& req)
    {
        (void)req;
        ControlReadinessResponse rsp;
        rsp.success                   = true;
        rsp.error_code                = ErrorCode::OK;
        rsp.message                   = "运控就绪（stub）";
        rsp.agent_ready               = true;
        rsp.trajectory_executor_ready = true;
        rsp.vehicle_feedback_ready    = true;
        rsp.control_output_ready      = true;
        rsp.estop_released            = true;
        rsp.vehicle_safe_to_move      = true;
        if (external_pose_store_ != nullptr) {
            const auto pose_status = external_pose_store_->status(500.0);
            rsp.vehicle_feedback_ready = pose_status.ready;
            rsp.external_pose_feedback_ready = pose_status.ready;
            rsp.external_pose_source_name = pose_status.source_name;
            rsp.external_pose_age_ms = pose_status.age_ms;
            rsp.external_pose_quality_score = pose_status.quality_score;
            rsp.external_pose_transport = pose_status.transport;
        }
        rsp.checked_timestamp_us      = std::chrono::duration_cast<std::chrono::microseconds>(
            std::chrono::system_clock::now().time_since_epoch()).count();
        return rsp;
    }

    // 执行控制评估任务
    // req: 已解析的 ControllerEvaluationRequest
    virtual ControlJobResult execute_evaluation(const ControllerEvaluationRequest& req)
    {
        (void)req;
        ControlJobResult result;
        result.success              = true;
        result.error_code           = ErrorCode::OK;
        result.message              = "控制评估完成（stub）";
        result.job_id               = req.test_case_id;
        result.data_quality_passed  = true;
        result.suitable_for_commit  = true;
        result.validation_summary.auto_acceptance_passed = true;
        return result;
    }

private:
    const ExternalPoseStore* external_pose_store_{nullptr};
};

}  // namespace vaw
