import 'package:sigap/src/features/personalization/data/models/models/roles_model.dart';

class OfficerModel {
  final String? id;
  final String? unitId;
  final String? roleId;
  final String? patrolUnitId;
  final dynamic nrp; // Changed to dynamic to handle both string and int
  final String? name;
  final String? rank;
  final String? position;
  final String? phone;
  final String? email;
  final String? avatar;
  final String? placeOfBirth;
  final DateTime? dateOfBirth;
  final DateTime? validUntil;
  final String? qrCode;
  final DateTime? createdAt;
  final DateTime? updatedAt;
  final RoleModel? role;
  // New fields from the JSON response
  final String? bannedReason;
  final DateTime? bannedUntil;
  final bool isBanned;
  final int panicStrike;
  final int spoofingAttempts;

  OfficerModel({
    this.id,
    this.unitId,
    this.roleId,
    this.patrolUnitId,
    this.nrp,
    this.name,
    this.rank,
    this.position,
    this.phone,
    this.email,
    this.avatar,
    this.placeOfBirth,
    this.dateOfBirth,
    this.validUntil,
    this.qrCode,
    this.createdAt,
    this.updatedAt,
    this.role,
    this.bannedReason,
    this.bannedUntil,
    this.isBanned = false,
    this.panicStrike = 0,
    this.spoofingAttempts = 0,
  });

  // Create an OfficerModel instance from a JSON object
  factory OfficerModel.fromJson(Map<String, dynamic> json) {
    return OfficerModel(
      id: json['id'],
      unitId: json['unit_id'],
      roleId: json['role_id'],
      patrolUnitId: json['patrol_unit_id'],
      nrp: json['nrp'], // Accept as dynamic
      name: json['name'],
      rank: json['rank'],
      position: json['position'],
      phone: json['phone'],
      email: json['email'],
      avatar: json['avatar'],
      placeOfBirth: json['place_of_birth'] ?? json['birth_place'],
      dateOfBirth:
          json['date_of_birth'] != null
              ? DateTime.parse(json['date_of_birth'] as String)
              : null,
      validUntil:
          json['valid_until'] != null
              ? DateTime.parse(json['valid_until'] as String)
              : null,
      qrCode: json['qr_code'],
      createdAt:
          json['created_at'] != null
              ? DateTime.parse(json['created_at'] as String)
              : null,
      updatedAt:
          json['updated_at'] != null
              ? DateTime.parse(json['updated_at'] as String)
              : null,
      // New fields
      bannedReason: json['banned_reason'],
      bannedUntil:
          json['banned_until'] != null
              ? DateTime.parse(json['banned_until'] as String)
              : null,
      isBanned: json['is_banned'] ?? false,
      panicStrike: json['panic_strike'] ?? 0,
      spoofingAttempts: json['spoofing_attempts'] ?? 0,
    );
  }

  // Convert an OfficerModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'unit_id': unitId,
      'role_id': roleId,
      'patrol_unit_id': patrolUnitId,
      'nrp': nrp,
      'name': name,
      'rank': rank,
      'position': position,
      'phone': phone,
      'email': email,
      'avatar': avatar,
      'place_of_birth': placeOfBirth,
      'date_of_birth': dateOfBirth?.toIso8601String(),
      'valid_until': validUntil?.toIso8601String(),
      'qr_code': qrCode,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
      'banned_reason': bannedReason,
      'banned_until': bannedUntil?.toIso8601String(),
      'is_banned': isBanned,
      'panic_strike': panicStrike,
      'spoofing_attempts': spoofingAttempts,
    };
  }

  // Create to json non null fields
  Map<String, dynamic> toJsonNonNull() {
    final json = <String, dynamic>{};

    if (id != null) json['id'] = id;
    if (unitId != null) json['unit_id'] = unitId;
    if (roleId != null) json['role_id'] = roleId;
    if (patrolUnitId != null) json['patrol_unit_id'] = patrolUnitId;
    if (nrp != null) json['nrp'] = nrp;
    if (name != null) json['name'] = name;
    if (rank != null) json['rank'] = rank;
    if (position != null) json['position'] = position;
    if (phone != null) json['phone'] = phone;
    if (email != null) json['email'] = email;
    if (avatar != null) json['avatar'] = avatar;
    if (placeOfBirth != null) json['place_of_birth'] = placeOfBirth;
    if (dateOfBirth != null)
      json['date_of_birth'] = dateOfBirth!.toIso8601String();
    if (validUntil != null) json['valid_until'] = validUntil!.toIso8601String();
    if (qrCode != null) json['qr_code'] = qrCode;
    if (createdAt != null) json['created_at'] = createdAt!.toIso8601String();
    if (updatedAt != null) json['updated_at'] = updatedAt!.toIso8601String();
    // New fields
    if (bannedReason != null) json['banned_reason'] = bannedReason;
    if (bannedUntil != null)
      json['banned_until'] = bannedUntil!.toIso8601String();
    json['is_banned'] = isBanned;
    json['panic_strike'] = panicStrike;
    json['spoofing_attempts'] = spoofingAttempts;

    return json;
  }

  // Create a copy of the OfficerModel with updated fields
  OfficerModel copyWith({
    String? id,
    String? unitId,
    String? roleId,
    String? patrolUnitId,
    dynamic nrp, // Changed to dynamic
    String? name,
    String? rank,
    String? position,
    String? phone,
    String? email,
    String? avatar,
    String? placeOfBirth,
    DateTime? dateOfBirth,
    DateTime? validUntil,
    String? qrCode,
    DateTime? createdAt,
    DateTime? updatedAt,
    RoleModel? role,
    String? bannedReason,
    DateTime? bannedUntil,
    bool? isBanned,
    int? panicStrike,
    int? spoofingAttempts,
  }) {
    return OfficerModel(
      id: id ?? this.id,
      unitId: unitId ?? this.unitId,
      roleId: roleId ?? this.roleId,
      patrolUnitId: patrolUnitId ?? this.patrolUnitId,
      nrp: nrp ?? this.nrp,
      name: name ?? this.name,
      rank: rank ?? this.rank,
      position: position ?? this.position,
      phone: phone ?? this.phone,
      email: email ?? this.email,
      avatar: avatar ?? this.avatar,
      placeOfBirth: placeOfBirth ?? this.placeOfBirth,
      dateOfBirth: dateOfBirth ?? this.dateOfBirth,
      validUntil: validUntil ?? this.validUntil,
      qrCode: qrCode ?? this.qrCode,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      role: role ?? this.role,
      bannedReason: bannedReason ?? this.bannedReason,
      bannedUntil: bannedUntil ?? this.bannedUntil,
      isBanned: isBanned ?? this.isBanned,
      panicStrike: panicStrike ?? this.panicStrike,
      spoofingAttempts: spoofingAttempts ?? this.spoofingAttempts,
    );
  }

  // Create an empty OfficerModel
  factory OfficerModel.empty() {
    return OfficerModel(
      id: '',
      unitId: '',
      roleId: '',
      patrolUnitId: null,
      nrp: null,
      name: '',
      rank: null,
      position: null,
      phone: null,
      email: null,
      avatar: null,
      placeOfBirth: null,
      dateOfBirth: null,
      validUntil: null,
      qrCode: null,
      createdAt: DateTime.now(),
      updatedAt: DateTime.now(),
      role: null,
      bannedReason: null,
      bannedUntil: null,
      isBanned: false,
      panicStrike: 0,
      spoofingAttempts: 0,
    );
  }

  // Create an OfficerModel from a User metadata
  factory OfficerModel.fromUserMetadata(
    String userId,
    Map<String, dynamic> metadata,
  ) {
    final officerData = metadata['officer_data'] ?? {};

    return OfficerModel(
      id: userId,
      unitId: officerData['unit_id'] ?? '',
      roleId: '', // This would need to be fetched or defined elsewhere
      nrp: officerData['nrp'] ?? '',
      name: officerData['name'] ?? '',
      rank: officerData['rank'],
      position: officerData['position'],
      phone: officerData['phone'],
      email: metadata['email'],
      avatar: officerData['avatar'],
      placeOfBirth: officerData['birth_place'],
      dateOfBirth:
          officerData['date_of_birth'] != null
              ? DateTime.parse(officerData['date_of_birth'])
              : null,
      validUntil:
          officerData['valid_until'] != null
              ? DateTime.parse(officerData['valid_until'])
              : null,
      qrCode: officerData['qr_code'],
    );
  }

  @override
  String toString() {
    return 'OfficerModel(id: $id, name: $name, nrp: $nrp)';
  }
}
