/// Modèle pour un utilisateur admin (Keycloak) - aligné sur l'API /api/admin/users
library admin_user_model;

class AdminUserModel {
  final String id;
  final String? username;
  final String? email;
  final String? prenom;
  final String? nom;
  final bool? enabled;
  final List<String>? realmRoles;

  AdminUserModel({
    required this.id,
    this.username,
    this.email,
    this.prenom,
    this.nom,
    this.enabled,
    this.realmRoles,
  });

  String get displayName {
    if (prenom != null && nom != null) return '$prenom $nom';
    if (prenom != null) return prenom!;
    if (nom != null) return nom!;
    return username ?? email ?? id;
  }

  factory AdminUserModel.fromJson(Map<String, dynamic> json) {
    final roles = json['realmRoles'] as List<dynamic>?;
    return AdminUserModel(
      id: json['id'] as String? ?? '',
      username: json['username'] as String?,
      email: json['email'] as String?,
      prenom: json['prenom'] as String?,
      nom: json['nom'] as String?,
      enabled: json['enabled'] as bool?,
      realmRoles: roles?.map((e) => e is Map ? (e['name'] as String?) ?? e.toString() : e.toString()).toList(),
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'username': username,
        'email': email,
        'prenom': prenom,
        'nom': nom,
        'enabled': enabled,
        'realmRoles': realmRoles,
      };
}

class AdminRoleModel {
  final String id;
  final String name;
  final String? description;

  AdminRoleModel({required this.id, required this.name, this.description});

  factory AdminRoleModel.fromJson(Map<String, dynamic> json) => AdminRoleModel(
        id: json['id'] as String? ?? '',
        name: json['name'] as String? ?? '',
        description: json['description'] as String?,
      );
}
