class UserModel {
  final String username;
  final String email;
  final String role;
  final String token;
  final String? phoneNumber;

  UserModel({
    required this.username,
    required this.email,
    required this.role,
    required this.token,
    this.phoneNumber,
  });

  /// Defensive factory: uses defaults when keys are missing
  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      username: (json['username'] ?? json['email'] ?? '').toString(),
      email: (json['email'] ?? '').toString(),
      role: (json['role'] ?? 'user').toString(),
      token: (json['token'] ?? '').toString(),
      phoneNumber: json['phone_number'] != null ? json['phone_number'].toString() : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'username': username,
      'email': email,
      'role': role,
      'token': token,
      if (phoneNumber != null) 'phone_number': phoneNumber,
    };
  }
}
