import 'package:sigap/src/features/daily-ops/data/models/models/units_model.dart';
import 'package:sigap/src/features/map/data/models/models/cities_model.dart';
import 'package:sigap/src/features/map/data/models/models/demographics_model.dart';
import 'package:sigap/src/features/map/data/models/models/geographics_model.dart';
import 'package:sigap/src/features/map/data/models/models/locations_model.dart';
import 'package:sigap/src/features/panic-button/data/models/models/crimes_model.dart';

class DistrictModel {
  final String id;
  final String cityId;
  final String name;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<CrimeModel>? crimes;
  final List<DemographicModel>? demographics;
  final CityModel? city;
  final List<GeographicModel>? geographics;
  final List<LocationModel>? locations;
  final UnitModel? unit;

  DistrictModel({
    required this.id,
    required this.cityId,
    required this.name,
    this.createdAt,
    this.updatedAt,
    this.crimes,
    this.demographics,
    this.city,
    this.geographics,
    this.locations,
    this.unit,
  });

  // Create a DistrictModel instance from a JSON object
  factory DistrictModel.fromJson(Map<String, dynamic> json) {
    return DistrictModel(
      id: json['id'] as String,
      cityId: json['city_id'] as String,
      name: json['name'] as String,
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      crimes:
          json['crimes'] != null
              ? (json['crimes'] as List)
                  .map((e) => CrimeModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
      demographics:
          json['demographics'] != null
              ? (json['demographics'] as List)
                  .map(
                    (e) => DemographicModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
      city:
          json['cities'] != null
              ? CityModel.fromJson(json['cities'] as Map<String, dynamic>)
              : null,
      geographics:
          json['geographics'] != null
              ? (json['geographics'] as List)
                  .map(
                    (e) => GeographicModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
      locations:
          json['locations'] != null
              ? (json['locations'] as List)
                  .map((e) => LocationModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
      unit:
          json['units'] != null
              ? UnitModel.fromJson(json['units'] as Map<String, dynamic>)
              : null,
    );
  }

  // Convert a DistrictModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'city_id': cityId,
      'name': name,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the DistrictModel with updated fields
  DistrictModel copyWith({
    String? id,
    String? cityId,
    String? name,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<CrimeModel>? crimes,
    List<DemographicModel>? demographics,
    CityModel? city,
    List<GeographicModel>? geographics,
    List<LocationModel>? locations,
    UnitModel? unit,
  }) {
    return DistrictModel(
      id: id ?? this.id,
      cityId: cityId ?? this.cityId,
      name: name ?? this.name,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      crimes: crimes ?? this.crimes,
      demographics: demographics ?? this.demographics,
      city: city ?? this.city,
      geographics: geographics ?? this.geographics,
      locations: locations ?? this.locations,
      unit: unit ?? this.unit,
    );
  }

  @override
  String toString() {
    return 'DistrictModel(id: $id, name: $name, cityId: $cityId)';
  }
}
