import SwiftUI

// MARK: - GroupMemberRow

/// Telegram-parity group member row. All colors adaptive for light/dark.
/// Avatar 40pt, name 17pt regular, status 14pt, role badge right-aligned.
struct GroupMemberRow: View {
    let member: GroupMember

    /// Telegram accent blue (#3e88f7) — same on both themes.
    private let accentBlue = Color(hex: 0x3e88f7)
    /// Telegram admin badge green (#49a355).
    private let adminBadge = Color(hex: 0x49a355)

    var body: some View {
        HStack(spacing: 9) {
            AvatarView(
                initials: RosettaColors.initials(name: member.title, publicKey: member.id),
                colorIndex: RosettaColors.avatarColorIndex(for: member.title, publicKey: member.id),
                size: 40,
                isOnline: member.isOnline,
                isSavedMessages: false,
                image: AvatarRepository.shared.loadAvatar(publicKey: member.id),
                onlineBorderColor: telegramSectionFill
            )

            VStack(alignment: .leading, spacing: 2) {
                HStack(spacing: 4) {
                    Text(member.title)
                        .font(.system(size: 17))
                        .foregroundStyle(RosettaColors.Adaptive.text)
                        .lineLimit(1)

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

                if member.isOnline {
                    Text("online")
                        .font(.system(size: 14))
                        .foregroundStyle(accentBlue)
                        .lineLimit(1)
                } else if !member.username.isEmpty {
                    Text("@\(member.username)")
                        .font(.system(size: 14))
                        .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                        .lineLimit(1)
                } else {
                    Text("last seen recently")
                        .font(.system(size: 14))
                        .foregroundStyle(RosettaColors.Adaptive.textSecondary)
                        .lineLimit(1)
                }
            }

            Spacer(minLength: 4)

            if member.isAdmin {
                VerifiedBadge(verified: 3, size: 20)
            }
        }
        .padding(.vertical, 10)
        .contentShape(Rectangle())
    }
}
