/// MQTT 摄像头告警消息数据模型
/// 对应 topic: camera/{cabinSn}/to 中 type="abnormal_behavior" 的消息
///
/// 与 Android 项目 qisuanfa_0621 MqttManager.processControlCommand() 保持一致：
/// - 只提取 behaviorCode 和 timestamp 两个字段用于标题映射
/// - 其余字段保留解析，方便后续扩展
class CameraAlarmBO {
  /// 事件唯一标识（UUID 去横线），用于去重
  final String eventId;

  /// 仓 SN
  final String deviceId;

  /// 设备类型，固定 "camera"
  final String deviceType;

  /// 毫秒时间戳
  final int timestamp;

  /// 消息类型，如 "abnormal_behavior"
  final String type;

  /// 告警 ID
  final int alarmId;

  /// 告警类别
  final String alarmType;

  /// 行为编码
  final int? behaviorCode;

  /// 行为描述
  final String? behaviorDesc;

  /// 宠物名称
  final String? petName;

  const CameraAlarmBO({
    required this.eventId,
    required this.deviceId,
    required this.deviceType,
    required this.timestamp,
    required this.type,
    required this.alarmId,
    required this.alarmType,
    this.behaviorCode,
    this.behaviorDesc,
    this.petName,
  });

  factory CameraAlarmBO.fromJson(Map<String, dynamic> json) {
    return CameraAlarmBO(
      eventId: json['eventId'] as String? ?? '',
      deviceId: json['deviceId'] as String? ?? '',
      deviceType: json['deviceType'] as String? ?? '',
      timestamp: json['timestamp'] as int? ?? 0,
      type: json['type'] as String? ?? '',
      alarmId: json['alarmId'] as int? ?? 0,
      alarmType: json['alarmType'] as String? ?? '',
      behaviorCode: json['behaviorCode'] as int?,
      behaviorDesc: json['behaviorDesc'] as String?,
      petName: json['petName'] as String?,
    );
  }

  /// 与 Android 行为编码映射完全一致：
  ///   2 → 异常液体
  ///   3 → 检测到排泄物
  ///   其他 → 未知异常
  String get titleByBehaviorCode {
    if (behaviorCode == 2) return '异常液体';
    if (behaviorCode == 3) return '检测到排泄物';
    return '未知异常';
  }
}
