import 'package:sigap/src/features/map/data/models/models/districts_model.dart';

class DemographicModel {
  final String id;
  final String districtId;
  final int population;
  final int numberOfUnemployed;
  final double populationDensity;
  final int year;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final DistrictModel? district;

  DemographicModel({
    required this.id,
    required this.districtId,
    required this.population,
    required this.numberOfUnemployed,
    required this.populationDensity,
    required this.year,
    this.createdAt,
    this.updatedAt,
    this.district,
  });

  // Create a DemographicModel instance from a JSON object
  factory DemographicModel.fromJson(Map<String, dynamic> json) {
    return DemographicModel(
      id: json['id'] as String,
      districtId: json['district_id'] as String,
      population: json['population'] as int,
      numberOfUnemployed: json['number_of_unemployed'] as int,
      populationDensity: json['population_density'].toDouble(),
      year: json['year'] as int,
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      district:
          json['districts'] != null
              ? DistrictModel.fromJson(
                json['districts'] as Map<String, dynamic>,
              )
              : null,
    );
  }

  // Convert a DemographicModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'district_id': districtId,
      'population': population,
      'number_of_unemployed': numberOfUnemployed,
      'population_density': populationDensity,
      'year': year,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the DemographicModel with updated fields
  DemographicModel copyWith({
    String? id,
    String? districtId,
    int? population,
    int? numberOfUnemployed,
    double? populationDensity,
    int? year,
    DateTime? createdAt,
    DateTime? updatedAt,
    DistrictModel? district,
  }) {
    return DemographicModel(
      id: id ?? this.id,
      districtId: districtId ?? this.districtId,
      population: population ?? this.population,
      numberOfUnemployed: numberOfUnemployed ?? this.numberOfUnemployed,
      populationDensity: populationDensity ?? this.populationDensity,
      year: year ?? this.year,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      district: district ?? this.district,
    );
  }

  @override
  String toString() {
    return 'DemographicModel(id: $id, districtId: $districtId, year: $year)';
  }
}
