// 1. Models/users.dart - Version corrigée
class Users {
  int? id;
  String name;
  String lastName;
  String email;
  String password;
  String username;
  int roleId;
  String? roleName;
  int? pointDeVenteId;

  Users({
    this.id,
    required this.name,
    required this.lastName,
    required this.email,
    required this.password,
    required this.username,
    required this.roleId,
    this.roleName,
    this.pointDeVenteId,
  });

  // ✅ CORRIGÉ: Méthode toMap() qui correspond exactement aux colonnes de la DB
  Map<String, dynamic> toMap() {
    return {
      'id': id,  // ✅ Inclure l'ID pour les updates
      'name': name,
      'lastname': lastName, // ✅ Correspond à la colonne DB
      'email': email,
      'password': password,
      'username': username,
      'role_id': roleId,
      'point_de_vente_id': pointDeVenteId,
    };
  }

  // ✅ Méthode pour créer un map sans l'ID (pour les insertions)
  Map<String, dynamic> toMapForInsert() {
    final map = toMap();
    map.remove('id');
    return map;
  }

  factory Users.fromMap(Map<String, dynamic> map) {
    return Users(
      id: map['id'] as int?,
      name: map['name'] as String,
      lastName: map['lastname'] as String, // ✅ Correspond à la colonne DB
      email: map['email'] as String,
      password: map['password'] as String,
      username: map['username'] as String,
      roleId: map['role_id'] as int,
      roleName: map['role_name'] as String?, // Depuis les JOINs
      pointDeVenteId: map['point_de_vente_id'] as int?,
    );
  }

  String get role => roleName ?? '';
}