enum CrimeStatus { open, closed, underInvestigation, resolved, unresolved }

class CrimeIncidentModel {
  final String id;
  final String crimeId;
  final String crimeCategoryId;
  final String locationId;
  final String description;
  final int victimCount;
  final CrimeStatus? status;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final DateTime timestamp;

  CrimeIncidentModel({
    required this.id,
    required this.crimeId,
    required this.crimeCategoryId,
    required this.locationId,
    required this.description,
    required this.victimCount,
    this.status = CrimeStatus.open,
    this.createdAt,
    this.updatedAt,
    required this.timestamp,
  });

  // Create a CrimeIncidentModel instance from a JSON object
  factory CrimeIncidentModel.fromJson(Map<String, dynamic> json) {
    return CrimeIncidentModel(
      id: json['id'] as String,
      crimeId: json['crime_id'] as String,
      crimeCategoryId: json['crime_category_id'] as String,
      locationId: json['location_id'] as String,
      description: json['description'] as String,
      victimCount: json['victim_count'] as int,
      status: _parseStatus(json['status']?.toString()),
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      timestamp: DateTime.parse(json['timestamp']),
    );
  }

  // Convert a CrimeIncidentModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'crime_id': crimeId,
      'crime_category_id': crimeCategoryId,
      'location_id': locationId,
      'description': description,
      'victim_count': victimCount,
      'status': status?.toString().split('.').last ?? 'open',
      'timestamp': timestamp.toIso8601String(),
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the CrimeIncidentModel with updated fields
  CrimeIncidentModel copyWith({
    String? id,
    String? crimeId,
    String? crimeCategoryId,
    String? locationId,
    String? description,
    int? victimCount,
    CrimeStatus? status,
    DateTime? createdAt,
    DateTime? updatedAt,
    DateTime? timestamp,
  }) {
    return CrimeIncidentModel(
      id: id ?? this.id,
      crimeId: crimeId ?? this.crimeId,
      crimeCategoryId: crimeCategoryId ?? this.crimeCategoryId,
      locationId: locationId ?? this.locationId,
      description: description ?? this.description,
      victimCount: victimCount ?? this.victimCount,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      timestamp: timestamp ?? this.timestamp,
    );
  }

  static CrimeStatus? _parseStatus(String? status) {
    if (status == null) return CrimeStatus.open;

    switch (status) {
      case 'open':
        return CrimeStatus.open;
      case 'closed':
        return CrimeStatus.closed;
      case 'under_investigation':
        return CrimeStatus.underInvestigation;
      case 'resolved':
        return CrimeStatus.resolved;
      case 'unresolved':
        return CrimeStatus.unresolved;
      default:
        return CrimeStatus.open;
    }
  }

  @override
  String toString() {
    return 'CrimeIncidentModel(id: $id, crimeId: $crimeId, status: ${status.toString()})';
  }
}
