import 'package:sigap/src/features/daily-ops/data/models/models/patrol_units_model.dart';
import 'package:sigap/src/features/panic-button/data/models/models/crime_incidents_model.dart';
import 'package:sigap/src/features/panic-button/data/models/models/events_model.dart';
import 'package:sigap/src/features/panic-button/data/models/models/incident_logs_model.dart';

class LocationModel {
  final String id;
  final String districtId;
  final String eventId;
  final String? address;
  final String? type;
  final double latitude;
  final double longitude;
  final double? landArea;
  final double? distanceToUnit;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  // Relations
  final EventModel event;
  final List<CrimeIncidentModel>? crimeIncidents;
  final List<IncidentLogModel>? incidentLogs;
  final List<PatrolUnitModel>? patrolUnits;

  LocationModel({
    required this.id,
    required this.districtId,
    required this.eventId,
    this.address,
    this.type,
    required this.latitude,
    required this.longitude,
    this.landArea,
    this.distanceToUnit,
    this.createdAt,
    this.updatedAt,
    required this.event,
    this.crimeIncidents,
    this.incidentLogs,
    this.patrolUnits,
  });

  // Create a LocationModel instance from a JSON object
  factory LocationModel.fromJson(Map<String, dynamic> json) {
    return LocationModel(
      id: json['id'] as String,
      districtId: json['district_id'] as String,
      eventId: json['event_id'] as String,
      address: json['address'] as String?,
      type: json['type'] as String?,
      latitude: json['latitude'].toDouble(),
      longitude: json['longitude'].toDouble(),
      landArea: json['land_area']?.toDouble(),
      distanceToUnit: json['distance_to_unit']?.toDouble(),
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      event: EventModel.fromJson(json['event'] as Map<String, dynamic>),
      crimeIncidents:
          json['crime_incidents'] != null
              ? (json['crime_incidents'] as List)
                  .map(
                    (e) =>
                        CrimeIncidentModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
      incidentLogs:
          json['incident_logs'] != null
              ? (json['incident_logs'] as List)
                  .map(
                    (e) => IncidentLogModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
      patrolUnits:
          json['patrol_units'] != null
              ? (json['patrol_units'] as List)
                  .map(
                    (e) => PatrolUnitModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
    );
  }

  // Convert a LocationModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'district_id': districtId,
      'event_id': eventId,
      'address': address,
      'type': type,
      'latitude': latitude,
      'longitude': longitude,
      'land_area': landArea,
      'distance_to_unit': distanceToUnit,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Create a copy of the LocationModel with updated fields
  LocationModel copyWith({
    String? id,
    String? districtId,
    String? eventId,
    String? address,
    String? type,
    double? latitude,
    double? longitude,
    double? landArea,
    double? distanceToUnit,
    DateTime? createdAt,
    DateTime? updatedAt,
    EventModel? event,
    List<CrimeIncidentModel>? crimeIncidents,
    List<IncidentLogModel>? incidentLogs,
    List<PatrolUnitModel>? patrolUnits,
  }) {
    return LocationModel(
      id: id ?? this.id,
      districtId: districtId ?? this.districtId,
      eventId: eventId ?? this.eventId,
      address: address ?? this.address,
      type: type ?? this.type,
      latitude: latitude ?? this.latitude,
      longitude: longitude ?? this.longitude,
      landArea: landArea ?? this.landArea,
      distanceToUnit: distanceToUnit ?? this.distanceToUnit,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      event: event ?? this.event,
      crimeIncidents: crimeIncidents ?? this.crimeIncidents,
      incidentLogs: incidentLogs ?? this.incidentLogs,
      patrolUnits: patrolUnits ?? this.patrolUnits,
    );
  }

  @override
  String toString() {
    return 'LocationModel(id: $id, latitude: $latitude, longitude: $longitude)';
  }
}
