import SwiftUI

// MARK: - SearchResultsSection

struct SearchResultsSection: View {
    let isSearching: Bool
    let searchResults: [SearchUser]
    let currentPublicKey: String
    var onSelectUser: (SearchUser) -> Void

    var body: some View {
        if isSearching {
            loadingState
        } else if searchResults.isEmpty {
            noResultsState
        } else {
            VStack(spacing: 0) {
                ForEach(searchResults, id: \.publicKey) { user in
                    searchResultRow(user)
                }
            }
        }
    }
}

// MARK: - States

private extension SearchResultsSection {
    var loadingState: some View {
        VStack(spacing: 12) {
            Spacer().frame(height: 40)
            ProgressView()
                .tint(RosettaColors.Adaptive.textSecondary)
            Text("Searching...")
                .font(.system(size: 15))
                .foregroundStyle(RosettaColors.Adaptive.textSecondary)
        }
        .frame(maxWidth: .infinity)
    }

    var noResultsState: some View {
        VStack(spacing: 12) {
            Spacer().frame(height: 40)
            Image(systemName: "person.slash")
                .font(.system(size: 40))
                .foregroundStyle(RosettaColors.Adaptive.textSecondary.opacity(0.5))
            Text("No users found")
                .font(.system(size: 17, weight: .medium))
                .foregroundStyle(RosettaColors.Adaptive.textSecondary)
            Text("Try a different username or public key")
                .font(.system(size: 15))
                .foregroundStyle(RosettaColors.Adaptive.textTertiary)
        }
        .frame(maxWidth: .infinity)
    }
}

// MARK: - Result Row

private extension SearchResultsSection {
    func searchResultRow(_ user: SearchUser) -> some View {
        let isSelf = user.publicKey == currentPublicKey
        let initials = isSelf ? "S" : RosettaColors.initials(name: user.title, publicKey: user.publicKey)
        let colorIdx = RosettaColors.avatarColorIndex(for: user.title, publicKey: user.publicKey)

        return Button {
            onSelectUser(user)
        } label: {
            HStack(spacing: 12) {
                AvatarView(
                    initials: initials,
                    colorIndex: colorIdx,
                    size: 42,
                    isOnline: user.online == 0,
                    isSavedMessages: isSelf
                )

                VStack(alignment: .leading, spacing: 1) {
                    HStack(spacing: 4) {
                        Text(isSelf ? "Saved Messages" : (user.title.isEmpty ? String(user.publicKey.prefix(16)) + "..." : user.title))
                            .font(.system(size: 17, weight: .medium))
                            .foregroundStyle(RosettaColors.Adaptive.text)
                            .lineLimit(1)

                        if user.verified > 0 || isRosettaOfficial(user) {
                            VerifiedBadge(
                                verified: user.verified > 0 ? user.verified : 1,
                                size: 12
                            )
                        }
                    }

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

                Spacer()

                if user.online == 0 {
                    Circle()
                        .fill(RosettaColors.online)
                        .frame(width: 8, height: 8)
                }
            }
            .padding(.horizontal, 16)
            .padding(.vertical, 5)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
    }
}

// MARK: - Preview

#Preview {
    SearchResultsSection(
        isSearching: false,
        searchResults: [],
        currentPublicKey: "",
        onSelectUser: { _ in }
    )
}
