import 'package:sigap/src/features/daily-ops/data/models/models/patrol_units_model.dart';
import 'package:sigap/src/features/daily-ops/data/models/models/unit_statistics_model.dart';
import 'package:sigap/src/features/daily-ops/data/models/models/officers_model.dart';

enum UnitType { polda, polsek, polres, other }

class UnitModel {
  final String codeUnit;
  final String? districtId;
  final String name;
  final String? description;
  final UnitType type;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final String? address;
  final double? landArea;
  final double latitude;
  final double longitude;
  final String cityId;
  final String? phone;
  final List<OfficerModel>? officers;
  final List<PatrolUnitModel>? patrolUnits;
  final List<UnitStatisticModel>? statistics;

  UnitModel({
    required this.codeUnit,
    this.districtId,
    required this.name,
    this.description,
    required this.type,
    this.createdAt,
    this.updatedAt,
    this.address,
    this.landArea,
    required this.latitude,
    required this.longitude,
    required this.cityId,
    this.phone,
    this.officers,
    this.patrolUnits,
    this.statistics,
  });

  // Create a UnitModel instance from a JSON object
  factory UnitModel.fromJson(Map<String, dynamic> json) {
    return UnitModel(
      codeUnit: json['code_unit'] as String,
      districtId: json['district_id'] as String?,
      name: json['name'] as String,
      description: json['description'] as String?,
      type: _parseUnitType(json['type'] as String),
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'])
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'])
              : null,
      address: json['address'] as String?,
      landArea: json['land_area']?.toDouble(),
      latitude: json['latitude'].toDouble(),
      longitude: json['longitude'].toDouble(),
      cityId: json['city_id'] as String,
      phone: json['phone'] as String?,
      officers:
          json['officers'] != null
              ? (json['officers'] as List)
                  .map((e) => OfficerModel.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,
      statistics:
          json['unit_statistics'] != null
              ? (json['unit_statistics'] as List)
                  .map(
                    (e) =>
                        UnitStatisticModel.fromJson(e as Map<String, dynamic>),
                  )
                  .toList()
              : null,
    );
  }

  // Convert a UnitModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'code_unit': codeUnit,
      'district_id': districtId,
      'name': name,
      'description': description,
      'type': type.name,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'address': address,
      'land_area': landArea,
      'latitude': latitude,
      'longitude': longitude,
      'city_id': cityId,
      'phone': phone,
      if (officers != null)
        'officers': officers!.map((e) => e.toJson()).toList(),
      if (patrolUnits != null)
        'patrol_units': patrolUnits!.map((e) => e.toJson()).toList(),
      if (statistics != null)
        'unit_statistics': statistics!.map((e) => e.toJson()).toList(),
    };
  }

  // Create a copy of the UnitModel with updated fields
  UnitModel copyWith({
    String? codeUnit,
    String? districtId,
    String? name,
    String? description,
    UnitType? type,
    DateTime? createdAt,
    DateTime? updatedAt,
    String? address,
    double? landArea,
    double? latitude,
    double? longitude,
    String? cityId,
    String? phone,
    List<OfficerModel>? officers,
    List<PatrolUnitModel>? patrolUnits,
    List<UnitStatisticModel>? statistics,
  }) {
    return UnitModel(
      codeUnit: codeUnit ?? this.codeUnit,
      districtId: districtId ?? this.districtId,
      name: name ?? this.name,
      description: description ?? this.description,
      type: type ?? this.type,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      address: address ?? this.address,
      landArea: landArea ?? this.landArea,
      latitude: latitude ?? this.latitude,
      longitude: longitude ?? this.longitude,
      cityId: cityId ?? this.cityId,
      phone: phone ?? this.phone,
      officers: officers ?? this.officers,
      patrolUnits: patrolUnits ?? this.patrolUnits,
      statistics: statistics ?? this.statistics,
    );
  }

  static UnitType _parseUnitType(String type) {
    switch (type.toLowerCase()) {
      case 'polda':
        return UnitType.polda;
      case 'polsek':
        return UnitType.polsek;
      case 'polres':
        return UnitType.polres;
      default:
        return UnitType.other;
    }
  }

  @override
  String toString() {
    return 'UnitModel(codeUnit: $codeUnit, name: $name, type: ${type.name})';
  }
}
