import 'package:sigap/src/features/panic-button/data/models/models/evidences_model.dart';

class IncidentLogModel {
  final String id;
  final String userId;
  final String locationId;
  final String categoryId;
  final String? description;
  final String? source;
  final DateTime time;
  final bool? verified;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final List<EvidenceModel>? evidence;

  IncidentLogModel({
    required this.id,
    required this.userId,
    required this.locationId,
    required this.categoryId,
    this.description,
    this.source = 'manual',
    required this.time,
    this.verified = false,
    this.createdAt,
    this.updatedAt,
    this.evidence,
  });

  // Create an IncidentLogModel instance from a JSON object
  factory IncidentLogModel.fromJson(Map<String, dynamic> json) {
    return IncidentLogModel(
      id: json['id'] as String,
      userId: json['user_id'] as String,
      locationId: json['location_id'] as String,
      categoryId: json['category_id'] as String,
      description: json['description'] as String?,
      source: json['source'] as String? ?? 'manual',
      time: DateTime.parse(json['time'] as String),
      verified: json['verified'] as bool? ?? false,
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      evidence:
          json['evidence'] != null
              ? (json['evidence'] as List)
                  .map((e) => EvidenceModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
    );
  }

  // Convert an IncidentLogModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'location_id': locationId,
      'category_id': categoryId,
      'description': description,
      'source': source,
      'time': time.toIso8601String(),
      'verified': verified,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the IncidentLogModel with updated fields
  IncidentLogModel copyWith({
    String? id,
    String? userId,
    String? locationId,
    String? categoryId,
    String? description,
    String? source,
    DateTime? time,
    bool? verified,
    DateTime? createdAt,
    DateTime? updatedAt,
    List<EvidenceModel>? evidence,
  }) {
    return IncidentLogModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      locationId: locationId ?? this.locationId,
      categoryId: categoryId ?? this.categoryId,
      description: description ?? this.description,
      source: source ?? this.source,
      time: time ?? this.time,
      verified: verified ?? this.verified,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      evidence: evidence ?? this.evidence,
    );
  }

  @override
  String toString() {
    return 'IncidentLogModel(id: $id, locationId: $locationId, time: $time)';
  }
}
