// class CustomerModel {
//   late String customerName, phoneNumber, type, profilePicture, emailAddress, customerAddress, dueAmount, openingBalance, remainedBalance, gst;
//   bool? receiveWhatsappUpdates;
//
//   CustomerModel(
//       {required this.customerName,
//       required this.phoneNumber,
//       required this.type,
//       required this.profilePicture,
//       required this.emailAddress,
//       required this.customerAddress,
//       required this.dueAmount,
//       required this.openingBalance,
//       required this.remainedBalance,
//       required this.gst,
//       this.receiveWhatsappUpdates});
//
//   CustomerModel.fromJson(Map<dynamic, dynamic> json)
//       : customerName = json['customerName'] as String,
//         phoneNumber = json['phoneNumber'] as String,
//         type = json['type'] as String,
//         profilePicture = json['profilePicture'] as String,
//         emailAddress = json['emailAddress'] as String,
//         customerAddress = json['customerAddress'] as String,
//         dueAmount = json['due'] as String,
//         openingBalance = json['openingBalance'] as String,
//         remainedBalance = json['remainedBalance'] as String,
//         gst = json['gst'] ?? '',
//         receiveWhatsappUpdates = json['receiveWhatsappUpdates'] ?? false;
//
//   Map<dynamic, dynamic> toJson() => <dynamic, dynamic>{
//         'customerName': customerName,
//         'phoneNumber': phoneNumber,
//         'type': type,
//         'profilePicture': profilePicture,
//         'emailAddress': emailAddress,
//         'customerAddress': customerAddress,
//         'due': dueAmount,
//         'openingBalance': openingBalance,
//         'remainedBalance': remainedBalance,
//         'gst': gst,
//         'receiveWhatsappUpdates': receiveWhatsappUpdates,
//       };
// }

class CustomerModel {
  String customerName;
  String phoneNumber;
  String type;
  String profilePicture;
  String emailAddress;
  String customerAddress;
  double dueAmount;
  double openingBalance;
  double remainedBalance;
  String gst;
  bool? receiveWhatsappUpdates;

  CustomerModel({
    required this.customerName,
    required this.phoneNumber,
    required this.type,
    required this.profilePicture,
    required this.emailAddress,
    required this.customerAddress,
    required this.dueAmount,
    required this.openingBalance,
    required this.remainedBalance,
    required this.gst,
    this.receiveWhatsappUpdates,
  });

  CustomerModel.fromJson(Map<dynamic, dynamic> json)
      : customerName = json['customerName'] ?? 'Test',
        phoneNumber = json['phoneNumber'] ?? '',
        type = json['type'] ?? '',
        profilePicture = json['profilePicture'] ?? '',
        emailAddress = json['emailAddress'] ?? '',
        customerAddress = json['customerAddress'] ?? '',
        dueAmount = _parseToDouble(json['due']),
        openingBalance = _parseToDouble(json['openingBalance']),
        remainedBalance = _parseToDouble(json['remainedBalance']),
        receiveWhatsappUpdates = json['receiveWhatsappUpdates'] ?? false,
        gst = json['gst'] ?? '';

  Map<dynamic, dynamic> toJson() => <dynamic, dynamic>{
        'customerName': customerName,
        'phoneNumber': phoneNumber,
        'type': type,
        'profilePicture': profilePicture,
        'emailAddress': emailAddress,
        'customerAddress': customerAddress,
        'due': dueAmount,
        'openingBalance': openingBalance,
        'remainedBalance': remainedBalance,
        'gst': gst,
        'receiveWhatsappUpdates': receiveWhatsappUpdates,
      };

  /// Helper for safe number parsing
  static double _parseToDouble(dynamic value) {
    if (value == null) return 0.0;
    if (value is num) return value.toDouble();
    if (value is String) return double.tryParse(value.trim()) ?? 0.0;
    return 0.0;
  }
}
