import SwiftUI

// MARK: - MeetingSummaryView
// Zeigt die Zusammenfassung eines bewerteten Treffen-Moments.
// Wird sowohl nach der Sofort-Bewertung als auch nach der Nachwirkung gezeigt.

struct MeetingSummaryView: View {
    let moment: Moment
    let onDismiss: () -> Void

    private var immediateCategories: [RatingCategory] {
        [.selbst, .beziehung, .gespraech]
    }

    var body: some View {
        ScrollView {
            VStack(spacing: 28) {
                // Header
                VStack(spacing: 8) {
                    Image(systemName: moment.isMeetingComplete ? "checkmark.circle.fill" : "clock.fill")
                        .font(.system(size: 48))
                        .foregroundStyle(moment.isMeetingComplete ? .green : .orange)

                    Text(moment.isMeetingComplete ? "Alles festgehalten" : "Gut gemacht!")
                        .font(.title2.bold())

                    Text(moment.meetingStatus == .awaitingAftermath
                         ? "Wir erinnern dich an die Nachwirkung."
                         : "Bewertung abgeschlossen.")
                        .font(.subheadline)
                        .foregroundStyle(.secondary)
                        .multilineTextAlignment(.center)
                }
                .padding(.top, 32)

                // Sofort-Werte
                if let immediateAvg = moment.immediateAverage {
                    summaryCard(title: "Sofort-Eindruck", average: immediateAvg,
                                categories: immediateCategories, isAftermath: false)
                }

                // Nachwirkungs-Wert (falls vorhanden)
                if let aftermathAvg = moment.aftermathAverage {
                    summaryCard(title: "Nachwirkung", average: aftermathAvg,
                                categories: [.nachwirkung], isAftermath: true)
                }

                // Momenttext / Notiz
                if !moment.text.isEmpty {
                    VStack(alignment: .leading, spacing: 8) {
                        Label("Notiz", systemImage: "note.text")
                            .font(.subheadline.bold())
                            .foregroundStyle(.secondary)
                        Text(moment.text)
                            .font(.body)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .padding(16)
                    .background(Color(.secondarySystemBackground), in: RoundedRectangle(cornerRadius: 12))
                    .padding(.horizontal, 20)
                }

                // Fertig-Button
                Button {
                    onDismiss()
                } label: {
                    Text("Fertig")
                        .font(.headline)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 14)
                        .background(Color.accentColor, in: RoundedRectangle(cornerRadius: 12))
                        .foregroundStyle(.white)
                }
                .padding(.horizontal, 20)
                .padding(.bottom, 32)
            }
        }
    }

    // MARK: - Hilfsmethoden

    private func summaryCard(title: LocalizedStringKey, average: Double, categories: [RatingCategory], isAftermath: Bool) -> some View {
        VStack(alignment: .leading, spacing: 14) {
            HStack {
                Text(title)
                    .font(.subheadline.bold())
                Spacer()
                scoreCircle(value: average)
            }

            ForEach(categories, id: \.self) { category in
                if let avg = moment.averageForCategory(category, aftermath: isAftermath) {
                    categoryRow(category: category, average: avg)
                }
            }
        }
        .padding(16)
        .background(Color(.secondarySystemBackground), in: RoundedRectangle(cornerRadius: 12))
        .padding(.horizontal, 20)
    }

    private func categoryRow(category: RatingCategory, average: Double) -> some View {
        HStack(spacing: 10) {
            Image(systemName: category.icon)
                .foregroundStyle(category.color)
                .frame(width: 20)
            Text(LocalizedStringKey(category.rawValue))
                .font(.subheadline)
            Spacer()
            GeometryReader { geo in
                ZStack(alignment: .leading) {
                    RoundedRectangle(cornerRadius: 4)
                        .fill(Color(.systemGray5))
                    RoundedRectangle(cornerRadius: 4)
                        .fill(barColor(for: average))
                        .frame(width: geo.size.width * CGFloat((average + 2) / 4))
                }
            }
            .frame(height: 8)
            .frame(maxWidth: 100)
        }
    }

    private func scoreCircle(value: Double) -> some View {
        ZStack {
            Circle()
                .fill(barColor(for: value).opacity(0.15))
                .frame(width: 40, height: 40)
            Text(String(format: "%.1f", value))
                .font(.caption.bold())
                .foregroundStyle(barColor(for: value))
        }
    }

    private func barColor(for value: Double) -> Color {
        switch value {
        case ..<(-0.5): return .red
        case (-0.5)..<(0.5): return Color(.systemGray3)
        case (0.5)...: return .green
        default: return .gray
        }
    }
}
