using System.Text.Json;
using ScadaLink.Commons.Messages.Management;

namespace ScadaLink.Commons.Tests.Messages;

/// <summary>
/// Regression tests for Commons-008 — <see cref="SetConnectionBindingsCommand"/>
/// previously declared its bindings as <c>IReadOnlyList&lt;(string, int)&gt;</c>.
/// A <c>ValueTuple</c> serializes as <c>Item1</c>/<c>Item2</c> and cannot evolve
/// additively (REQ-COM-5a). It is now a named <see cref="ConnectionBinding"/> record.
/// </summary>
public class ConnectionBindingSerializationTests
{
    [Fact]
    public void ConnectionBinding_SerializesWithNamedProperties()
    {
        var json = JsonSerializer.Serialize(new ConnectionBinding("Temperature", 42));

        using var doc = JsonDocument.Parse(json);
        Assert.Equal(JsonValueKind.String, doc.RootElement.GetProperty("AttributeName").ValueKind);
        Assert.Equal("Temperature", doc.RootElement.GetProperty("AttributeName").GetString());
        Assert.Equal(42, doc.RootElement.GetProperty("DataConnectionId").GetInt32());

        // The ValueTuple failure mode: Item1/Item2 must NOT appear.
        Assert.False(doc.RootElement.TryGetProperty("Item1", out _));
        Assert.False(doc.RootElement.TryGetProperty("Item2", out _));
    }

    [Fact]
    public void SetConnectionBindingsCommand_RoundTripsThroughJson()
    {
        var original = new SetConnectionBindingsCommand(
            7,
            new List<ConnectionBinding>
            {
                new("Speed", 5),
                new("Mode", 11),
            });

        var json = JsonSerializer.Serialize(original);
        var deserialized = JsonSerializer.Deserialize<SetConnectionBindingsCommand>(json);

        Assert.NotNull(deserialized);
        Assert.Equal(7, deserialized!.InstanceId);
        Assert.Equal(2, deserialized.Bindings.Count);
        Assert.Equal("Speed", deserialized.Bindings[0].AttributeName);
        Assert.Equal(5, deserialized.Bindings[0].DataConnectionId);
        Assert.Equal("Mode", deserialized.Bindings[1].AttributeName);
        Assert.Equal(11, deserialized.Bindings[1].DataConnectionId);

        // ConnectionBinding is a record: each element compares by value.
        Assert.Equal(original.Bindings, deserialized.Bindings);
    }
}
