import SwiftUI

struct MacroBar: View {
    let label: String
    let consumed: Double
    let goal: Double
    var color: Color = .emerald
    var unit: String = "g"

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

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack {
                Text(label)
                    .font(.caption.weight(.medium))
                    .foregroundStyle(Color.textSecondary)
                Spacer()
                Text("\(Int(consumed))/\(Int(goal))\(unit)")
                    .font(.caption.weight(.semibold))
                    .foregroundStyle(Color.textPrimary)
            }

            GeometryReader { geo in
                ZStack(alignment: .leading) {
                    RoundedRectangle(cornerRadius: 4)
                        .fill(color.opacity(0.15))
                        .frame(height: 8)

                    RoundedRectangle(cornerRadius: 4)
                        .fill(color)
                        .frame(width: geo.size.width * progress, height: 8)
                        .animation(.easeInOut(duration: 0.5), value: progress)
                }
            }
            .frame(height: 8)
        }
    }
}
