using System.Text.Json;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Management;

namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Messages;

/// <summary>
/// Task 21 (PLAN-07): the additive optional <c>SiteIdentifier</c> trailing param on the
/// Browse/Verify/Cert-trust management commands. Verifies backward compatibility (legacy
/// JSON without the field deserializes to null) and round-trip survival when set.
/// </summary>
public class BrowseVerifyCertSiteIdentifierTests
{
    private static readonly JsonSerializerOptions Options = new()
    {
        PropertyNameCaseInsensitive = true
    };

    // ── Backward compatibility: legacy JSON (no SiteIdentifier) → null ──

    [Fact]
    public void BrowseNodeCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = """{"ConnectionName":"conn","ParentNodeId":"ns=2;s=Node","ContinuationToken":null}""";
        var cmd = JsonSerializer.Deserialize<BrowseNodeCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
        Assert.Equal("conn", cmd.ConnectionName);
    }

    [Fact]
    public void SearchAddressSpaceCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = """{"ConnectionName":"conn","Query":"pump","MaxDepth":5,"MaxResults":100}""";
        var cmd = JsonSerializer.Deserialize<SearchAddressSpaceCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
        Assert.Equal("pump", cmd.Query);
    }

    [Fact]
    public void VerifyEndpointCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = """{"ConnectionName":"conn","Protocol":"OpcUa","ConfigJson":"{}"}""";
        var cmd = JsonSerializer.Deserialize<VerifyEndpointCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
        Assert.Equal("OpcUa", cmd.Protocol);
    }

    [Fact]
    public void TrustServerCertCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = """{"ConnectionName":"conn","DerBase64":"AAAA","Thumbprint":"ABCD"}""";
        var cmd = JsonSerializer.Deserialize<TrustServerCertCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
        Assert.Equal("ABCD", cmd.Thumbprint);
    }

    [Fact]
    public void RemoveServerCertCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = """{"Thumbprint":"ABCD"}""";
        var cmd = JsonSerializer.Deserialize<RemoveServerCertCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
        Assert.Equal("ABCD", cmd.Thumbprint);
    }

    [Fact]
    public void ListServerCertsCommand_LegacyJson_SiteIdentifierNull()
    {
        var json = "{}";
        var cmd = JsonSerializer.Deserialize<ListServerCertsCommand>(json, Options)!;
        Assert.Null(cmd.SiteIdentifier);
    }

    // ── Round-trip: SiteIdentifier set survives serialize → deserialize ──

    [Fact]
    public void BrowseNodeCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new BrowseNodeCommand("conn", "ns=2;s=Node", "tok", "SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<BrowseNodeCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }

    [Fact]
    public void SearchAddressSpaceCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new SearchAddressSpaceCommand("conn", "pump", 5, 100, "SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<SearchAddressSpaceCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }

    [Fact]
    public void VerifyEndpointCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new VerifyEndpointCommand("conn", "OpcUa", "{}", "SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<VerifyEndpointCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }

    [Fact]
    public void TrustServerCertCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new TrustServerCertCommand("conn", "AAAA", "ABCD", "SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<TrustServerCertCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }

    [Fact]
    public void RemoveServerCertCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new RemoveServerCertCommand("ABCD", "SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<RemoveServerCertCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }

    [Fact]
    public void ListServerCertsCommand_RoundTrip_SiteIdentifierSurvives()
    {
        var original = new ListServerCertsCommand("SITE-01");
        var json = JsonSerializer.Serialize(original, Options);
        var round = JsonSerializer.Deserialize<ListServerCertsCommand>(json, Options)!;
        Assert.Equal("SITE-01", round.SiteIdentifier);
        Assert.Equal(original, round);
    }
}
