import 'package:equatable/equatable.dart';

/// Model representing a KTA (Kartu Tanda Anggota) police officer ID card
class KtaModel extends Equatable {
  /// Full name of the officer
  final String name;

  /// NRP (Nomor Registrasi Polisi) - Police Registration Number
  final String nrp;

  /// Police station/unit where the officer is assigned
  final String policeUnit;

  /// Issue date of the ID card in format dd-mm-yyyy
  final String issueDate;

  /// Officer rank (e.g., IPDA, IPTU, AKP, etc.)
  final String? rank;

  /// Officer position/role
  final String? position;

  /// Card unique identification number
  final String cardNumber;

  /// URL to the officer's photo on the card
  final String? photoUrl;

  /// Additional details about the card or officer
  final Map<String, dynamic>? extraData;

  const KtaModel({
    required this.name,
    required this.nrp,
    required this.policeUnit,
    required this.issueDate,
    required this.cardNumber,
    this.rank = '',
    this.position = '',
    this.photoUrl,
    this.extraData,
  });

  /// Create a KTA model from a map/JSON
  factory KtaModel.fromJson(Map<String, dynamic> json) {
    return KtaModel(
      name: json['name'] ?? '',
      nrp: json['nrp'] ?? '',
      policeUnit: json['police_unit'] ?? '',
      issueDate: json['issue_date'] ?? '',
      cardNumber: json['card_number'] ?? '',
      rank: json['rank'] ?? '',
      position: json['position'] ?? '',
      photoUrl: json['photo_url'],
      extraData: json['extra_data'] as Map<String, dynamic>?,
    );
  }

  /// Convert the KTA model to a map/JSON
  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'nrp': nrp,
      'police_unit': policeUnit,
      'issue_date': issueDate,
      'card_number': cardNumber,
      'rank': rank,
      'position': position,
      'photo_url': photoUrl,
      'extra_data': extraData,
    };
  }

  /// Create an empty KTA model
  factory KtaModel.empty() {
    return const KtaModel(
      name: '',
      nrp: '',
      policeUnit: '',
      issueDate: '',
      cardNumber: '',
      rank: '',
      position: '',
    );
  }

  /// Format the NRP with proper spacing for display
  /// e.g., "1234567890" becomes "12345 67890"
  String get formattedNrp {
    if (nrp.length < 5) return nrp;
    return '${nrp.substring(0, 5)} ${nrp.substring(5)}';
  }

  /// Check if the KTA model contains valid and complete information
  bool get isValid {
    return name.isNotEmpty &&
        nrp.isNotEmpty &&
        policeUnit.isNotEmpty &&
        cardNumber.isNotEmpty;
  }

  /// Create a copy of this KTA model with modified fields
  KtaModel copyWith({
    String? name,
    String? nrp,
    String? policeUnit,
    String? issueDate,
    String? cardNumber,
    String? rank,
    String? position,
    String? photoUrl,
    Map<String, dynamic>? extraData,
  }) {
    return KtaModel(
      name: name ?? this.name,
      nrp: nrp ?? this.nrp,
      policeUnit: policeUnit ?? this.policeUnit,
      issueDate: issueDate ?? this.issueDate,
      cardNumber: cardNumber ?? this.cardNumber,
      rank: rank ?? this.rank,
      position: position ?? this.position,
      photoUrl: photoUrl ?? this.photoUrl,
      extraData: extraData ?? this.extraData,
    );
  }

  /// Parse the issue date into a DateTime object
  /// Expects format: DD-MM-YYYY
  DateTime? get parsedIssueDate {
    try {
      if (issueDate.isEmpty) return null;

      // Split the date string by '-'
      final parts = issueDate.split('-');
      if (parts.length != 3) return null;

      final day = int.tryParse(parts[0]);
      final month = int.tryParse(parts[1]);
      final year = int.tryParse(parts[2]);

      if (day == null || month == null || year == null) return null;

      return DateTime(year, month, day);
    } catch (e) {
      return null;
    }
  }

  @override
  List<Object?> get props => [
    name,
    nrp,
    policeUnit,
    issueDate,
    cardNumber,
    rank,
    position,
    photoUrl,
    extraData,
  ];

  @override
  String toString() {
    return 'KtaModel(name: $name, nrp: $nrp, rank: $rank, position: $position, policeUnit: $policeUnit, issueDate: $issueDate, cardNumber: $cardNumber)';
  }
}
