import SwiftUI

struct CallSuggestionView: View {
    @Environment(\.nahbarTheme) var theme
    @Environment(\.dismiss) var dismiss
    @Bindable var person: Person
    let onConfirm: () -> Void

    /// Zeigt PersonalityBadge wenn Profil vorhanden und letztes Treffen schon länger zurückliegt.
    private var showRecommendedBadge: Bool {
        guard let profile = PersonalityStore.shared.profile,
              profile.level(for: .agreeableness) == .high else { return false }
        guard let lastMeeting = person.lastMeetingDate else { return true }
        let days = Calendar.current.dateComponents([.day], from: lastMeeting, to: Date()).day ?? 0
        return days > 14
    }

    var body: some View {
        VStack(spacing: 0) {
            RoundedRectangle(cornerRadius: 2)
                .fill(Color.secondary.opacity(0.3))
                .frame(width: 36, height: 4)
                .padding(.top, 12)

            VStack(spacing: 24) {

                // Person
                VStack(spacing: 10) {
                    PersonAvatar(person: person, size: 72)

                    VStack(spacing: 4) {
                        Text("Wie geht es \(person.firstName)?")
                            .font(.system(size: 22, weight: .light, design: theme.displayDesign))
                            .foregroundStyle(theme.contentPrimary)
                        TagBadge(text: person.tag.rawValue)
                    }

                    if showRecommendedBadge {
                        RecommendedBadge(variant: .small)
                    }
                }

                // Gesprächseinstieg
                if let context = callContext {
                    VStack(alignment: .leading, spacing: 6) {
                        Text("Gesprächseinstieg")
                            .font(.system(size: 11, weight: .semibold))
                            .foregroundStyle(theme.contentTertiary)
                            .textCase(.uppercase)
                            .tracking(0.5)

                        Text(context)
                            .font(.system(size: 15, design: theme.displayDesign))
                            .foregroundStyle(theme.contentPrimary)
                            .fixedSize(horizontal: false, vertical: true)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding(14)
                    .background(theme.surfaceCard)
                    .clipShape(RoundedRectangle(cornerRadius: theme.radiusCard))
                }

                // Offenes Vorhaben (ersetzt nextStep)
                if let intention = person.openIntentions.first {
                    HStack(spacing: 8) {
                        Image(systemName: "arrow.right.circle")
                            .font(.system(size: 13))
                        Text(intention.text)
                            .font(.system(size: 14))
                    }
                    .foregroundStyle(theme.accent.opacity(0.85))
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding(.top, -8)
                }

                // Buttons
                VStack(spacing: 10) {
                    Button {
                        onConfirm()
                        dismiss()
                    } label: {
                        Text("Los geht's")
                            .font(.system(size: 16, weight: .medium))
                            .foregroundStyle(.white)
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 14)
                            .background(theme.accent)
                            .clipShape(RoundedRectangle(cornerRadius: theme.radiusTag))
                    }

                    Button { dismiss() } label: {
                        Text("Nicht jetzt")
                            .font(.system(size: 15))
                            .foregroundStyle(theme.contentSecondary)
                    }
                }
            }
            .padding(.horizontal, 24)
            .padding(.top, 20)
            .padding(.bottom, 32)
        }
        .background(theme.backgroundPrimary)
        .presentationDetents([.medium])
        .presentationDragIndicator(.hidden)
    }

    // Letzten Moment-Text oder Interessen als Einstieg
    private var callContext: String? {
        if let last = person.sortedMoments.first {
            return last.text
        }
        if let interests = person.interests, !interests.isEmpty {
            return interests
        }
        return nil
    }
}
