class UnitStatisticModel {
  final String id;
  final int crimeTotal;
  final int crimeCleared;
  final double? percentage;
  final int? pending;
  final int month;
  final int year;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String codeUnit;

  UnitStatisticModel({
    required this.id,
    required this.crimeTotal,
    required this.crimeCleared,
    this.percentage,
    this.pending,
    required this.month,
    required this.year,
    this.createdAt,
    this.updatedAt,
    required this.codeUnit,
  });

  // Create a UnitStatisticModel instance from a JSON object
  factory UnitStatisticModel.fromJson(Map<String, dynamic> json) {
    return UnitStatisticModel(
      id: json['id'] as String,
      crimeTotal: json['crime_total'] as int,
      crimeCleared: json['crime_cleared'] as int,
      percentage: json['percentage']?.toDouble(),
      pending: json['pending'] as int?,
      month: json['month'] as int,
      year: json['year'] as int,
      createdAt: json['created_at'] != null
          ? DateTime.parse(json['created_at'] as String)
          : null,
      updatedAt: json['updated_at'] != null
          ? DateTime.parse(json['updated_at'] as String)
          : null,
      codeUnit: json['code_unit'] as String,
    );
  }

  // Convert a UnitStatisticModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'crime_total': crimeTotal,
      'crime_cleared': crimeCleared,
      'percentage': percentage,
      'pending': pending,
      'month': month,
      'year': year,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'code_unit': codeUnit,
    };
  }

  // Create a copy of the UnitStatisticModel with updated fields
  UnitStatisticModel copyWith({
    String? id,
    int? crimeTotal,
    int? crimeCleared,
    double? percentage,
    int? pending,
    int? month,
    int? year,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? codeUnit,
  }) {
    return UnitStatisticModel(
      id: id ?? this.id,
      crimeTotal: crimeTotal ?? this.crimeTotal,
      crimeCleared: crimeCleared ?? this.crimeCleared,
      percentage: percentage ?? this.percentage,
      pending: pending ?? this.pending,
      month: month ?? this.month,
      year: year ?? this.year,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      codeUnit: codeUnit ?? this.codeUnit,
    );
  }
  
  @override
  String toString() {
    return 'UnitStatisticModel(id: $id, month: $month, year: $year, crimeTotal: $crimeTotal, crimeCleared: $crimeCleared)';
  }
}
