@* Reusable confirmation dialog using Bootstrap modal *@

@if (_visible)
{
    <div class="modal-backdrop fade show"></div>
    <div class="modal fade show d-block" tabindex="-1" role="dialog">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">@Title</h5>
                    <button type="button" class="btn-close" @onclick="Cancel"></button>
                </div>
                <div class="modal-body">
                    <p>@Message</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary btn-sm" @onclick="Cancel">Cancel</button>
                    <button type="button" class="btn @ConfirmButtonClass btn-sm" @onclick="Confirm">@ConfirmText</button>
                </div>
            </div>
        </div>
    </div>
}

@code {
    private bool _visible;
    private TaskCompletionSource<bool>? _tcs;

    [Parameter] public string Title { get; set; } = "Confirm";
    [Parameter] public string Message { get; set; } = "Are you sure?";
    [Parameter] public string ConfirmText { get; set; } = "Confirm";
    [Parameter] public string ConfirmButtonClass { get; set; } = "btn-danger";

    public Task<bool> ShowAsync(string? message = null, string? title = null)
    {
        if (message != null) Message = message;
        if (title != null) Title = title;
        _visible = true;
        _tcs = new TaskCompletionSource<bool>();
        StateHasChanged();
        return _tcs.Task;
    }

    private void Confirm()
    {
        _visible = false;
        _tcs?.TrySetResult(true);
    }

    private void Cancel()
    {
        _visible = false;
        _tcs?.TrySetResult(false);
    }
}
