// class CustomerModel {
//   late String customerName, phoneNumber, type, profilePicture, emailAddress, customerAddress, dueAmount, openingBalance, remainedBalance, gst, note;
//   bool? receiveWhatsappUpdates;
//
//   CustomerModel(this.customerName, this.phoneNumber, this.type, this.profilePicture, this.emailAddress, this.customerAddress, this.dueAmount, this.openingBalance, this.remainedBalance, this.note, {required this.gst, this.receiveWhatsappUpdates});
//
//   CustomerModel.fromJson(Map<dynamic, dynamic> json)
//       : customerName = json['customerName'] ?? 'Test',
//         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,
//         note = json['note'] ?? 'N/A',
//         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, 'note': note, 'gst': gst, 'receiveWhatsappUpdates': receiveWhatsappUpdates};
// }

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

  CustomerModel(
    this.customerName,
    this.phoneNumber,
    this.type,
    this.profilePicture,
    this.emailAddress,
    this.customerAddress,
    this.dueAmount,
    this.openingBalance,
    this.remainedBalance,
    this.note, {
    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']),
        note = json['note'] ?? 'N/A',
        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,
        'note': note,
        '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;
  }
}
