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

/// <summary>
/// Task 25: Instance Configure exposes a "Native Alarm Source Overrides" card —
/// per-instance retarget of an inherited native alarm source binding (connection /
/// source reference / filter; blank = inherited). <c>InstanceConfigure</c> is a
/// heavyweight page (7 injected services incl. <c>InstanceService</c> and the
/// flattening pipeline), so — consistent with the template-editor coverage — these
/// are structural assertions over the component source that pin the card and its
/// repository-direct upsert/clear wiring. The override CRUD itself is covered
/// behaviorally by the ManagementActor native-alarm-source handler tests.
/// </summary>
public class InstanceConfigureNativeAlarmTests
{
    private static string InstanceConfigureMarkup
    {
        get
        {
            var dir = AppContext.BaseDirectory;
            for (var i = 0; i < 6 && dir is not null; i++)
                dir = Directory.GetParent(dir)?.FullName;
            return File.ReadAllText(Path.Combine(dir!, "src", "ZB.MOM.WW.ScadaBridge.CentralUI",
                "Components", "Pages", "Deployment", "InstanceConfigure.razor"));
        }
    }

    [Fact]
    public void InstanceConfigure_HasNativeAlarmSourceOverridesCard()
    {
        var markup = InstanceConfigureMarkup;
        Assert.Contains("Native Alarm Source Overrides", markup);
        Assert.Contains("Leave a field blank to keep the inherited value", markup);
        Assert.Contains("HasNativeOverride", markup);
    }

    [Fact]
    public void NativeOverrideRow_HasConnectionSourceRefAndFilterEditors()
    {
        var markup = InstanceConfigureMarkup;
        Assert.Contains("AlarmCapableConnections", markup);  // dropdown filtered to OPC UA / MxGateway
        Assert.Contains("_nasConnEdit", markup);
        Assert.Contains("_nasRefEdit", markup);
        Assert.Contains("_nasFilterEdit", markup);
        Assert.Contains("(inherited)", markup);
    }

    [Fact]
    public void NativeOverride_WiresRepositoryUpsertAndClear()
    {
        var markup = InstanceConfigureMarkup;
        Assert.Contains("GetNativeAlarmSourceOverridesByInstanceIdAsync", markup);
        Assert.Contains("GetNativeAlarmSourceOverrideAsync", markup);
        Assert.Contains("AddInstanceNativeAlarmSourceOverrideAsync", markup);
        Assert.Contains("UpdateInstanceNativeAlarmSourceOverrideAsync", markup);
        Assert.Contains("DeleteInstanceNativeAlarmSourceOverrideAsync", markup);
        Assert.Contains("SaveChangesAsync", markup);
        Assert.Contains("SaveNativeOverride", markup);
        Assert.Contains("ClearNativeOverride", markup);
    }
}
