class UserInfo {
  final int id;
  final String? name;
  final String? username;

  UserInfo({
    required this.id,
    this.name,
    this.username,
  });

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    // Handle wrapped response from API (e.g., {"success": {...}})
    final data = json.containsKey('success') ? json['success'] : json;

    return UserInfo(
      id: data['id'] as int,
      name: data['name'] as String?,
      username: data['username'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'username': username,
    };
  }

  String get displayName {
    if (username != null && username!.isNotEmpty) {
      if (name != null && name!.isNotEmpty) {
        return '$username ($name)';
      }
      return username!;
    }
    return name ?? 'User $id';
  }
}
