import SwiftUI

// MARK: - GroupMemberRow

/// Reusable row for displaying a group member with avatar, name, role badge.
struct GroupMemberRow: View {
    let member: GroupMember

    var body: some View {
        HStack(spacing: 12) {
            // Avatar
            AvatarView(
                initials: RosettaColors.initials(name: member.title, publicKey: member.id),
                colorIndex: RosettaColors.avatarColorIndex(for: member.title, publicKey: member.id),
                size: 44,
                isOnline: member.isOnline,
                isSavedMessages: false,
                image: AvatarRepository.shared.loadAvatar(publicKey: member.id)
            )

            // Name + username
            VStack(alignment: .leading, spacing: 2) {
                HStack(spacing: 4) {
                    Text(member.title)
                        .font(.system(size: 16, weight: .medium))
                        .foregroundStyle(RosettaColors.Adaptive.text)
                        .lineLimit(1)

                    if member.isAdmin {
                        Text("admin")
                            .font(.system(size: 11, weight: .semibold))
                            .foregroundStyle(.white)
                            .padding(.horizontal, 6)
                            .padding(.vertical, 2)
                            .background(
                                Capsule().fill(RosettaColors.figmaBlue)
                            )
                    }

                    if member.verified > 0 {
                        VerifiedBadge(verified: member.verified, size: 14)
                    }
                }

                if !member.username.isEmpty {
                    Text("@\(member.username)")
                        .font(.system(size: 13))
                        .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                        .lineLimit(1)
                }
            }

            Spacer()
        }
        .padding(.vertical, 6)
        .contentShape(Rectangle())
    }
}
