import 'package:equatable/equatable.dart';

// ==================== 告警详情 ====================

class AlertDetailBO extends Equatable {
  final AlertBasicInfoBO alertBasicInfo;
  final TrendChartBO trendChart;
  final AlertDetailInfoBO alertDetailInfo;

  const AlertDetailBO({
    required this.alertBasicInfo,
    required this.trendChart,
    required this.alertDetailInfo,
  });

  factory AlertDetailBO.fromJson(Map<String, dynamic> json) {
    return AlertDetailBO(
      alertBasicInfo: AlertBasicInfoBO.fromJson(
        json['alertBasicInfo'] as Map<String, dynamic>? ?? {},
      ),
      trendChart: TrendChartBO.fromJson(
        json['trendChart'] as Map<String, dynamic>? ?? {},
      ),
      alertDetailInfo: AlertDetailInfoBO.fromJson(
        json['alertDetailInfo'] as Map<String, dynamic>? ?? {},
      ),
    );
  }

  @override
  List<Object?> get props => [alertBasicInfo, trendChart, alertDetailInfo];
}

// ==================== 告警基本信息 ====================

class AlertBasicInfoBO extends Equatable {
  final int alertId;
  final String alertTitle;
  final String alertLevel;
  final String alertLevelText;
  final String alertStatus;
  final String alertDeviceLocation;
  final int alertCount;
  final String alertTime;
  final String alertIcon;
  final String alertDeviceId;

  const AlertBasicInfoBO({
    required this.alertId,
    required this.alertTitle,
    required this.alertLevel,
    required this.alertLevelText,
    required this.alertStatus,
    required this.alertDeviceLocation,
    required this.alertCount,
    required this.alertTime,
    required this.alertIcon,
    required this.alertDeviceId,
  });

  factory AlertBasicInfoBO.fromJson(Map<String, dynamic> json) {
    return AlertBasicInfoBO(
      alertId: json['alertId'] as int? ?? 0,
      alertTitle: json['alertTitle'] as String? ?? '',
      alertLevel: json['alertLevel'] as String? ?? '',
      alertLevelText: json['alertLevelText'] as String? ?? '',
      alertStatus: json['alertStatus']?.toString() ?? '',
      alertDeviceLocation: json['alertDeviceLocation'] as String? ?? '',
      alertCount: json['alertCount'] as int? ?? 0,
      alertTime: json['alertTime'] as String? ?? '',
      alertIcon: json['alertIcon'] as String? ?? '',
      alertDeviceId: json['alertDeviceId']?.toString() ?? '',
    );
  }

  @override
  List<Object?> get props => [
        alertId,
        alertTitle,
        alertLevel,
        alertLevelText,
        alertStatus,
        alertDeviceLocation,
        alertCount,
        alertTime,
        alertIcon,
        alertDeviceId,
      ];
}

// ==================== 趋势图 ====================

class TrendChartBO extends Equatable {
  final String chartTitle;
  final String chartDate;
  final String unit;
  final List<DataPointBO> dataPoints;
  final int yaxisMax;
  final int yaxisMin;

  const TrendChartBO({
    required this.chartTitle,
    required this.chartDate,
    required this.unit,
    required this.dataPoints,
    required this.yaxisMax,
    required this.yaxisMin,
  });

  factory TrendChartBO.fromJson(Map<String, dynamic> json) {
    return TrendChartBO(
      chartTitle: json['chartTitle'] as String? ?? '',
      chartDate: json['chartDate'] as String? ?? '',
      unit: json['unit'] as String? ?? '',
      dataPoints: (json['dataPoints'] as List<dynamic>?)
              ?.map(
                  (e) => DataPointBO.fromJson(e as Map<String, dynamic>))
              .toList() ??
          [],
      yaxisMax: json['yaxisMax'] as int? ?? 100,
      yaxisMin: json['yaxisMin'] as int? ?? 0,
    );
  }

  @override
  List<Object?> get props =>
      [chartTitle, chartDate, unit, dataPoints, yaxisMax, yaxisMin];
}

class DataPointBO extends Equatable {
  final String time;
  final double value;
  final int recordId;

  const DataPointBO({
    required this.time,
    required this.value,
    required this.recordId,
  });

  factory DataPointBO.fromJson(Map<String, dynamic> json) {
    final rawValue = json['value'];
    final double value;
    if (rawValue is num) {
      value = rawValue.toDouble();
    } else if (rawValue is String) {
      value = double.tryParse(rawValue) ?? 0;
    } else {
      value = 0;
    }

    final rawRecordId = json['recordId'];
    final int recordId;
    if (rawRecordId is int) {
      recordId = rawRecordId;
    } else if (rawRecordId is String) {
      recordId = int.tryParse(rawRecordId) ?? 0;
    } else {
      recordId = 0;
    }

    return DataPointBO(
      time: json['time'] as String? ?? '',
      value: value,
      recordId: recordId,
    );
  }

  @override
  List<Object?> get props => [time, value, recordId];
}

// ==================== 告警详细信息 ====================

class AlertDetailInfoBO extends Equatable {
  final String thresholdSetting;
  final String duration;
  final int durationMinutes;
  final String peakRecord;
  final String linkageAction;
  final String alertValue;
  final String alertContent;

  const AlertDetailInfoBO({
    required this.thresholdSetting,
    required this.duration,
    required this.durationMinutes,
    required this.peakRecord,
    required this.linkageAction,
    required this.alertValue,
    required this.alertContent,
  });

  factory AlertDetailInfoBO.fromJson(Map<String, dynamic> json) {
    return AlertDetailInfoBO(
      thresholdSetting: json['thresholdSetting'] as String? ?? '',
      duration: json['duration'] as String? ?? '',
      durationMinutes: json['durationMinutes'] as int? ?? 0,
      peakRecord: json['peakRecord'] as String? ?? '',
      linkageAction: json['linkageAction'] as String? ?? '',
      alertValue: json['alertValue'] as String? ?? '',
      alertContent: json['alertContent'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        thresholdSetting,
        duration,
        durationMinutes,
        peakRecord,
        linkageAction,
        alertValue,
        alertContent,
      ];
}