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

namespace ZB.MOM.WW.OtOpcUa.Driver.S7.IntegrationTests.S7_1500;

/// <summary>
///     End-to-end smoke against the python-snap7 S7-1500 profile. Drives the real
///     <see cref="S7Driver"/> + real S7netplus ISO-on-TCP stack + real CIP-free
///     S7comm exchange against <c>localhost:1102</c>. Success proves initialisation,
///     typed reads (u16 / i16 / i32 / f32 / bool-with-bit), and a write-then-read
///     round-trip all work against a real S7 server — the baseline everything
///     S7-specific (byte-order, optimized-DB differences, probe behaviour) layers on.
/// </summary>
[Collection(Snap7ServerCollection.Name)]
[Trait("Category", "Integration")]
[Trait("Device", "S7_1500")]
public sealed class S7_1500SmokeTests(Snap7ServerFixture sim)
{
    /// <summary>Verifies that the driver reads a seeded u16 value through real S7comm.</summary>
    [Fact]
    public async Task Driver_reads_seeded_u16_through_real_S7comm()
    {
        if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);

        var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
        await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-u16");
        await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);

        var snapshots = await drv.ReadAsync(
            [S7_1500Profile.ProbeTag], TestContext.Current.CancellationToken);

        snapshots.Count.ShouldBe(1);
        snapshots[0].StatusCode.ShouldBe(0u, "seeded u16 read must succeed end-to-end");
        Convert.ToInt32(snapshots[0].Value).ShouldBe(S7_1500Profile.ProbeSeedValue);
    }

    /// <summary>Verifies that the driver reads a typed batch of seeded values.</summary>
    [Fact]
    public async Task Driver_reads_seeded_typed_batch()
    {
        if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);

        var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
        await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-batch");
        await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);

        var snapshots = await drv.ReadAsync(
            [S7_1500Profile.SmokeI16Tag, S7_1500Profile.SmokeI32Tag,
             S7_1500Profile.SmokeF32Tag, S7_1500Profile.SmokeBoolTag],
            TestContext.Current.CancellationToken);

        snapshots.Count.ShouldBe(4);
        foreach (var s in snapshots) s.StatusCode.ShouldBe(0u);

        Convert.ToInt32(snapshots[0].Value).ShouldBe((int)S7_1500Profile.SmokeI16SeedValue);
        Convert.ToInt32(snapshots[1].Value).ShouldBe(S7_1500Profile.SmokeI32SeedValue);
        Convert.ToSingle(snapshots[2].Value).ShouldBe(S7_1500Profile.SmokeF32SeedValue, tolerance: 0.0001f);
        Convert.ToBoolean(snapshots[3].Value).ShouldBeTrue();
    }

    /// <summary>Verifies that the driver can write and then read back a value on a scratch register.</summary>
    [Fact]
    public async Task Driver_write_then_read_round_trip_on_scratch_word()
    {
        if (sim.SkipReason is not null) Assert.Skip(sim.SkipReason);

        var options = S7_1500Profile.BuildOptions(sim.Host, sim.Port);
        await using var drv = new S7Driver(options, driverInstanceId: "s7-smoke-write");
        await drv.InitializeAsync("{}", TestContext.Current.CancellationToken);

        const ushort probe = 0xBEEF;
        var writeResults = await drv.WriteAsync(
            [new WriteRequest(S7_1500Profile.WriteScratchTag, probe)],
            TestContext.Current.CancellationToken);
        writeResults.Count.ShouldBe(1);
        writeResults[0].StatusCode.ShouldBe(0u,
            "write must succeed against snap7's DB1.DBW100 scratch register");

        var readResults = await drv.ReadAsync(
            [S7_1500Profile.WriteScratchTag], TestContext.Current.CancellationToken);
        readResults.Count.ShouldBe(1);
        readResults[0].StatusCode.ShouldBe(0u);
        Convert.ToInt32(readResults[0].Value).ShouldBe(probe);
    }
}
