using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Helpers;

namespace ZB.MOM.WW.OtOpcUa.Client.Shared.Tests.Helpers;

public class FailoverUrlParserTests
{
    [Fact]
    public void Parse_CsvNull_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", (string?)null);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    [Fact]
    public void Parse_CsvEmpty_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "");
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    [Fact]
    public void Parse_CsvWhitespace_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "   ");
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    [Fact]
    public void Parse_SingleFailover_ReturnsBoth()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "opc.tcp://backup:4840");
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }

    [Fact]
    public void Parse_MultipleFailovers_ReturnsAll()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "opc.tcp://backup1:4840,opc.tcp://backup2:4840");
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup1:4840", "opc.tcp://backup2:4840"]);
    }

    [Fact]
    public void Parse_TrimsWhitespace()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "  opc.tcp://backup:4840  ");
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }

    [Fact]
    public void Parse_DeduplicatesPrimaryInFailoverList()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "opc.tcp://primary:4840,opc.tcp://backup:4840");
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }

    [Fact]
    public void Parse_DeduplicatesCaseInsensitive()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://Primary:4840", "opc.tcp://primary:4840");
        result.ShouldBe(["opc.tcp://Primary:4840"]);
    }

    [Fact]
    public void Parse_ArrayNull_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", (string[]?)null);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    [Fact]
    public void Parse_ArrayEmpty_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", []);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    [Fact]
    public void Parse_ArrayWithUrls_ReturnsAll()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840",
            ["opc.tcp://backup1:4840", "opc.tcp://backup2:4840"]);
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup1:4840", "opc.tcp://backup2:4840"]);
    }

    [Fact]
    public void Parse_ArrayDeduplicates()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840",
            ["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }

    [Fact]
    public void Parse_ArrayTrimsWhitespace()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840",
            ["  opc.tcp://backup:4840  "]);
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }

    [Fact]
    public void Parse_ArraySkipsNullAndEmpty()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840",
            [null!, "", "opc.tcp://backup:4840"]);
        result.ShouldBe(["opc.tcp://primary:4840", "opc.tcp://backup:4840"]);
    }
}