// 天气数据模型
class Weather {
  final String location;
  final String country;
  final double temperature;
  final String condition;
  final String conditionIcon;
  final double feelsLike;
  final int humidity;
  final double windSpeed;
  final String windDirection;
  final double pressure;
  final double visibility;
  final int uvIndex;
  final DateTime lastUpdated;

  Weather({
    required this.location,
    required this.country,
    required this.temperature,
    required this.condition,
    required this.conditionIcon,
    required this.feelsLike,
    required this.humidity,
    required this.windSpeed,
    required this.windDirection,
    required this.pressure,
    required this.visibility,
    required this.uvIndex,
    required this.lastUpdated,
  });

  factory Weather.fromJson(Map<String, dynamic> json) {
    final location = json['location'];
    final current = json['current'];

    return Weather(
      location: location['name'] ?? '',
      country: location['country'] ?? '',
      temperature: (current['temp_c'] ?? 0.0).toDouble(),
      condition: current['condition']['text'] ?? '',
      conditionIcon: 'https:${current['condition']['icon'] ?? ''}',
      feelsLike: (current['feelslike_c'] ?? 0.0).toDouble(),
      humidity: (current['humidity'] ?? 0).toInt(),
      windSpeed: (current['wind_kph'] ?? 0.0).toDouble(),
      windDirection: current['wind_dir'] ?? '',
      pressure: (current['pressure_mb'] ?? 0.0).toDouble(),
      visibility: (current['vis_km'] ?? 0.0).toDouble(),
      uvIndex: (current['uv'] ?? 0).toInt(),
      lastUpdated: DateTime.parse(
        current['last_updated'] ?? DateTime.now().toIso8601String(),
      ),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'location': location,
      'country': country,
      'temperature': temperature,
      'condition': condition,
      'conditionIcon': conditionIcon,
      'feelsLike': feelsLike,
      'humidity': humidity,
      'windSpeed': windSpeed,
      'windDirection': windDirection,
      'pressure': pressure,
      'visibility': visibility,
      'uvIndex': uvIndex,
      'lastUpdated': lastUpdated.toIso8601String(),
    };
  }
}

// 天气预报数据模型
class WeatherForecast {
  final String date;
  final double maxTemp;
  final double minTemp;
  final String condition;
  final String conditionIcon;
  final int chanceOfRain;

  WeatherForecast({
    required this.date,
    required this.maxTemp,
    required this.minTemp,
    required this.condition,
    required this.conditionIcon,
    required this.chanceOfRain,
  });

  factory WeatherForecast.fromJson(Map<String, dynamic> json) {
    final day = json['day'];

    return WeatherForecast(
      date: json['date'] ?? '',
      maxTemp: (day['maxtemp_c'] ?? 0.0).toDouble(),
      minTemp: (day['mintemp_c'] ?? 0.0).toDouble(),
      condition: day['condition']['text'] ?? '',
      conditionIcon: 'https:${day['condition']['icon'] ?? ''}',
      chanceOfRain: (day['daily_chance_of_rain'] ?? 0).toInt(),
    );
  }
}
