using Xunit;

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

/// <summary>
/// TDD coverage for the typed <see cref="CliRunner"/> fixture helpers used by
/// state-changing Central UI Playwright E2E tests to provision and tear down
/// templates, attributes, areas, and instances against the running dev cluster.
/// When the cluster / MSSQL is unreachable the facts report as Skipped (not
/// Failed), matching the established suite idiom.
/// </summary>
[Collection("Playwright")]
public class CliRunnerHelpersTests
{
    /// <summary>
    /// A freshly created template is discoverable by name prefix and can be
    /// deleted, exercising <see cref="CliRunner.CreateTemplateAsync"/>,
    /// <see cref="CliRunner.ListTemplateIdsByNamePrefixAsync"/>, and
    /// <see cref="CliRunner.DeleteTemplateAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteTemplate_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var name = CliRunner.UniqueName("tmpl");
        int id = await CliRunner.CreateTemplateAsync(name);
        try
        {
            var ids = await CliRunner.ListTemplateIdsByNamePrefixAsync(name);
            Assert.Contains(id, ids);
        }
        finally { await CliRunner.DeleteTemplateAsync(id); }
    }

    /// <summary>
    /// <see cref="CliRunner.ResolveSiteIdAsync"/> finds the well-known
    /// <c>site-a</c> seed site by its <c>siteIdentifier</c> and returns a
    /// positive id.
    /// </summary>
    [SkippableFact]
    public async Task ResolveSiteA_ReturnsId()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        Assert.True(await CliRunner.ResolveSiteIdAsync("site-a") > 0);
    }

    /// <summary>
    /// A freshly created data connection returns a positive id and is cleanly
    /// deleted in teardown, exercising <see cref="CliRunner.CreateDataConnectionAsync"/>
    /// and <see cref="CliRunner.DeleteDataConnectionAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteDataConnection_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var siteId = await CliRunner.ResolveSiteIdAsync("site-a");
        var id = await CliRunner.CreateDataConnectionAsync(siteId, CliRunner.UniqueName("conn"));
        try
        {
            Assert.True(id > 0);
        }
        finally
        {
            await CliRunner.DeleteDataConnectionAsync(id);
        }
    }

    /// <summary>
    /// A freshly created API method returns a positive id and is cleanly deleted in
    /// teardown, exercising <see cref="CliRunner.CreateApiMethodAsync"/> and
    /// <see cref="CliRunner.DeleteApiMethodAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteApiMethod_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var id = await CliRunner.CreateApiMethodAsync(CliRunner.UniqueName("method"));
        try
        {
            Assert.True(id > 0);
        }
        finally
        {
            await CliRunner.DeleteApiMethodAsync(id);
        }
    }

    /// <summary>
    /// A freshly created area is discoverable by name prefix via
    /// <see cref="CliRunner.ListAreaIdsByNamePrefixAsync"/>, confirming the round-trip
    /// used by UI tests that need to tear down areas whose ids are never surfaced.
    /// Exercises <see cref="CliRunner.CreateAreaAsync"/>,
    /// <see cref="CliRunner.ListAreaIdsByNamePrefixAsync"/>, and
    /// <see cref="CliRunner.DeleteAreaAsync"/>.
    /// </summary>
    [SkippableFact]
    public async Task ListAreaIdsByNamePrefix_FindsCreatedArea()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);

        var siteId = await CliRunner.ResolveSiteIdAsync("site-a");
        var name = CliRunner.UniqueName("listarea");
        var areaId = await CliRunner.CreateAreaAsync(siteId, name);
        try
        {
            var ids = await CliRunner.ListAreaIdsByNamePrefixAsync(siteId, name);
            Assert.Contains(areaId, ids);
        }
        finally
        {
            await CliRunner.DeleteAreaAsync(areaId);
        }
    }

    /// <summary>
    /// A freshly created external system is discoverable by name prefix and is cleanly
    /// deleted in teardown, exercising <see cref="CliRunner.CreateExternalSystemAsync"/>,
    /// <see cref="CliRunner.ListExternalSystemIdsByNamePrefixAsync"/>, and
    /// <see cref="CliRunner.DeleteExternalSystemAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteExternalSystem_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var name = CliRunner.UniqueName("extsys");
        var id = await CliRunner.CreateExternalSystemAsync(name);
        try
        {
            Assert.True(id > 0);
            Assert.Contains(id, await CliRunner.ListExternalSystemIdsByNamePrefixAsync(name));
        }
        finally { await CliRunner.DeleteExternalSystemAsync(id); }
    }

    /// <summary>
    /// A freshly created notification list is discoverable by name prefix and is cleanly
    /// deleted in teardown, exercising <see cref="CliRunner.CreateNotificationListAsync"/>,
    /// <see cref="CliRunner.ListNotificationListIdsByNamePrefixAsync"/>, and
    /// <see cref="CliRunner.DeleteNotificationListAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteNotificationList_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var name = CliRunner.UniqueName("notiflist");
        var id = await CliRunner.CreateNotificationListAsync(name);
        try
        {
            Assert.True(id > 0);
            Assert.Contains(id, await CliRunner.ListNotificationListIdsByNamePrefixAsync(name));
        }
        finally { await CliRunner.DeleteNotificationListAsync(id); }
    }

    /// <summary>
    /// A freshly created shared script returns a positive id and is cleanly deleted in
    /// teardown, exercising <see cref="CliRunner.CreateSharedScriptAsync"/> and
    /// <see cref="CliRunner.DeleteSharedScriptAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteSharedScript_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var id = await CliRunner.CreateSharedScriptAsync(CliRunner.UniqueName("script"));
        try { Assert.True(id > 0); }
        finally { await CliRunner.DeleteSharedScriptAsync(id); }
    }

    /// <summary>
    /// A freshly created LDAP→role mapping returns a positive id, is discoverable by its
    /// <c>ldapGroupName</c> in <c>security role-mapping list</c>, and is cleanly deleted in
    /// teardown, exercising <see cref="CliRunner.CreateRoleMappingAsync"/> and
    /// <see cref="CliRunner.DeleteRoleMappingAsync"/> as a round-trip.
    /// </summary>
    [SkippableFact]
    public async Task CreateThenDeleteRoleMapping_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var group = CliRunner.UniqueName("grp");
        var id = await CliRunner.CreateRoleMappingAsync(group, "Designer");
        try
        {
            Assert.True(id > 0);
            using var list = await CliRunner.RunJsonAsync("security", "role-mapping", "list");
            Assert.Contains(
                list.RootElement.EnumerateArray(),
                e => e.TryGetProperty("ldapGroupName", out var n) && n.GetString() == group);
        }
        finally { await CliRunner.DeleteRoleMappingAsync(id); }
    }

    /// <summary>
    /// Exercises the typed HiLo setpoint flags end-to-end: a template alarm added via
    /// <see cref="CliRunner.AddAlarmAsync"/> with <c>--hi</c>/<c>--hihi</c> returns a
    /// positive id, confirming the server accepted the serialized trigger-config JSON.
    /// </summary>
    [SkippableFact]
    public async Task AddAlarmWithTypedFlags_RoundTrips()
    {
        Skip.IfNot(await ClusterAvailability.IsAvailableAsync(), ClusterAvailability.SkipReason);
        var id = await CliRunner.CreateTemplateAsync(CliRunner.UniqueName("tmpl"));
        try
        {
            await CliRunner.AddAttributeAsync(id, "Value", "Double");
            var alarmId = await CliRunner.AddAlarmAsync(id, "HiHi", "HiLo", 500, attribute: "Value", hi: 80, hiHi: 95);
            Assert.True(alarmId > 0); // server accepted the serialized trigger-config JSON
        }
        finally { await CliRunner.DeleteTemplateAsync(id); }
    }
}
