using Microsoft.Playwright;
using Xunit;
using ZB.MOM.WW.ScadaBridge.CentralUI.PlaywrightTests.Cluster;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.PlaywrightTests.Monitoring;

/// <summary>
/// End-to-end render / no-hang guard for the central Parked Messages page
/// (<c>/monitoring/parked-messages</c>).
///
/// <para>
/// <b>Why this is a render guard and NOT a mutation test (unlike
/// <see cref="Notifications.NotificationActionTests"/>):</b> parked store-and-forward
/// messages live in the SITE's local SQLite buffer, not in central MS SQL. The page
/// resolves them by relaying a <c>ParkedMessageQueryRequest</c> to the owning site over
/// the cluster (an Akka Ask answered by the site's S&amp;F singleton). There is no central
/// table to seed — a directly-INSERTed central row cannot produce a parked S&amp;F message —
/// so this test cannot deterministically seed a row to act on. Instead it asserts the
/// singleton-backed query <em>resolves</em> (renders the results table or the empty-state
/// card) within a generous window rather than hanging on the cross-cluster Ask — the
/// regression class this guards against. Empty results are tolerated.
/// </para>
///
/// <para>
/// Gated on <see cref="ClusterAvailability"/> via <c>Skip.IfNot</c>: when the cluster is
/// unreachable the fact reports as Skipped (not Failed), matching the established suite
/// idiom. The query relays to a live site, so the cluster (not just MSSQL) must be up.
/// </para>
/// </summary>
[Collection("Playwright")]
public class ParkedMessagesTests
{
    private const string ParkedMessagesUrl = "/monitoring/parked-messages";

    private readonly PlaywrightFixture _fixture;

    public ParkedMessagesTests(PlaywrightFixture fixture)
    {
        _fixture = fixture;
    }

    [SkippableFact]
    public async Task ParkedMessages_QueryForSite_RendersWithoutHang()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);

        var page = await _fixture.NewAuthenticatedPageAsync();
        await page.GotoAsync($"{PlaywrightFixture.BaseUrl}{ParkedMessagesUrl}");
        await page.WaitForLoadStateAsync(LoadState.NetworkIdle);

        await Assertions.Expect(page.Locator("h4:has-text('Parked Messages')")).ToBeVisibleAsync();

        // Select site-a — the <option> value is the SiteIdentifier "site-a". The select is
        // an @onchange handler that, on a non-empty selection, kicks off the query itself;
        // SelectOptionAsync raises the change event so the query fires. Click Query as well
        // to be explicit (the button is enabled once a site is selected).
        await page.Locator("#pm-filter-site").SelectOptionAsync("site-a");
        await page.Locator("button.btn.btn-primary.btn-sm:has-text('Query')").ClickAsync();

        // The singleton-backed query resolves to EITHER the results table or the empty-state
        // card. Web-first assertion with a generous timeout (20s) — the relay round-trips to
        // the site over the cluster, and the regression this guards is the query hanging
        // (leaving the page stuck on "Loading…"). Either terminal state proves it resolved.
        var resolved = page.Locator("table.parked-table, div.card-body:has-text('No parked messages')");
        await Assertions.Expect(resolved.First).ToBeVisibleAsync(new() { Timeout = 20_000 });
    }
}
