using Bunit;
using Microsoft.AspNetCore.Components;
using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;

/// <summary>
/// Component tests for <see cref="KeysetPager"/> — the purely-presentational
/// cursor-pagination button bar shared by SiteCallsReport and AuditResultsGrid.
/// </summary>
public class KeysetPagerTests : BunitContext
{
    [Fact]
    public void PrevDisabled_WhenCannotGoBack()
    {
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.CanGoBack, false)
            .Add(p => p.HasNextPage, true));

        var prev = cut.Find("[data-test='keyset-prev']");

        Assert.NotNull(prev.GetAttribute("disabled"));
    }

    [Fact]
    public void NextDisabled_WhenNoNextPage()
    {
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.CanGoBack, true)
            .Add(p => p.HasNextPage, false));

        var next = cut.Find("[data-test='keyset-next']");

        Assert.NotNull(next.GetAttribute("disabled"));
    }

    [Fact]
    public void Prev_Click_InvokesOnPrevious()
    {
        var fired = false;
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.CanGoBack, true)
            .Add(p => p.HasNextPage, false)
            .Add(p => p.OnPrevious,
                EventCallback.Factory.Create(this, () => { fired = true; })));

        cut.Find("[data-test='keyset-prev']").Click();

        Assert.True(fired);
    }

    [Fact]
    public void Next_Click_InvokesOnNext()
    {
        var fired = false;
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.CanGoBack, false)
            .Add(p => p.HasNextPage, true)
            .Add(p => p.OnNext,
                EventCallback.Factory.Create(this, () => { fired = true; })));

        cut.Find("[data-test='keyset-next']").Click();

        Assert.True(fired);
    }

    [Fact]
    public void Summary_RendersPageAndRows()
    {
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.PageNumber, 3)
            .Add(p => p.RowCount, 42));

        var summary = cut.Find("[data-test='keyset-summary']");

        Assert.Contains("Page 3", summary.TextContent);
        Assert.Contains("42 rows", summary.TextContent);
    }

    [Fact]
    public void BothDisabled_WhenDisabled()
    {
        var cut = Render<KeysetPager>(ps => ps
            .Add(p => p.CanGoBack, true)
            .Add(p => p.HasNextPage, true)
            .Add(p => p.Disabled, true));

        var prev = cut.Find("[data-test='keyset-prev']");
        var next = cut.Find("[data-test='keyset-next']");

        Assert.NotNull(prev.GetAttribute("disabled"));
        Assert.NotNull(next.GetAttribute("disabled"));
    }
}
