import SwiftUI

// MARK: - Person Avatar

struct PersonAvatar: View {
    @Environment(\.nahbarTheme) var theme
    let person: Person
    let size: CGFloat

    var body: some View {
        Group {
            // currentPhotoData bevorzugt photo?.imageData (V3), fällt auf
            // das Legacy-Feld photoData zurück bis der Repair-Pass gelaufen ist.
            if let data = person.currentPhotoData, let uiImage = UIImage(data: data) {
                Image(uiImage: uiImage)
                    .resizable()
                    .scaledToFill()
            } else {
                Text(person.initials)
                    .font(.system(size: size * 0.38, weight: .medium, design: theme.displayDesign))
                    .foregroundStyle(theme.accent)
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(theme.accent.opacity(0.12))
            }
        }
        .frame(width: size, height: size)
        .clipShape(Circle())
    }
}

// MARK: - Tag Badge

struct TagBadge: View {
    @Environment(\.nahbarTheme) var theme
    let text: String

    var body: some View {
        Text(text)
            .font(.system(size: 11, weight: .medium))
            .foregroundStyle(theme.contentTertiary)
            .padding(.horizontal, 8)
            .padding(.vertical, 3)
            .background(theme.backgroundSecondary)
            .clipShape(Capsule())
    }
}

// MARK: - Max Badge

struct MaxBadge: View {
    @Environment(\.nahbarTheme) var theme

    var body: some View {
        Text("MAX")
            .font(.system(size: 10, weight: .bold))
            .foregroundStyle(theme.accent)
            .padding(.horizontal, 7)
            .padding(.vertical, 3)
            .background(theme.accent.opacity(0.10))
            .clipShape(Capsule())
    }
}

// MARK: - Section Header

struct SectionHeader: View {
    @Environment(\.nahbarTheme) var theme
    let title: LocalizedStringKey
    let icon: String

    var body: some View {
        HStack(spacing: 6) {
            Image(systemName: icon)
                .font(.system(size: 11, weight: .medium))
                .foregroundStyle(theme.contentTertiary)
            Text(title)
                .textCase(.uppercase)
                .font(.system(size: 11, weight: .semibold))
                .tracking(0.8)
                .foregroundStyle(theme.contentTertiary)
        }
    }
}

// MARK: - Divider Row

struct RowDivider: View {
    @Environment(\.nahbarTheme) var theme

    var body: some View {
        Rectangle()
            .fill(theme.borderSubtle)
            .frame(height: 0.5)
            .padding(.leading, 16)
            .allowsHitTesting(false)
    }
}

// MARK: - Themed Navigation Bar Modifier

private struct ThemedNavBar: ViewModifier {
    @Environment(\.nahbarTheme) var theme

    func body(content: Content) -> some View {
        content
            .toolbarBackground(theme.backgroundPrimary.opacity(0.92), for: .navigationBar)
            .toolbarBackground(.visible, for: .navigationBar)
            .toolbarColorScheme(theme.id.isDark ? .dark : .light, for: .navigationBar)
    }
}

extension View {
    func themedNavBar() -> some View {
        modifier(ThemedNavBar())
    }
}
