import SwiftUI

struct MacroBar: View {
    let label: String
    let current: Double
    let goal: Double
    let color: Color
    var showGrams: Bool = true

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

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack {
                Text(label)
                    .font(.caption)
                    .fontWeight(.medium)
                    .foregroundStyle(Color.text3)
                Spacer()
                if showGrams {
                    Text("\(Int(current))g / \(Int(goal))g")
                        .font(.caption)
                        .foregroundStyle(Color.text3)
                } else {
                    Text("\(Int(current)) / \(Int(goal))")
                        .font(.caption)
                        .foregroundStyle(Color.text3)
                }
            }

            GeometryReader { geo in
                ZStack(alignment: .leading) {
                    Capsule()
                        .fill(color.opacity(0.12))
                        .frame(height: 6)

                    Capsule()
                        .fill(color)
                        .frame(width: geo.size.width * progress, height: 6)
                        .animation(.easeOut(duration: 0.5), value: progress)
                }
            }
            .frame(height: 6)
        }
    }
}

struct MacroBarCompact: View {
    let current: Double
    let goal: Double
    let color: Color

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

    var body: some View {
        GeometryReader { geo in
            ZStack(alignment: .leading) {
                Capsule()
                    .fill(color.opacity(0.12))

                Capsule()
                    .fill(color)
                    .frame(width: geo.size.width * progress)
                    .animation(.easeOut(duration: 0.5), value: progress)
            }
        }
        .frame(height: 4)
    }
}
