class CityModel {
  final String name;
  final String? region;
  final String? country;
  final String query; // 用于API查询的字符串
  final DateTime addedAt;
  final bool isDefault; // 是否为默认城市

  CityModel({
    required this.name,
    this.region,
    this.country,
    required this.query,
    DateTime? addedAt,
    this.isDefault = false,
  }) : addedAt = addedAt ?? DateTime.now();

  // 转换为JSON用于存储
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'region': region,
      'country': country,
      'query': query,
      'addedAt': addedAt.toIso8601String(),
      'isDefault': isDefault,
    };
  }

  // 从JSON创建
  factory CityModel.fromJson(Map<String, dynamic> json) {
    return CityModel(
      name: json['name'],
      region: json['region'],
      country: json['country'],
      query: json['query'],
      addedAt: DateTime.parse(json['addedAt']),
      isDefault: json['isDefault'] ?? false,
    );
  }

  // 复制方法
  CityModel copyWith({
    String? name,
    String? region,
    String? country,
    String? query,
    DateTime? addedAt,
    bool? isDefault,
  }) {
    return CityModel(
      name: name ?? this.name,
      region: region ?? this.region,
      country: country ?? this.country,
      query: query ?? this.query,
      addedAt: addedAt ?? this.addedAt,
      isDefault: isDefault ?? this.isDefault,
    );
  }

  // 显示名称
  String get displayName {
    if (region != null && country != null) {
      return '$name, $region, $country';
    } else if (region != null) {
      return '$name, $region';
    } else if (country != null) {
      return '$name, $country';
    }
    return name;
  }
}
