import 'package:sigap/src/features/daily-ops/data/models/models/officers_model.dart';
import 'package:sigap/src/features/daily-ops/data/models/models/units_model.dart';
import 'package:sigap/src/features/map/data/models/models/locations_model.dart';

class PatrolUnitModel {
  final String id;
  final String unitId;
  final String locationId;
  final String name;
  final String type;
  final String status;
  final double radius;
  final DateTime createdAt;
  final List<OfficerModel>? members;
  final LocationModel? location;
  final UnitModel? unit;
  final String? category; // Added category field (individual/group)
  final int? memberCount; // Added member count field

  PatrolUnitModel({
    required this.id,
    required this.unitId,
    required this.locationId,
    required this.name,
    required this.type,
    required this.status,
    required this.radius,
    required this.createdAt,
    this.members,
    this.location,
    this.unit,
    this.category,
    this.memberCount,
  });

  // Create a PatrolUnitModel instance from a JSON object
  factory PatrolUnitModel.fromJson(Map<String, dynamic> json) {
    return PatrolUnitModel(
      id: json['id'] as String,
      unitId: json['unit_id'] as String,
      locationId: json['location_id'] as String,
      name: json['name'] as String,
      type: json['type'] as String,
      status: json['status'] as String,
      radius: json['radius'].toDouble(),
      createdAt: DateTime.parse(json['created_at'] as String),
      members:
          json['members'] != null
              ? (json['members'] as List)
                  .map((e) => OfficerModel.fromJson(e as Map<String, dynamic>))
                  .toList()
              : null,
      location:
          json['location'] != null
              ? LocationModel.fromJson(json['location'] as Map<String, dynamic>)
              : null,
      unit:
          json['unit'] != null
              ? UnitModel.fromJson(json['unit'] as Map<String, dynamic>)
              : null,
      category: json['category'] as String?, // Parse category from JSON
      memberCount:
          json['member_count'] != null
              ? int.parse(json['member_count'].toString())
              : null, // Parse member_count from JSON
    );
  }

  // Convert a PatrolUnitModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'unit_id': unitId,
      'location_id': locationId,
      'name': name,
      'type': type,
      'status': status,
      'radius': radius,
      'created_at': createdAt.toIso8601String(),
      if (members != null) 'members': members!.map((e) => e.toJson()).toList(),
      if (category != null) 'category': category,
      if (memberCount != null) 'member_count': memberCount,
    };
  }

  // Create a copy of the PatrolUnitModel with updated fields
  PatrolUnitModel copyWith({
    String? id,
    String? unitId,
    String? locationId,
    String? name,
    String? type,
    String? status,
    double? radius,
    DateTime? createdAt,
    List<OfficerModel>? members,
    LocationModel? location,
    UnitModel? unit,
    String? category,
    int? memberCount,
  }) {
    return PatrolUnitModel(
      id: id ?? this.id,
      unitId: unitId ?? this.unitId,
      locationId: locationId ?? this.locationId,
      name: name ?? this.name,
      type: type ?? this.type,
      status: status ?? this.status,
      radius: radius ?? this.radius,
      createdAt: createdAt ?? this.createdAt,
      members: members ?? this.members,
      location: location ?? this.location,
      unit: unit ?? this.unit,
      category: category ?? this.category,
      memberCount: memberCount ?? this.memberCount,
    );
  }

  get coordinates {
    return {'latitude': location?.latitude, 'longitude': location?.longitude};
  }

  // Calculate member count if it's not provided but members list exists
  int get effectiveMemberCount {
    if (memberCount != null) return memberCount!;
    return members?.length ?? 0;
  }

  @override
  String toString() {
    return 'PatrolUnitModel(id: $id, name: $name, status: $status, category: $category, memberCount: $memberCount)';
  }
}
