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

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

public class FailoverUrlParserTests
{
    /// <summary>Verifies that Parse returns only the primary URL when CSV failover list is null.</summary>
    [Fact]
    public void Parse_CsvNull_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", (string?)null);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    /// <summary>Verifies that Parse returns only the primary URL when CSV failover list is empty.</summary>
    [Fact]
    public void Parse_CsvEmpty_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "");
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    /// <summary>Verifies that Parse returns only the primary URL when CSV failover list is whitespace.</summary>
    [Fact]
    public void Parse_CsvWhitespace_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", "   ");
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    /// <summary>Verifies that Parse returns both primary and single failover URL.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse returns primary URL and all failover URLs from CSV list.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse trims leading and trailing whitespace from failover URLs.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse deduplicates the primary URL if present in the failover list.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse performs case-insensitive deduplication of URLs.</summary>
    [Fact]
    public void Parse_DeduplicatesCaseInsensitive()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://Primary:4840", "opc.tcp://primary:4840");
        result.ShouldBe(["opc.tcp://Primary:4840"]);
    }

    /// <summary>Verifies that Parse returns only the primary URL when array of failover URLs is null.</summary>
    [Fact]
    public void Parse_ArrayNull_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", (string[]?)null);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    /// <summary>Verifies that Parse returns only the primary URL when array of failover URLs is empty.</summary>
    [Fact]
    public void Parse_ArrayEmpty_ReturnsPrimaryOnly()
    {
        var result = FailoverUrlParser.Parse("opc.tcp://primary:4840", []);
        result.ShouldBe(["opc.tcp://primary:4840"]);
    }

    /// <summary>Verifies that Parse returns primary URL and all failover URLs from array.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse deduplicates URLs from array if primary URL is present.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse trims whitespace from URLs in array.</summary>
    [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"]);
    }

    /// <summary>Verifies that Parse skips null and empty URLs from array.</summary>
    [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"]);
    }
}