import SwiftUI

struct GoalProgressBar: View {
    let progress: Double
    var tint: Color = .appSecondary
    var background: Color = Color.gray.opacity(0.15)
    var iconName: String = "flag.checkered"
    var iconColor: Color = .appSecondary

    private var clampedProgress: CGFloat {
        CGFloat(min(max(progress, 0), 1))
    }

    var body: some View {
        GeometryReader { geometry in
            let width = geometry.size.width
            let iconOffset = max(0, width * clampedProgress - 10)

            ZStack(alignment: .leading) {
                Capsule()
                    .fill(background)
                Capsule()
                    .fill(tint)
                    .frame(width: width * clampedProgress)
            }
            .overlay(alignment: .leading) {
                Image(systemName: iconName)
                    .font(.caption2)
                    .foregroundColor(iconColor)
                    .offset(x: iconOffset)
            }
        }
        .frame(height: 8)
        .accessibilityLabel("Goal progress")
        .accessibilityValue("\(Int(progress * 100)) percent")
    }
}

#Preview {
    GoalProgressBar(progress: 0.45)
        .padding()
}
