class ProfileModel {
  final String id;
  final String userId;
  final String? nik;
  final String? avatar;
  final String? username;
  final String? firstName;
  final String? lastName;
  final String? bio;
  final Map<String, dynamic>? address;
  final String? placeOfBirth;
  final DateTime? birthDate;
  

  ProfileModel({
    required this.id,
    required this.userId,
    required this.nik,
    this.avatar,
    this.username,
    this.firstName,
    this.lastName,
    this.bio,
    this.address,
    this.placeOfBirth,
    this.birthDate,
  });

  // Create a ProfileModel instance from a JSON object
  factory ProfileModel.fromJson(Map<String, dynamic> json) {
    return ProfileModel(
      id: json['id'] as String,
      userId: json['user_id'] as String,
      nik: json['nik'] as String?,
      avatar: json['avatar'] as String?,
      username: json['username'] as String?,
      firstName: json['first_name'] as String?,
      lastName: json['last_name'] as String?,
      bio: json['bio'] as String?,
      address:
          json['address'] != null
              ? Map<String, dynamic>.from(json['address'] as Map)
              : null,
      placeOfBirth: json['birth_place'] as String?,
      birthDate:
          json['birth_date'] != null
              ? DateTime.parse(json['birth_date'] as String)
              : null,
    );
  }

  // Convert a ProfileModel instance to a JSON object
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'user_id': userId,
      'nik': nik,
      'avatar': avatar,
      'username': username,
      'first_name': firstName,
      'last_name': lastName,
      'bio': bio,
      'address': address,
      'birth_place': placeOfBirth,
      'birth_date': birthDate?.toIso8601String(),
    };
  }

  // Create a copy of the ProfileModel with updated fields
  ProfileModel copyWith({
    String? id,
    String? userId,
    String? nik,
    String? avatar,
    String? username,
    String? firstName,
    String? lastName,
    String? bio,
    Map<String, dynamic>? address,
    String? placeOfBirth,
    DateTime? birthDate,
  }) {
    return ProfileModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      nik: nik ?? this.nik,
      avatar: avatar ?? this.avatar,
      username: username ?? this.username,
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      bio: bio ?? this.bio,
      address: address ?? this.address,
      placeOfBirth: placeOfBirth ?? this.placeOfBirth,
      birthDate: birthDate ?? this.birthDate,
    );
  }

  // Get full name from first name and last name
  String? get fullName {
    if (firstName == null && lastName == null) return null;
    return [firstName, lastName].where((x) => x != null).join(' ');
  }

  // Get initials from first name and last name
  String? get initials {
    if (firstName == null && lastName == null) return null;
    return [
      firstName?.substring(0, 1),
      lastName?.substring(0, 1),
    ].where((x) => x != null).join('');
  }
  

  @override
  String toString() {
    return 'ProfileModel(id: $id, username: $username, fullName: $fullName)';
  }
}
