//
//  ValueBadge.swift
//  CasinoKit
//
//  A circular badge showing a numeric value (hand value, score, etc.).
//

import SwiftUI

/// A capsule badge showing a numeric value.
public struct ValueBadge: View {
    /// The value to display.
    public let value: Int
    
    /// The background color of the badge.
    public let color: Color
    
    /// The size of the badge (default: uses CasinoDesign.Size.valueBadge).
    public let size: CGFloat?
    
    // MARK: - Scaled Font Sizes (Dynamic Type)
    
    @ScaledMetric(relativeTo: .headline) private var valueFontSize: CGFloat = 15
    @ScaledMetric(relativeTo: .headline) private var badgeHeight: CGFloat = CasinoDesign.Size.valueBadge
    @ScaledMetric(relativeTo: .headline) private var badgePadding: CGFloat = 8
    
    /// Creates a value badge.
    /// - Parameters:
    ///   - value: The numeric value to display.
    ///   - color: The background color.
    ///   - size: Optional custom size (overrides default).
    public init(value: Int, color: Color, size: CGFloat? = nil) {
        self.value = value
        self.color = color
        self.size = size
    }
    
    private var displayHeight: CGFloat {
        size ?? badgeHeight
    }
    
    public var body: some View {
        Text("\(value)")
            .font(.system(size: valueFontSize, weight: .black, design: .rounded))
            .foregroundStyle(.white)
            .lineLimit(1)
            .fixedSize(horizontal: true, vertical: false)
            .padding(.horizontal, badgePadding)
            .frame(minWidth: displayHeight, minHeight: displayHeight)
            .background(
                Capsule()
                    .fill(color)
            )
            .accessibilityLabel("\(value)")
    }
}

#Preview {
    HStack(spacing: 20) {
        ValueBadge(value: 9, color: .blue)
        ValueBadge(value: 8, color: .red)
        ValueBadge(value: 0, color: .purple)
    }
    .padding()
    .background(Color.black)
}

