@* Reusable confirm dialog for the /raw destructive operations (delete folder / driver / device /
   tag-group / tag). Raises OnConfirm when the operator confirms; the caller runs the delete service
   call and surfaces any UnsMutationResult.Error (a blocked delete names its blocker) via a follow-up
   message. Markup mirrors the other /raw modal shells. *@

@if (Visible)
{
    <div class="modal-backdrop fade show" style="display:block"></div>
    <div class="modal fade show" tabindex="-1" role="dialog" style="display:block">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">@Title</h5>
                    <button type="button" class="btn-close" aria-label="Close" @onclick="Cancel"></button>
                </div>
                <div class="modal-body">
                    <p class="mb-0">@Message</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" @onclick="Cancel" disabled="@_busy">Cancel</button>
                    <button type="button" class="btn btn-danger" @onclick="Confirm" disabled="@_busy">
                        @if (_busy) { <span class="spinner-border spinner-border-sm me-1"></span> }
                        @ConfirmLabel
                    </button>
                </div>
            </div>
        </div>
    </div>
}

@code {
    /// <summary>Whether the dialog is shown (supports <c>@bind-Visible</c>).</summary>
    [Parameter] public bool Visible { get; set; }

    /// <summary>Two-way visibility callback.</summary>
    [Parameter] public EventCallback<bool> VisibleChanged { get; set; }

    /// <summary>The dialog title (e.g. "Delete driver").</summary>
    [Parameter] public string Title { get; set; } = "Confirm";

    /// <summary>The confirmation body message.</summary>
    [Parameter] public string Message { get; set; } = "";

    /// <summary>The confirm-button label.</summary>
    [Parameter] public string ConfirmLabel { get; set; } = "Delete";

    /// <summary>Raised when the operator confirms. The dialog awaits it, then self-closes.</summary>
    [Parameter] public EventCallback OnConfirm { get; set; }

    private bool _busy;

    private async Task Confirm()
    {
        _busy = true;
        try
        {
            await OnConfirm.InvokeAsync();
        }
        finally
        {
            _busy = false;
        }
        await Close();
    }

    private Task Cancel() => Close();

    private async Task Close()
    {
        Visible = false;
        await VisibleChanged.InvokeAsync(false);
    }
}
