import SwiftUI

struct MacroRing: View {
    let consumed: Double
    let goal: Double
    var lineWidth: CGFloat = 10
    var color: Color = .emerald
    var size: CGFloat = 100

    private var progress: Double {
        guard goal > 0 else { return 0 }
        return min(max(consumed / goal, 0), 1.0)
    }

    var body: some View {
        ZStack {
            Circle()
                .stroke(color.opacity(0.15), lineWidth: lineWidth)

            Circle()
                .trim(from: 0, to: progress)
                .stroke(
                    color,
                    style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)
                )
                .rotationEffect(.degrees(-90))
                .animation(.easeInOut(duration: 0.6), value: progress)
        }
        .frame(width: size, height: size)
    }
}

struct MacroRingWithLabel: View {
    let consumed: Double
    let goal: Double
    let label: String
    var color: Color = .emerald
    var size: CGFloat = 100
    var lineWidth: CGFloat = 10

    var body: some View {
        ZStack {
            MacroRing(
                consumed: consumed,
                goal: goal,
                lineWidth: lineWidth,
                color: color,
                size: size
            )

            VStack(spacing: 2) {
                Text("\(Int(consumed))")
                    .font(.system(size: size * 0.22, weight: .bold, design: .rounded))
                    .foregroundStyle(Color.textPrimary)
                Text(label)
                    .font(.system(size: size * 0.1, weight: .medium))
                    .foregroundStyle(Color.textSecondary)
            }
        }
    }
}
