import 'wayz_common_models.dart';

/// A single danger site item.
///
/// Reference: `doc/接口文档/05危险水域API接口文档.md` (2.3 返回参数)
class WzDangerSite {
  final String id;
  final WzGeoJson geometry;
  final String geoPoint;
  final String type;
  final WzAddress address;
  final double distanceMeters;

  WzDangerSite({
    required this.id,
    required this.geometry,
    required this.geoPoint,
    required this.type,
    required this.address,
    required this.distanceMeters,
  });

  factory WzDangerSite.fromJson(Map<String, dynamic> json) {
    return WzDangerSite(
      id: WzJson.toStringOrNull(json['id']) ?? '',
      geometry: json['geometry'] is Map
          ? WzGeoJson.fromJson((json['geometry'] as Map).cast<String, dynamic>())
          : WzGeoJson(type: ''),
      geoPoint: WzJson.toStringOrNull(json['geoPoint']) ?? '',
      type: WzJson.toStringOrNull(json['type']) ?? '',
      address: json['address'] is Map
          ? WzAddress.fromJson((json['address'] as Map).cast<String, dynamic>())
          : WzAddress(name: '', context: const []),
      distanceMeters: WzJson.toDouble(json['distanceMeters']) ?? 0,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'geometry': geometry.toJson(),
        'geoPoint': geoPoint,
        'type': type,
        'address': address.toJson(),
        'distanceMeters': distanceMeters,
      };
}

/// Helper typedef: danger sites endpoint returns an array.
typedef WzDangerSitesResponse = List<WzDangerSite>;

