class GroupMember {
  final String userId;
  final String firstName;
  final String lastName;
  final String pseudo; // Pseudo du membre (par défaut = prénom)
  final String role; // 'admin' ou 'member'
  final DateTime joinedAt;
  final String? profilePictureUrl; // URL de la photo de profil

  GroupMember({
    required this.userId,
    required this.firstName,
    String? lastName,
    String? pseudo,
    this.role = 'member',
    DateTime? joinedAt,
    this.profilePictureUrl,
  })  : lastName = lastName ?? '',
        pseudo = pseudo ?? firstName,
        joinedAt = joinedAt ?? DateTime.now();

  factory GroupMember.fromMap(Map<String, dynamic> map, String userId) {
    return GroupMember(
      userId: userId,
      firstName: map['firstName'] ?? '',
      lastName: map['lastName'] ?? '',
      pseudo: map['pseudo'] ?? map['firstName'] ?? '',
      role: map['role'] ?? 'member',
      joinedAt: DateTime.fromMillisecondsSinceEpoch(map['joinedAt'] ?? 0),
      profilePictureUrl: map['profilePictureUrl'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'firstName': firstName,
      'lastName': lastName,
      'pseudo': pseudo,
      'role': role,
      'joinedAt': joinedAt.millisecondsSinceEpoch,
      'profilePictureUrl': profilePictureUrl,
    };
  }

  GroupMember copyWith({
    String? userId,
    String? firstName,
    String? lastName,
    String? pseudo,
    String? role,
    DateTime? joinedAt,
    String? profilePictureUrl,
  }) {
    return GroupMember(
      userId: userId ?? this.userId,
      firstName: firstName ?? this.firstName,
      lastName: lastName ?? this.lastName,
      pseudo: pseudo ?? this.pseudo,
      role: role ?? this.role,
      joinedAt: joinedAt ?? this.joinedAt,
      profilePictureUrl: profilePictureUrl ?? this.profilePictureUrl,
    );
  }
}