import SwiftUI

struct ProfileHeaderCardView: View {
    struct PresenceStatus {
        let text: String
        let isOnline: Bool
    }

    struct StatusTag: Identifiable {
        let id = UUID()
        let icon: String
        let text: String
        let background: Color
        let tint: Color
    }

    private let avatar: AnyView
    private let displayName: String
    private let presenceStatus: PresenceStatus?
    private let statusTags: [StatusTag]
    private let isOfficial: Bool

    init<Avatar: View>(
        avatar: Avatar,
        displayName: String,
        presenceStatus: PresenceStatus?,
        statusTags: [StatusTag],
        isOfficial: Bool
    ) {
        self.avatar = AnyView(avatar)
        self.displayName = displayName
        self.presenceStatus = presenceStatus
        self.statusTags = statusTags
        self.isOfficial = isOfficial
    }

    var body: some View {
        VStack(spacing: 16) {
            avatar
                .overlay(alignment: .bottomTrailing) {
                    officialBadge
                }

            VStack(spacing: 6) {
                Text(displayName)
                    .font(.title2)
                    .fontWeight(.semibold)
                    .multilineTextAlignment(.center)
                    .frame(maxWidth: .infinity)

                if let presenceStatus {
                    HStack(spacing: 6) {
                        Circle()
                            .fill(presenceStatus.isOnline ? Color.green : Color.gray.opacity(0.4))
                            .frame(width: 8, height: 8)
                        Text(presenceStatus.text)
                            .font(.footnote)
                            .foregroundColor(.secondary)
                    }
                }
            }

            if !statusTags.isEmpty {
                LazyVGrid(columns: [GridItem(.adaptive(minimum: 120), spacing: 8)], spacing: 8) {
                    ForEach(statusTags) { tag in
                        HStack(spacing: 6) {
                            Image(systemName: tag.icon)
                                .font(.system(size: 12, weight: .semibold))
                            Text(tag.text)
                                .font(.caption)
                                .fontWeight(.medium)
                        }
                        .padding(.vertical, 6)
                        .padding(.horizontal, 10)
                        .foregroundColor(tag.tint)
                        .background(tag.background)
                        .clipShape(Capsule())
                    }
                }
            }
        }
        .padding(24)
        .frame(maxWidth: .infinity)
        .background(
            RoundedRectangle(cornerRadius: 32, style: .continuous)
                .fill(headerGradient)
        )
        .overlay(
            RoundedRectangle(cornerRadius: 32, style: .continuous)
                .stroke(Color.white.opacity(0.08), lineWidth: 1)
        )
        .shadow(color: Color.black.opacity(0.08), radius: 20, x: 0, y: 10)
    }

    @ViewBuilder
    private var officialBadge: some View {
        if isOfficial {
            Image(systemName: "checkmark.seal.fill")
                .font(.system(size: 18, weight: .semibold))
                .foregroundColor(.white)
                .padding(6)
                .background(Circle().fill(Color.accentColor))
                .offset(x: 6, y: 6)
        }
    }

    private var headerGradient: LinearGradient {
        let first = isOfficial ? Color.accentColor : Color.accentColor.opacity(0.6)
        let second = Color.accentColor.opacity(isOfficial ? 0.6 : 0.3)
        let third = Color(UIColor.secondarySystemBackground)
        return LinearGradient(colors: [first, second, third], startPoint: .topLeading, endPoint: .bottomTrailing)
    }
}
