@namespace SufiChain.SufiBlazor.Components.Feedback

<span class="@CssClass" style="@Style" aria-label="@AriaLabel" @attributes="AdditionalAttributes">
    @if (!Dot)
    {
        @if (ChildContent != null)
        {
            @ChildContent
        }
        else
        {
            @DisplayValue
        }
    }
</span>

@code {
    /// <summary>
    /// Child content for displaying custom text or elements inside the badge.
    /// If provided, this takes precedence over the Value property.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Badge value (number). Used when ChildContent is not provided.
    /// </summary>
    [Parameter]
    public int? Value { get; set; }

    /// <summary>
    /// Maximum value to display before showing max+.
    /// </summary>
    [Parameter]
    public int Max { get; set; } = 99;

    /// <summary>
    /// Whether to show as a dot (no value).
    /// </summary>
    [Parameter]
    public bool Dot { get; set; }

    /// <summary>
    /// Badge color.
    /// </summary>
    [Parameter]
    public SbColor Color { get; set; } = SbColor.Primary;

    /// <summary>
    /// Accessible label.
    /// </summary>
    [Parameter]
    public string? AriaLabel { get; set; }

    /// <summary>
    /// Additional CSS classes.
    /// </summary>
    [Parameter]
    public string? Class { get; set; }

    /// <summary>
    /// Inline styles.
    /// </summary>
    [Parameter]
    public string? Style { get; set; }

    /// <summary>
    /// Additional HTML attributes.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object>? AdditionalAttributes { get; set; }

    private string DisplayValue
    {
        get
        {
            if (Value == null) return "";
            return Value > Max ? $"{Max}+" : Value.ToString()!;
        }
    }

    private string CssClass
    {
        get
        {
            var classes = new List<string> { "sb-badge" };

            classes.Add($"sb-badge--{Color.ToString().ToLowerInvariant()}");

            if (Dot)
            {
                classes.Add("sb-badge--dot");
            }

            if (!string.IsNullOrWhiteSpace(Class))
            {
                classes.Add(Class);
            }

            return string.Join(" ", classes);
        }
    }
}
