using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.AbCip;

namespace ZB.MOM.WW.OtOpcUa.Driver.AbCip.Tests;

[Trait("Category", "Unit")]
public sealed class AbCipUdtMemberTests
{
    /// <summary>Verifies that UDT with declared members expands to individual member variables.</summary>
    [Fact]
    public async Task UDT_with_declared_members_fans_out_to_member_variables()
    {
        var builder = new RecordingBuilder();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(
                new AbCipTagDefinition(
                    Name: "Motor1",
                    DeviceHostAddress: "ab://10.0.0.5/1,0",
                    TagPath: "Motor1",
                    DataType: AbCipDataType.Structure,
                    Members:
                    [
                        new AbCipStructureMember("Speed", AbCipDataType.DInt),
                        new AbCipStructureMember("Running", AbCipDataType.Bool, Writable: false),
                        new AbCipStructureMember("SetPoint", AbCipDataType.Real, WriteIdempotent: true),
                    ]))
            ,
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.DiscoverAsync(builder, CancellationToken.None);

        builder.Folders.ShouldContain(f => f.BrowseName == "Motor1");
        var variables = builder.Variables.Select(v => (v.BrowseName, v.Info.FullName)).ToList();
        variables.ShouldContain(("Speed", "Motor1.Speed"));
        variables.ShouldContain(("Running", "Motor1.Running"));
        variables.ShouldContain(("SetPoint", "Motor1.SetPoint"));

        builder.Variables.Single(v => v.BrowseName == "Running").Info.SecurityClass
            .ShouldBe(SecurityClassification.ViewOnly);
        builder.Variables.Single(v => v.BrowseName == "SetPoint").Info.WriteIdempotent
            .ShouldBeTrue();
    }

    /// <summary>Verifies that UDT members can be read via synthesised full reference paths.</summary>
    [Fact]
    public async Task UDT_members_resolvable_for_read_via_synthesised_full_reference()
    {
        var factory = new FakeAbCipTagFactory
        {
            Customise = p => p.TagName switch
            {
                "Motor1.Speed" => new FakeAbCipTag(p) { Value = 1800 },
                "Motor1.Running" => new FakeAbCipTag(p) { Value = true },
                _ => new FakeAbCipTag(p),
            },
        };
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(
                new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
                    Members:
                    [
                        new AbCipStructureMember("Speed", AbCipDataType.DInt),
                        new AbCipStructureMember("Running", AbCipDataType.Bool),
                    ]))
            ,
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

        var snapshots = await drv.ReadAsync(["Motor1.Speed", "Motor1.Running"], CancellationToken.None);

        snapshots[0].Value.ShouldBe(1800);
        snapshots[0].StatusCode.ShouldBe(AbCipStatusMapper.Good);
        snapshots[1].Value.ShouldBe(true);
        snapshots[1].StatusCode.ShouldBe(AbCipStatusMapper.Good);
    }

    /// <summary>Verifies that UDT member writes route through synthesised tag paths.</summary>
    [Fact]
    public async Task UDT_member_write_routes_through_synthesised_tagpath()
    {
        var factory = new FakeAbCipTagFactory();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(
                new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
                    Members:
                    [
                        new AbCipStructureMember("SetPoint", AbCipDataType.Real),
                    ]))
            ,
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

        var results = await drv.WriteAsync(
            [new WriteRequest("Motor1.SetPoint", 42.5f)], CancellationToken.None);

        results.Single().StatusCode.ShouldBe(AbCipStatusMapper.Good);
        factory.Tags["Motor1.SetPoint"].Value.ShouldBe(42.5f);
    }

    /// <summary>Verifies that UDT member read/write operations respect the Writable flag.</summary>
    [Fact]
    public async Task UDT_member_read_write_honours_member_Writable_flag()
    {
        var factory = new FakeAbCipTagFactory();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(
                new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
                    Members:
                    [
                        new AbCipStructureMember("Status", AbCipDataType.DInt, Writable: false),
                    ]))
            ,
        }, "drv-1", factory);
        await drv.InitializeAsync("{}", CancellationToken.None);

        var results = await drv.WriteAsync(
            [new WriteRequest("Motor1.Status", 1)], CancellationToken.None);

        results.Single().StatusCode.ShouldBe(AbCipStatusMapper.BadNotWritable);
    }

    /// <summary>Verifies that structure tags without declared members appear as single variables.</summary>
    [Fact]
    public async Task Structure_tag_without_members_is_emitted_as_single_variable()
    {
        // Fallback path: a Structure tag with no declared Members still appears as a Variable so
        // downstream configuration can address it manually. This matches the "black box" note in
        // AbCipTagDefinition's docstring.
        var builder = new RecordingBuilder();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(new AbCipTagDefinition("OpaqueUdt", "ab://10.0.0.5/1,0", "OpaqueUdt", AbCipDataType.Structure)),
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.DiscoverAsync(builder, CancellationToken.None);

        builder.Variables.ShouldContain(v => v.BrowseName == "OpaqueUdt");
        builder.Folders.ShouldNotContain(f => f.BrowseName == "OpaqueUdt");
    }

    /// <summary>Verifies that empty member lists are treated the same as null.</summary>
    [Fact]
    public async Task Empty_Members_list_is_treated_like_null()
    {
        var builder = new RecordingBuilder();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(new AbCipTagDefinition("EmptyUdt", "ab://10.0.0.5/1,0", "E", AbCipDataType.Structure, Members: [])),
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.DiscoverAsync(builder, CancellationToken.None);

        builder.Folders.ShouldNotContain(f => f.BrowseName == "EmptyUdt");
        builder.Variables.ShouldContain(v => v.BrowseName == "EmptyUdt");
    }

    /// <summary>Verifies that UDT members and flat tags can coexist in the address space.</summary>
    [Fact]
    public async Task UDT_members_mixed_with_flat_tags_coexist()
    {
        var builder = new RecordingBuilder();
        var drv = new AbCipDriver(new AbCipDriverOptions
        {
            Devices = [new AbCipDeviceOptions("ab://10.0.0.5/1,0")],
            RawTags = AbCipRawTags.From(
                new AbCipTagDefinition("FlatA", "ab://10.0.0.5/1,0", "A", AbCipDataType.DInt),
                new AbCipTagDefinition("Motor1", "ab://10.0.0.5/1,0", "Motor1", AbCipDataType.Structure,
                    Members:
                    [
                        new AbCipStructureMember("Speed", AbCipDataType.DInt),
                    ]),
                new AbCipTagDefinition("FlatB", "ab://10.0.0.5/1,0", "B", AbCipDataType.Real))
            ,
        }, "drv-1");
        await drv.InitializeAsync("{}", CancellationToken.None);

        await drv.DiscoverAsync(builder, CancellationToken.None);

        builder.Variables.Select(v => v.BrowseName).ShouldBe(["FlatA", "Speed", "FlatB"], ignoreOrder: true);
    }

    // ---- helpers ----

    /// <summary>Recording builder for testing address space construction.</summary>
    private sealed class RecordingBuilder : IAddressSpaceBuilder
    {
        /// <summary>Gets the collected folders.</summary>
        public List<(string BrowseName, string DisplayName)> Folders { get; } = new();
        /// <summary>Gets the collected variables.</summary>
        public List<(string BrowseName, DriverAttributeInfo Info)> Variables { get; } = new();

        /// <summary>Records a folder in the address space.</summary>
        /// <param name="browseName">The browse name of the folder.</param>
        /// <param name="displayName">The display name of the folder.</param>
        public IAddressSpaceBuilder Folder(string browseName, string displayName)
        { Folders.Add((browseName, displayName)); return this; }

        /// <summary>Records a variable in the address space.</summary>
        /// <param name="browseName">The browse name of the variable.</param>
        /// <param name="displayName">The display name of the variable.</param>
        /// <param name="info">The driver attribute information for the variable.</param>
        public IVariableHandle Variable(string browseName, string displayName, DriverAttributeInfo info)
        { Variables.Add((browseName, info)); return new Handle(info.FullName); }

        /// <summary>Records a property (stub implementation for testing).</summary>
        /// <param name="_">The property name (unused in this stub).</param>
        /// <param name="__">The property data type (unused in this stub).</param>
        /// <param name="___">The property value (unused in this stub).</param>
        public void AddProperty(string _, DriverDataType __, object? ___) { }

        /// <summary>Variable handle implementation for testing.</summary>
        private sealed class Handle(string fullRef) : IVariableHandle
        {
            /// <summary>Gets the full reference path.</summary>
            public string FullReference => fullRef;
            /// <summary>Marks this handle as an alarm condition.</summary>
            /// <param name="info">The alarm condition information.</param>
            public IAlarmConditionSink MarkAsAlarmCondition(AlarmConditionInfo info) => new NullSink();
        }
        /// <summary>Null alarm condition sink for testing.</summary>
        private sealed class NullSink : IAlarmConditionSink
        {
            /// <summary>Handles alarm transitions (stub).</summary>
            /// <param name="args">The alarm event arguments.</param>
            public void OnTransition(AlarmEventArgs args) { }
        }
    }
}
