#ifndef ACOUSTIC_ANALYZER_CORE_TYPES_H_
#define ACOUSTIC_ANALYZER_CORE_TYPES_H_

#include <array>
#include <chrono>
#include <cstdint>
#include <string>
#include <vector>

#ifdef ROS_TIME_INCLUDED
#include <ros/time.h>
#endif

namespace acoustic {

#ifdef ROS_TIME_INCLUDED
using Timestamp = ros::Time;
#else
using Timestamp = std::chrono::steady_clock::time_point;
#endif

/**
 * @brief 单个声源威胁检测结果
 */
struct AcousticThreat {
    Timestamp timestamp;
    std::string threat_id;          ///< 唯一威胁标识符
    std::string sound_type;         ///< 声音类别 (gunshot / artillery / explosion / ambient)
    float confidence = 0.0f;        ///< 分类置信度 [0, 1]
    float azimuth = 0.0f;           ///< 方位角 0~360°，0=正北，顺时针
    float elevation = 0.0f;         ///< 俯仰角 -90~90°
    float distance = -1.0f;         ///< 估计距离 (米)，-1 表示未知
    float distance_confidence = 0.0f; ///< 距离估计置信度 [0, 1]
};

/**
 * @brief 单帧声学分析输出
 */
struct AcousticFrame {
    Timestamp timestamp;
    std::vector<AcousticThreat> threats;
    bool is_clear = true;           ///< true = 当前帧无威胁
};

/**
 * @brief 麦克风阵列几何配置
 */
struct MicArrayConfig {
    uint32_t num_mics = 4;
    float spacing = 0.15f;          ///< 相邻麦克风间距 (米)
    std::vector<std::array<float, 3>> positions; ///< 自定义坐标 [x, y, z]
    std::string layout = "cross";   ///< cross / linear / circular / custom
};

/**
 * @brief 分类器配置
 */
struct ClassifierConfig {
    std::string model_path;         ///< ONNX 模型路径
    std::string label_map_path;     ///< 标签映射 JSON 路径
    float threshold = 0.7f;         ///< 检测阈值
    uint32_t smoothing_window = 3;  ///< 后处理平滑窗口
};

/**
 * @brief 距离估计器配置
 */
struct DistanceConfig {
    float ref_spl_gunshot = 150.0f;      ///< 枪声参考 SPL (dB)
    float ref_spl_artillery = 180.0f;    ///< 火炮参考 SPL (dB)
    float ref_spl_explosion = 170.0f;    ///< 爆炸参考 SPL (dB)
    float attenuation_alpha = 0.6f;      ///< 空气吸收衰减系数
    float spl_calibration_offset = 0.0f; ///< SPL 校准偏移
    float kalman_process_noise = 0.01f;  ///< Kalman 过程噪声
    float kalman_measurement_noise = 5.0f; ///< Kalman 测量噪声
};

/**
 * @brief 流水线顶层配置
 */
struct PipelineConfig {
    uint32_t sample_rate = 16000;
    float chunk_duration = 2.0f;    ///< 分析窗口长度 (秒)
    float hop_duration = 0.5f;      ///< 步进长度 (秒)
    uint32_t n_mels = 64;           ///< Mel 滤波器组数量
    float confidence_threshold = 0.7f; ///< 输出置信度阈值
    float min_event_interval = 0.3f;   ///< 同一威胁最小上报间隔 (秒)

    // 特征提取参数（原 hard-coded，现可配置）
    uint32_t n_fft = 2048;          ///< FFT 长度
    uint32_t hop_length = 512;      ///< 帧移样本数
    float f_min = 0.0f;             ///< 最低频率 (Hz)
    float f_max = 8000.0f;          ///< 最高频率 (Hz)
    float preemphasis = 0.97f;      ///< 预加重系数

    // 定位参数（原 hard-coded，现可配置）
    float max_tdoa = 0.00044f;      ///< 最大允许时延 (秒)
    int interpolation_factor = 4;   ///< GCC-PHAT 插值倍数

    MicArrayConfig mic_array;
    ClassifierConfig classifier;
    DistanceConfig distance;
};

}  // namespace acoustic

#endif  // ACOUSTIC_ANALYZER_CORE_TYPES_H_
