class PatientModel {
  String? name;
  String? phoneNumber;
  String? gender;
  DateTime? dateOfBirth;
  String? profileImagePath;
  String? profileImageUrl;
  PatientAddress address;

  List<FamilyMember> familyMembers = [];

  PatientModel() : address = PatientAddress();

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'phoneNumber': phoneNumber,
      'gender': gender,
      'dateOfBirth': dateOfBirth?.toIso8601String(),
      'profileImagePath': profileImagePath,
      'profileImageUrl': profileImageUrl,
      'address': address.toJson(),
      'familyMembers': familyMembers.map((member) => member.toJson()).toList(),
    };
  }

  PatientModel.fromJson(Map<String, dynamic> json)
      : address = PatientAddress() {
    name = json['name'];
    phoneNumber = json['phoneNumber'];
    gender = json['gender'];
    dateOfBirth = json['dateOfBirth'] != null
        ? DateTime.parse(json['dateOfBirth'])
        : null;
    profileImagePath = json['profileImagePath'];
    profileImageUrl = json['profileImageUrl'];
    if (json['address'] != null) {
      address =
          PatientAddress.fromJson(json['address'] as Map<String, dynamic>);
    }
    if (json['familyMembers'] != null) {
      familyMembers = (json['familyMembers'] as List)
          .map((memberJson) => FamilyMember.fromJson(memberJson))
          .toList();
    }
  }

  void updateFrom(PatientModel other) {
    name = other.name;
    phoneNumber = other.phoneNumber;
    gender = other.gender;
    dateOfBirth = other.dateOfBirth;
    profileImagePath = other.profileImagePath;
    profileImageUrl = other.profileImageUrl;
    address = other.address;
    familyMembers = other.familyMembers;
  }
}

class FamilyMember {
  String? name;
  String? relation;
  String? gender;
  DateTime? dateOfBirth;

  FamilyMember({this.name, this.relation, this.gender, this.dateOfBirth});

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'relation': relation,
      'gender': gender,
      'dateOfBirth': dateOfBirth?.toIso8601String(),
    };
  }

  FamilyMember.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    relation = json['relation'];
    gender = json['gender'];
    dateOfBirth = json['dateOfBirth'] != null
        ? DateTime.parse(json['dateOfBirth'])
        : null;
  }
}

class PatientAddress {
  String? houseNo;
  String? line;
  String? town;
  String? pincode;
  String? country;
  String? state;
  String? city;

  PatientAddress(
      {this.houseNo,
      this.line,
      this.town,
      this.pincode,
      this.country,
      this.state,
      this.city});

  Map<String, dynamic> toJson() {
    return {
      'houseNo': houseNo,
      'line': line,
      'town': town,
      'pincode': pincode,
      'country': country,
      'state': state,
      'city': city,
    };
  }

  PatientAddress.fromJson(Map<String, dynamic> json) {
    houseNo = json['houseNo'];
    line = json['line'];
    town = json['town'];
    pincode = json['pincode'];
    country = json['country'];
    state = json['state'];
    city = json['city'];
  }
}
