using System.CommandLine;
using ZB.MOM.WW.ScadaBridge.CLI.Commands;

namespace ZB.MOM.WW.ScadaBridge.CLI.Tests.Commands;

/// <summary>
/// PLAN-07 Task 17 (arch-review P3): <c>secured-write list</c> must expose <c>--skip</c>
/// / <c>--take</c> offset-paging options alongside the <c>--status</c> / <c>--site</c>
/// filters, and map them onto <see cref="Commons.Messages.Management.ListSecuredWritesCommand"/>.
/// Omitting the paging flags preserves the historical unpaged default (skip 0, take 200).
/// </summary>
public class SecuredWriteListCommandTests
{
    private static readonly Option<string> Url = new("--url") { Recursive = true };
    private static readonly Option<string> Format = new("--format") { Recursive = true };
    private static readonly Option<string> Username = new("--username") { Recursive = true };
    private static readonly Option<string> Password = new("--password") { Recursive = true };

    private static Command Group() => SecuredWriteCommands.Build(Url, Format, Username, Password);

    private static Command List() => Group().Subcommands.Single(c => c.Name == "list");

    private static Option? FindOption(Command command, string name)
        => command.Options.SingleOrDefault(o => o.Name == name);

    [Fact]
    public void List_HasSkipAndTakeOptions()
    {
        var list = List();
        Assert.NotNull(FindOption(list, "--skip"));
        Assert.NotNull(FindOption(list, "--take"));
    }

    [Fact]
    public void List_ParsesSkipTakeAndFilters_ForwardsToCommand()
    {
        var parse = Group().Parse(new[]
        {
            "list", "--status", "Pending", "--site", "SITE1", "--skip", "10", "--take", "25",
        });

        Assert.Empty(parse.Errors);
        var command = SecuredWriteCommands.BuildListCommand(parse);
        Assert.Equal("Pending", command.Status);
        Assert.Equal("SITE1", command.SiteId);
        Assert.Equal(10, command.Skip);
        Assert.Equal(25, command.Take);
    }

    [Fact]
    public void List_OmittedPaging_UsesUnpagedDefaults()
    {
        var parse = Group().Parse(new[] { "list" });

        Assert.Empty(parse.Errors);
        var command = SecuredWriteCommands.BuildListCommand(parse);
        Assert.Null(command.Status);
        Assert.Null(command.SiteId);
        Assert.Equal(0, command.Skip);
        Assert.Equal(200, command.Take);
    }
}
