@namespace SufiChain.SufiBlazor.Components.Actions

<button type="@Type"
        class="@CssClass"
        style="@Style"
        disabled="@IsDisabled"
        aria-label="@AriaLabel"
        aria-busy="@(Loading ? "true" : null)"
        title="@Title"
        @onclick="HandleClick"
        @attributes="AdditionalAttributes">
    @if (Loading)
    {
        <span class="sb-icon-button__spinner"></span>
    }
    else
    {
        @(Icon ?? ChildContent)
    }
</button>

@code {
    /// <summary>
    /// Icon content when set explicitly (e.g. Icon="@(...)").
    /// </summary>
    [Parameter]
    public RenderFragment? Icon { get; set; }

    /// <summary>
    /// Icon content when placed between tags (Blazor convention: content goes to ChildContent).
    /// At least one of Icon or ChildContent must be set.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// Accessible label for icon-only buttons (recommended for accessibility when no visible text).
    /// When not set, consider using Title for tooltip; screen readers may use title as fallback.
    /// </summary>
    [Parameter]
    public string? AriaLabel { get; set; }

    /// <summary>
    /// Button variant.
    /// </summary>
    [Parameter]
    public SbButtonVariant Variant { get; set; } = SbButtonVariant.Ghost;

    /// <summary>
    /// Button size.
    /// </summary>
    [Parameter]
    public SbSize Size { get; set; } = SbSize.Md;

    /// <summary>
    /// Color intent.
    /// </summary>
    [Parameter]
    public SbColor Color { get; set; } = SbColor.Default;

    /// <summary>
    /// Whether the button is disabled.
    /// </summary>
    [Parameter]
    public bool Disabled { get; set; }

    /// <summary>
    /// Whether the button is in a loading state.
    /// </summary>
    [Parameter]
    public bool Loading { get; set; }

    /// <summary>
    /// Button type (button/submit/reset).
    /// </summary>
    [Parameter]
    public string Type { get; set; } = "button";

    /// <summary>
    /// Tooltip title.
    /// </summary>
    [Parameter]
    public string? Title { get; set; }

    /// <summary>
    /// Click event callback.
    /// </summary>
    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { 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 bool IsDisabled => Disabled || Loading;

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

            classes.Add($"sb-icon-button--{Variant.ToString().ToLowerInvariant()}");
            classes.Add($"sb-icon-button--{Size.ToString().ToLowerInvariant()}");

            if (Color != SbColor.Default)
            {
                classes.Add($"sb-icon-button--{Color.ToString().ToLowerInvariant()}");
            }

            if (IsDisabled)
            {
                classes.Add("sb-icon-button--disabled");
            }

            if (Loading)
            {
                classes.Add("sb-icon-button--loading");
            }

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

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

    private async Task HandleClick(MouseEventArgs args)
    {
        if (!IsDisabled)
        {
            await OnClick.InvokeAsync(args);
        }
    }
}
