import 'package:equatable/equatable.dart';

class InspectionDeviceBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeIcon;
  final String deviceTypeName;
  final String runStatus;
  final String runStatusText;
  final String roomName;
  final String lastHeartbeat;
  final String lastHeartbeatDisplay;
  final String onlineStatus;
  final String powerStatus;
  final int roomId;
  final String deviceCategory;

  const InspectionDeviceBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceTypeIcon,
    required this.deviceTypeName,
    required this.runStatus,
    required this.runStatusText,
    required this.roomName,
    required this.lastHeartbeat,
    required this.lastHeartbeatDisplay,
    required this.onlineStatus,
    required this.powerStatus,
    required this.roomId,
    required this.deviceCategory,
  });

  factory InspectionDeviceBO.fromJson(Map<String, dynamic> json) {
    return InspectionDeviceBO(
      deviceId: int.tryParse(json['deviceId']?.toString() ?? '') ?? 0,
      deviceName: json['deviceName'] as String? ?? '',
      deviceTypeIcon: json['deviceTypeIcon'] as String? ?? '',
      deviceTypeName: json['deviceTypeName'] as String? ?? '',
      runStatus: json['runStatus'] as String? ?? '',
      runStatusText: json['runStatusText'] as String? ?? '',
      roomName: json['roomName'] as String? ?? '',
      lastHeartbeat: json['lastHeartbeat'] as String? ?? '',
      lastHeartbeatDisplay: json['lastHeartbeatDisplay'] as String? ?? '',
      onlineStatus: json['onlineStatus'] as String? ?? '',
      powerStatus: json['powerStatus'] as String? ?? '',
      roomId: int.tryParse(json['roomId']?.toString() ?? '') ?? 0,
      deviceCategory: json['deviceCategory'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        deviceTypeIcon,
        deviceTypeName,
        runStatus,
        runStatusText,
        roomName,
        lastHeartbeat,
        lastHeartbeatDisplay,
        onlineStatus,
        powerStatus,
        roomId,
        deviceCategory,
      ];
}

class InspectionDeviceListBO extends Equatable {
  final int total;
  final List<InspectionDeviceBO> data;

  const InspectionDeviceListBO({
    required this.total,
    required this.data,
  });

  factory InspectionDeviceListBO.fromJson(Map<String, dynamic> json) {
    final list = json['rows'] as List<dynamic>? ?? [];
    return InspectionDeviceListBO(
      total: int.tryParse(json['total']?.toString() ?? '') ?? 0,
      data: list
          .map((e) => InspectionDeviceBO.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  @override
  List<Object?> get props => [total, data];
}

// ==================== 设备拓扑图 - 设备详情 ====================

class DeviceDetailBO extends Equatable {
  final DeviceBasicInfoBO basicInfo;
  final LatestInspectionBO? latestInspection;
  final List<InspectionItemBO> inspectionItems;

  const DeviceDetailBO({
    required this.basicInfo,
    this.latestInspection,
    required this.inspectionItems,
  });

  factory DeviceDetailBO.fromJson(Map<String, dynamic> json) {
    final items = json['inspectionItems'] as List<dynamic>? ?? [];
    return DeviceDetailBO(
      basicInfo: DeviceBasicInfoBO.fromJson(
          json['basicInfo'] as Map<String, dynamic>? ?? {}),
      latestInspection: json['latestInspection'] != null
          ? LatestInspectionBO.fromJson(
              json['latestInspection'] as Map<String, dynamic>)
          : null,
      inspectionItems: items
          .map((e) =>
              InspectionItemBO.fromJson(e as Map<String, dynamic>))
          .toList(),
    );
  }

  @override
  List<Object?> get props => [basicInfo, latestInspection, inspectionItems];
}

class DeviceBasicInfoBO extends Equatable {
  final int deviceId;
  final String deviceName;
  final String deviceTypeName;
  final String onlineStatus;
  final String deviceModel;
  final String areaName;
  final String responsiblePerson;
  final String lastInspectTime;

  const DeviceBasicInfoBO({
    required this.deviceId,
    required this.deviceName,
    required this.deviceTypeName,
    required this.onlineStatus,
    required this.deviceModel,
    required this.areaName,
    required this.responsiblePerson,
    required this.lastInspectTime,
  });

  factory DeviceBasicInfoBO.fromJson(Map<String, dynamic> json) {
    return DeviceBasicInfoBO(
      deviceId: int.tryParse(json['deviceId']?.toString() ?? '') ?? 0,
      deviceName: json['deviceName'] as String? ?? '',
      deviceTypeName: json['deviceTypeName'] as String? ?? '',
      onlineStatus: json['onlineStatus'] as String? ?? '',
      deviceModel: json['deviceModel'] as String? ?? '',
      areaName: json['areaName'] as String? ?? '',
      responsiblePerson: json['responsiblePerson'] as String? ?? '',
      lastInspectTime: json['lastInspectTime'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        deviceId,
        deviceName,
        deviceTypeName,
        onlineStatus,
        deviceModel,
        areaName,
        responsiblePerson,
        lastInspectTime,
      ];
}

class LatestInspectionBO extends Equatable {
  final int inspectId;
  final String inspectorName;
  final String inspectTime;
  final String inspectStatus;
  final String remark;

  const LatestInspectionBO({
    required this.inspectId,
    required this.inspectorName,
    required this.inspectTime,
    required this.inspectStatus,
    required this.remark,
  });

  factory LatestInspectionBO.fromJson(Map<String, dynamic> json) {
    return LatestInspectionBO(
      inspectId: int.tryParse(json['inspectId']?.toString() ?? '') ?? 0,
      inspectorName: json['inspectorName'] as String? ?? '',
      inspectTime: json['inspectTime'] as String? ?? '',
      inspectStatus: json['inspectStatus'] as String? ?? '',
      remark: json['remark'] as String? ?? '',
    );
  }

  @override
  List<Object?> get props => [
        inspectId,
        inspectorName,
        inspectTime,
        inspectStatus,
        remark,
      ];
}

class InspectionItemBO extends Equatable {
  final String name;
  final String value;
  final String status;

  const InspectionItemBO({
    required this.name,
    required this.value,
    required this.status,
  });

  factory InspectionItemBO.fromJson(Map<String, dynamic> json) {
    return InspectionItemBO(
      name: json['name'] as String? ?? '',
      value: json['value'] as String? ?? '',
      status: json['status'] as String? ?? '',
    );
  }

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

// ==================== 设备拓扑图 - 开始巡检 ====================

class InspectionStartResultBO extends Equatable {
  final int inspectId;
  final String inspectStatus;
  final String inspectTime;
  final int totalItems;

  const InspectionStartResultBO({
    required this.inspectId,
    required this.inspectStatus,
    required this.inspectTime,
    required this.totalItems,
  });

  factory InspectionStartResultBO.fromJson(Map<String, dynamic> json) {
    return InspectionStartResultBO(
      inspectId: int.tryParse(json['inspectId']?.toString() ?? '') ?? 0,
      inspectStatus: json['inspectStatus'] as String? ?? '',
      inspectTime: json['inspectTime'] as String? ?? '',
      totalItems: int.tryParse(json['totalItems']?.toString() ?? '') ?? 0,
    );
  }

  @override
  List<Object?> get props => [inspectId, inspectStatus, inspectTime, totalItems];
}