import SwiftUI

struct StatView: View {
    enum IconType {
        case system(String)
        case asset(String)
    }

    let icon: IconType
    let value: String
    let label: String
    var color: Color = Color.appPrimary

    /// Convenience initializer that accepts a plain string for backward compatibility.
    /// Asset names are detected automatically; everything else is treated as an SF Symbol.
    init(icon: String, value: String, label: String, color: Color = Color.appPrimary) {
        if icon == "outline" {
            self.icon = .asset(icon)
        } else {
            self.icon = .system(icon)
        }
        self.value = value
        self.label = label
        self.color = color
    }

    init(icon: IconType, value: String, label: String, color: Color = Color.appPrimary) {
        self.icon = icon
        self.value = value
        self.label = label
        self.color = color
    }

    var body: some View {
        VStack(spacing: OrganicSpacing.compact) {
            ZStack {
                Circle()
                    .fill(color.opacity(0.1))
                    .frame(width: 52, height: 52)

                switch icon {
                case .asset(let name):
                    Image(name)
                        .renderingMode(.template)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 24, height: 24)
                        .foregroundColor(color)
                case .system(let name):
                    Image(systemName: name)
                        .font(.system(size: 22, weight: .semibold))
                        .foregroundColor(color)
                }
            }

            Text(value)
                .font(.system(size: 22, weight: .bold, design: .rounded))
                .foregroundColor(Color.appTextPrimary)

            Text(label)
                .font(.system(size: 12, weight: .medium))
                .foregroundColor(Color.appTextSecondary)
                .multilineTextAlignment(.center)
        }
        .frame(maxWidth: .infinity)
        .accessibilityElement(children: .combine)
        .accessibilityLabel(A11y.Common.stat(value: value, label: label))
    }
}
