//
//  HabitPerformanceView.swift
//  Andromida
//
//  Displays habit completion rates for a ritual.
//

import SwiftUI
import Bedrock

/// A view showing habit completion performance within a ritual.
struct HabitPerformanceView: View {
    let habitRates: [(habit: ArcHabit, rate: Double)]
    
    private var sortedByRate: [(habit: ArcHabit, rate: Double)] {
        habitRates.sorted { $0.rate > $1.rate }
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: Design.Spacing.small) {
            Text(String(localized: "Habit Performance"))
                .font(.headline)
                .foregroundStyle(AppTextColors.primary)
            
            VStack(spacing: Design.Spacing.xSmall) {
                ForEach(sortedByRate, id: \.habit.id) { item in
                    habitRow(item.habit, rate: item.rate)
                }
            }
        }
        .padding(Design.Spacing.large)
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(AppSurface.card)
        .clipShape(.rect(cornerRadius: Design.CornerRadius.large))
    }
    
    private func habitRow(_ habit: ArcHabit, rate: Double) -> some View {
        VStack(spacing: Design.Spacing.xSmall) {
            HStack(spacing: Design.Spacing.medium) {
                Image(systemName: habit.symbolName)
                    .foregroundStyle(colorForRate(rate))
                    .frame(width: AppMetrics.Size.iconMedium)
                
                Text(habit.title)
                    .font(.subheadline)
                    .foregroundStyle(AppTextColors.primary)
                
                Spacer()
                
                Text("\(Int(rate * 100))%")
                    .font(.subheadline)
                    .bold()
                    .foregroundStyle(colorForRate(rate))
            }
            
            // Progress bar
            GeometryReader { geometry in
                ZStack(alignment: .leading) {
                    Rectangle()
                        .fill(AppBorder.subtle)
                        .frame(height: Design.LineWidth.thick)
                    
                    Rectangle()
                        .fill(colorForRate(rate))
                        .frame(width: geometry.size.width * min(rate, 1.0), height: Design.LineWidth.thick)
                }
            }
            .frame(height: Design.LineWidth.thick)
            .clipShape(.rect(cornerRadius: Design.CornerRadius.small))
        }
        .padding(.vertical, Design.Spacing.xSmall)
        .accessibilityElement(children: .combine)
        .accessibilityLabel(habit.title)
        .accessibilityValue(String(localized: "\(Int(rate * 100)) percent completion rate"))
    }
    
    private func colorForRate(_ rate: Double) -> Color {
        if rate >= 0.8 { return AppStatus.success }
        if rate >= 0.5 { return AppAccent.primary }
        return AppTextColors.tertiary
    }
}

#Preview {
    HabitPerformanceView(habitRates: [
        (ArcHabit(title: "Meditate", symbolName: "brain.fill"), 0.85),
        (ArcHabit(title: "Exercise", symbolName: "figure.walk"), 0.65),
        (ArcHabit(title: "Read", symbolName: "book.fill"), 0.40)
    ])
    .padding(Design.Spacing.large)
    .background(AppSurface.primary)
}
