import 'wayz_common_models.dart';

/// 正地理编码
class WzGeocodeResponse {
  final String id;
  final String geoPoint;
  final String type;
  final String name;
  final String code;
  final WzAddress address;
  final double? relevance;
  final double? distance;
  final List<WzCategory>? categories;

  WzGeocodeResponse({
    required this.id,
    required this.geoPoint,
    required this.type,
    required this.name,
    required this.code,
    required this.address,
    this.relevance,
    this.distance,
    this.categories,
  });

  factory WzGeocodeResponse.fromJson(Map<String, dynamic> json) {
    return WzGeocodeResponse(
      id: (json['id'] ?? '') as String,
      geoPoint: (json['geoPoint'] ?? '') as String,
      type: (json['type'] ?? '') as String,
      name: (json['name'] ?? '') as String,
      code: (json['code'] ?? '') as String,
      address: json['address'] is Map
          ? WzAddress.fromJson((json['address'] as Map).cast<String, dynamic>())
          : WzAddress(name: '', context: const []),
      relevance: WzJson.toDouble(json['relevance']),
      distance: WzJson.toDouble(json['distance']),
      categories: WzJson.toList<WzCategory>(
        json['categories'],
        (e) => WzCategory.fromJson((e as Map).cast<String, dynamic>()),
      ),
    );
  }

  Map<String, dynamic> toJson() {
    final out = <String, dynamic>{
      'id': id,
      'geoPoint': geoPoint,
      'type': type,
      'name': name,
      'code': code,
      'address': address.toJson(),
    };
    if (relevance != null) out['relevance'] = relevance;
    if (distance != null) out['distance'] = distance;
    if (categories != null) {
      out['categories'] = categories!.map((e) => e.toJson()).toList();
    }
    return out;
  }
}

