class AnalyticsDevice {
  const AnalyticsDevice({
    required this.uuid,
    required this.name,
    this.createdAt,
    this.updatedAt,
    this.deviceTuyaUuid,
    this.isActive,
    this.productDevice,
    this.spaceUuid,
    this.latitude,
    this.longitude,
  });

  final String uuid;
  final String name;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String? deviceTuyaUuid;
  final bool? isActive;
  final ProductDevice? productDevice;
  final String? spaceUuid;
  final double? latitude;
  final double? longitude;

  factory AnalyticsDevice.fromJson(Map<String, dynamic> json) {
    return AnalyticsDevice(
      uuid: json['uuid'] as String? ?? '',
      name: json['name'] as String? ?? '',
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'] as String)
          : null,
      updatedAt: json['updatedAt'] != null
          ? DateTime.parse(json['updatedAt'] as String)
          : null,
      deviceTuyaUuid: json['deviceTuyaUuid'] as String?,
      isActive: json['isActive'] as bool?,
      productDevice: json['productDevice'] != null
          ? ProductDevice.fromJson(json['productDevice'] as Map<String, dynamic>)
          : null,
      spaceUuid: json['spaceUuid'] as String?,
      latitude: json['lat'] != null && json['lat'] != ''
          ? double.tryParse(json['lat']?.toString() ?? '0.0')
          : null,
      longitude: json['lon'] != null && json['lon'] != ''
          ? double.tryParse(json['lon']?.toString() ?? '0.0')
          : null,
    );
  }
}

class ProductDevice {
  const ProductDevice({
    this.uuid,
    this.createdAt,
    this.updatedAt,
    this.catName,
    this.prodId,
    this.name,
    this.prodType,
  });

  final String? uuid;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String? catName;
  final String? prodId;
  final String? name;
  final String? prodType;

  factory ProductDevice.fromJson(Map<String, dynamic> json) {
    return ProductDevice(
      uuid: json['uuid'] as String?,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'] as String)
          : null,
      updatedAt: json['updatedAt'] != null
          ? DateTime.parse(json['updatedAt'] as String)
          : null,
      catName: json['catName'] as String?,
      prodId: json['prodId'] as String?,
      name: json['name'] as String?,
      prodType: json['prodType'] as String?,
    );
  }
}
