class Beneficiary {
  final String accountNo;
  final String accountType;
  final String name;
  final DateTime? createdAt;
  final String ifscCode;
  final String? bankName;
  final String? branchName;
  final String? transactionLimit;
  final String? tpin;

  Beneficiary({
    required this.accountNo,
    required this.accountType,
    required this.name,
    this.createdAt,
    required this.ifscCode,
    this.bankName,
    this.branchName,
    this.transactionLimit,
    this.tpin,
  });

  factory Beneficiary.fromJson(Map<String, dynamic> json) {
    return Beneficiary(
      accountNo: json['account_no'] ?? json['accountNo'] ?? '',
      accountType: json['account_type'] ?? json['accountType'] ?? '',
      createdAt: json['createdAt'] == null
          ? null
          : DateTime.tryParse(json['createdAt']),
      name: json['name'] ?? '',
      ifscCode: json['ifsc_code'] ?? json['ifscCode'] ?? '',
      bankName: json['bank_name'] ?? json['bankName'] ?? '',
      branchName: json['branch_name'] ?? json['branchName'] ?? '',
      transactionLimit: json['transactionLimit'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> json = {
      'accountNo': accountNo,
      'accountType': accountType,
      'name': name,
      'ifscCode': ifscCode,
    };
    if (bankName != null && bankName!.isNotEmpty) {
      json['bankName'] = bankName;
    }
    if (branchName != null && branchName!.isNotEmpty) {
      json['branchName'] = branchName;
    }
    if (tpin != null && tpin!.isNotEmpty) {
      json['tpin'] = tpin;
    }
    return json;
  }

  static List<Beneficiary> listFromJson(List<dynamic> jsonList) {
    final beneficiaryList = jsonList
        .map((beneficiary) => Beneficiary.fromJson(beneficiary))
        .toList();
    return beneficiaryList;
  }

  @override
  String toString() {
    return 'Beneficiary(accountNo: $accountNo, accountType: $accountType, ifscCode: $ifscCode, name: $name)';
  }
}
