//
//  ProgressRing.swift
//  Andromida
//
//  A circular progress indicator for the history calendar.
//

import SwiftUI
import Bedrock

/// A circular progress ring showing completion percentage.
struct ProgressRing: View {
    let progress: Double  // 0.0 to 1.0
    let size: CGFloat
    var lineWidth: CGFloat = 3
    var showPercentage: Bool = false
    
    private var color: Color {
        if progress >= 1.0 { return AppStatus.success }
        if progress >= 0.5 { return AppAccent.primary }
        return AppTextColors.tertiary
    }
    
    var body: some View {
        ZStack {
            // Background ring
            Circle()
                .stroke(
                    AppBorder.subtle,
                    lineWidth: lineWidth
                )
            
            // Progress ring
            Circle()
                .trim(from: 0, to: min(progress, 1.0))
                .stroke(
                    color,
                    style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)
                )
                .rotationEffect(.degrees(-90))
                .animation(.easeOut(duration: Design.Animation.standard), value: progress)
            
            // Percentage text inside ring
            if showPercentage {
                Text("\(Int(progress * 100))%")
                    .font(.system(size: size * 0.25, weight: .bold))
                    .foregroundStyle(AppTextColors.primary)
                    .minimumScaleFactor(0.8)
                    .lineLimit(1)
            }
        }
        .frame(width: size, height: size)
        .accessibilityElement(children: .ignore)
        .accessibilityLabel(String(localized: "\(Int(progress * 100)) percent complete"))
    }
}

#Preview {
    VStack(spacing: Design.Spacing.large) {
        HStack(spacing: Design.Spacing.large) {
            ProgressRing(progress: 0.0, size: 40)
            ProgressRing(progress: 0.25, size: 40)
            ProgressRing(progress: 0.5, size: 40)
            ProgressRing(progress: 0.75, size: 40)
            ProgressRing(progress: 1.0, size: 40)
        }
        
        HStack(spacing: Design.Spacing.large) {
            ProgressRing(progress: 0.0, size: 80, lineWidth: 6, showPercentage: true)
            ProgressRing(progress: 0.5, size: 80, lineWidth: 6, showPercentage: true)
            ProgressRing(progress: 1.0, size: 80, lineWidth: 6, showPercentage: true)
        }
    }
    .padding()
    .background(AppSurface.primary)
}
