#ifndef ACOUSTIC_ANALYZER_CORE_THREAT_TRACKER_H_
#define ACOUSTIC_ANALYZER_CORE_THREAT_TRACKER_H_

#include <string>
#include <vector>

#include "acoustic_analyzer/core/types.h"

namespace acoustic {

/**
 * @brief 威胁跟踪器
 *
 * 对连续帧的检测结果进行去重、ID 分配、时间间隔过滤，
 * 保证同一威胁不会在短时间内重复上报。
 */
class ThreatTracker {
   public:
    /**
     * @brief 构造威胁跟踪器
     * @param min_event_interval 同一威胁最小上报间隔 (秒)
     * @param azimuth_gate_deg   数据关联方位角门限 (度)
     * @param max_missing_frames 最大允许连续缺失帧数
     */
    explicit ThreatTracker(float min_event_interval = 0.3f,
                           float azimuth_gate_deg = 15.0f,
                           int max_missing_frames = 5);

    ~ThreatTracker() = default;

    // 禁止拷贝，允许移动
    ThreatTracker(const ThreatTracker&) = delete;
    ThreatTracker& operator=(const ThreatTracker&) = delete;
    ThreatTracker(ThreatTracker&&) = default;
    ThreatTracker& operator=(ThreatTracker&&) = default;

    /**
     * @brief 更新跟踪器并返回应上报的威胁列表
     * @param detected_threats 当前帧检测到的原始威胁
     * @return 经过过滤后需要对外发布的威胁列表
     */
    std::vector<AcousticThreat> Update(
        const std::vector<AcousticThreat>& detected_threats);

    /**
     * @brief 清空所有跟踪状态
     */
    void Reset();

    /**
     * @brief 当前正在跟踪的威胁数量
     */
    std::size_t NumTracked() const noexcept;

   private:
    struct TrackedThreat {
        std::string threat_id;
        Timestamp last_publish_time;
        float last_azimuth = 0.0f;
        float last_elevation = 0.0f;
        std::string sound_type;
        float confidence = 0.0f;
        float distance = -1.0f;
        float distance_confidence = 0.0f;
        int missing_frames = 0;
        bool ever_published = false;
    };

    float min_event_interval_;
    float azimuth_gate_deg_;
    int max_missing_frames_;
    std::vector<TrackedThreat> tracks_;
    uint64_t id_counter_ = 0;

    std::string GenerateId();
};

}  // namespace acoustic

#endif  // ACOUSTIC_ANALYZER_CORE_THREAT_TRACKER_H_
