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

/// <summary>
/// Task 24: the template editor exposes a Native Alarm Sources subsection (a new
/// tab) for authoring read-only native alarm bindings. <c>TemplateEdit</c> is a
/// heavyweight page (~10 injected services with their own graphs); like the other
/// TemplateEdit coverage in this suite (see <c>TestRunWarningTests</c>), these are
/// structural assertions over the component source that pin the subsection's
/// wiring — the tab, the authoring form (connection dropdown, source reference,
/// filter, lock), and the repository-direct CRUD calls — so it cannot silently
/// regress. The CRUD behaviour itself is covered end-to-end by the ManagementActor
/// native-alarm-source handler tests.
/// </summary>
public class TemplateNativeAlarmSourceEditorTests
{
    private static string TemplateEditMarkup
    {
        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", "Design", "TemplateEdit.razor"));
        }
    }

    [Fact]
    public void TemplateEditor_HasNativeAlarmsTab()
    {
        var markup = TemplateEditMarkup;
        Assert.Contains("_activeTab = \"native-alarms\"", markup);
        Assert.Contains("Native Alarms", markup);
        Assert.Contains("RenderNativeAlarmsTab", markup);
        Assert.Contains("Native Alarm Sources", markup);
    }

    [Fact]
    public void NativeAlarmsForm_HasConnectionSourceFilterAndLockFields()
    {
        var markup = TemplateEditMarkup;
        // Connection dropdown filtered to alarm-capable protocols.
        Assert.Contains("_alarmCapableConnections", markup);
        Assert.Contains("IsAlarmCapable", markup);
        Assert.Contains("OpcUa", markup);
        Assert.Contains("MxGateway", markup);
        // The authoring form fields.
        Assert.Contains("@bind=\"_nasName\"", markup);
        Assert.Contains("@bind=\"_nasConnection\"", markup);
        Assert.Contains("@bind=\"_nasSourceRef\"", markup);
        Assert.Contains("@bind=\"_nasFilter\"", markup);
        Assert.Contains("@bind=\"_nasIsLocked\"", markup);
    }

    [Fact]
    public void NativeAlarmsCrud_WiresRepositoryAddUpdateDeleteWithSave()
    {
        var markup = TemplateEditMarkup;
        Assert.Contains("AddTemplateNativeAlarmSourceAsync", markup);
        Assert.Contains("UpdateTemplateNativeAlarmSourceAsync", markup);
        Assert.Contains("DeleteTemplateNativeAlarmSourceAsync", markup);
        Assert.Contains("GetNativeAlarmSourcesByTemplateIdAsync", markup);
        Assert.Contains("SaveChangesAsync", markup);
        // Add/edit/delete handlers are wired to the UI.
        Assert.Contains("BeginAddNativeSource", markup);
        Assert.Contains("BeginEditNativeSource", markup);
        Assert.Contains("SaveNativeSource", markup);
        Assert.Contains("DeleteNativeSource", markup);
    }
}
