class Device {
  final int id;
  final String? callsign;
  final String? name;
  final String? city;
  final String? state;
  final String? country;
  final String? surname;
  final String? linkname;
  final String? hardware;
  final String? firmware;
  final String? tx;
  final String? rx;
  final int? colorcode;
  final int? status;
  final int? lastKnownMaster;
  final double? lat;
  final double? lng;
  final String? website;
  final int? pep;
  final int? agl;
  final String? description;
  final String? createdAt;
  final String? updatedAt;
  final String? lastSeen;
  final List<String>? permissions;

  Device({
    required this.id,
    this.callsign,
    this.name,
    this.city,
    this.state,
    this.country,
    this.surname,
    this.linkname,
    this.hardware,
    this.firmware,
    this.tx,
    this.rx,
    this.colorcode,
    this.status,
    this.lastKnownMaster,
    this.lat,
    this.lng,
    this.website,
    this.pep,
    this.agl,
    this.description,
    this.createdAt,
    this.updatedAt,
    this.lastSeen,
    this.permissions,
  });

  factory Device.fromJson(Map<String, dynamic> json) {
    return Device(
      id: json['id'] as int,
      callsign: json['callsign'] as String?,
      name: json['name'] as String?,
      city: json['city'] as String?,
      state: json['state'] as String?,
      country: json['country'] as String?,
      surname: json['surname'] as String?,
      linkname: json['linkname'] as String?,
      hardware: json['hardware'] as String?,
      firmware: json['firmware'] as String?,
      tx: json['tx'] as String?,
      rx: json['rx'] as String?,
      colorcode: json['colorcode'] as int?,
      status: json['status'] as int?,
      lastKnownMaster: json['last_known_master'] as int?,
      lat: _toDouble(json['lat']),
      lng: _toDouble(json['lng']),
      website: json['website'] as String?,
      pep: json['pep'] as int?,
      agl: json['agl'] as int?,
      description: json['description'] as String?,
      createdAt: json['created_at'] as String?,
      updatedAt: json['updated_at'] as String?,
      lastSeen: json['last_seen'] as String?,
      permissions: (json['permissions'] as List<dynamic>?)
          ?.map((e) => e as String)
          .toList(),
    );
  }

  static double? _toDouble(dynamic value) {
    if (value == null) return null;
    if (value is double) return value;
    if (value is int) return value.toDouble();
    if (value is String) return double.tryParse(value);
    return null;
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'callsign': callsign,
      'name': name,
      'city': city,
      'state': state,
      'country': country,
      'surname': surname,
      'linkname': linkname,
      'hardware': hardware,
      'firmware': firmware,
      'tx': tx,
      'rx': rx,
      'colorcode': colorcode,
      'status': status,
      'last_known_master': lastKnownMaster,
      'lat': lat,
      'lng': lng,
      'website': website,
      'pep': pep,
      'agl': agl,
      'description': description,
      'created_at': createdAt,
      'updated_at': updatedAt,
      'last_seen': lastSeen,
      'permissions': permissions,
    };
  }

  String get displayLocation {
    final parts = <String>[];
    if (city != null && city!.isNotEmpty) parts.add(city!);
    if (state != null && state!.isNotEmpty) parts.add(state!);
    if (country != null && country!.isNotEmpty) parts.add(country!);
    return parts.isEmpty ? 'Unknown Location' : parts.join(', ');
  }

  String get displayName {
    return name ?? callsign ?? 'Unknown Device';
  }
}
