enum GroupRole { manager, member }

extension GroupRoleExtension on GroupRole {
  String get label {
    switch (this) {
      case GroupRole.manager:
        return 'مدیر';
      case GroupRole.member:
        return 'عضو';
    }
  }

  String get apiValue => name;
}

class GroupMemberModel {
  final String userId;
  final GroupRole role;
  /// Denormalized username for display — populated from local user cache
  final String? username;
  final bool isOnline;

  const GroupMemberModel({
    required this.userId,
    required this.role,
    this.username,
    this.isOnline = false,
  });

  factory GroupMemberModel.fromJson(Map<String, dynamic> json) {
    return GroupMemberModel(
      userId: json['user_id'].toString(),
      role: GroupRole.values.firstWhere(
        (r) => r.name == (json['role'] as String),
        orElse: () => GroupRole.member,
      ),
      username: json['username'] as String?,
      isOnline: (json['is_online'] as bool?) ?? false,
    );
  }

  Map<String, dynamic> toJson() => {
        'user_id': userId,
        'role': role.apiValue,
        if (username != null) 'username': username,
        'is_online': isOnline,
      };

  GroupMemberModel copyWith({
    String? userId,
    GroupRole? role,
    String? username,
    bool? isOnline,
  }) {
    return GroupMemberModel(
      userId: userId ?? this.userId,
      role: role ?? this.role,
      username: username ?? this.username,
      isOnline: isOnline ?? this.isOnline,
    );
  }
}
