using ZB.MOM.WW.ScadaBridge.Commons.Types.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;

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

/// <summary>
/// Audit Log #23 (M8): tests for the shared lax multi-value query-param parsers
/// used by the ManagementService + CentralUI audit endpoints and the
/// <c>AuditLogPage</c> drill-in parser. The contract under test: parse each
/// repeated value independently, silently drop unparseable/blank elements, and
/// collapse an empty result to <c>null</c>.
/// </summary>
public class AuditQueryParamParsersTests
{
    [Fact]
    public void ParseEnumList_NullInput_ReturnsNull()
    {
        Assert.Null(AuditQueryParamParsers.ParseEnumList<AuditChannel>(null));
    }

    [Fact]
    public void ParseEnumList_EmptyInput_ReturnsNull()
    {
        Assert.Null(AuditQueryParamParsers.ParseEnumList<AuditChannel>(Array.Empty<string?>()));
    }

    [Fact]
    public void ParseEnumList_AllValuesValid_ParsesEverything()
    {
        var result = AuditQueryParamParsers.ParseEnumList<AuditChannel>(
            new[] { "ApiOutbound", "DbOutbound" });
        Assert.Equal(new[] { AuditChannel.ApiOutbound, AuditChannel.DbOutbound }, result);
    }

    [Fact]
    public void ParseEnumList_IsCaseInsensitive()
    {
        var result = AuditQueryParamParsers.ParseEnumList<AuditChannel>(new[] { "apioutbound" });
        Assert.Equal(new[] { AuditChannel.ApiOutbound }, result);
    }

    [Fact]
    public void ParseEnumList_DropsUnparseableElement_KeepsTheRest()
    {
        var result = AuditQueryParamParsers.ParseEnumList<AuditChannel>(
            new[] { "ApiOutbound", "NotAChannel", "Notification" });
        Assert.Equal(new[] { AuditChannel.ApiOutbound, AuditChannel.Notification }, result);
    }

    [Fact]
    public void ParseEnumList_AllValuesUnparseable_ReturnsNull()
    {
        Assert.Null(AuditQueryParamParsers.ParseEnumList<AuditStatus>(new[] { "Bogus", "" }));
    }

    [Fact]
    public void ParseStringList_NullInput_ReturnsNull()
    {
        Assert.Null(AuditQueryParamParsers.ParseStringList(null));
    }

    [Fact]
    public void ParseStringList_TrimsValuesAndDropsBlanks()
    {
        var result = AuditQueryParamParsers.ParseStringList(
            new[] { "  site-1 ", "", "   ", "site-2", null });
        Assert.Equal(new[] { "site-1", "site-2" }, result);
    }

    [Fact]
    public void ParseStringList_AllBlank_ReturnsNull()
    {
        Assert.Null(AuditQueryParamParsers.ParseStringList(new[] { "", "  ", null }));
    }
}
