using ZB.MOM.WW.ScadaBridge.DelmiaNotifier;

namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier.Tests;

public class ArgParserTests
{
    [Fact]
    public void Parses_all_flags()
    {
        var r = ArgParser.Parse(new[] { "-m", "Z28061", "-d", @"C:\r.nc", "-w", "W1", "-p", "P1", "-s", "0100", "-u", "op" });
        Assert.True(r.Ok);
        Assert.Equal("Z28061", r.Payload!.MachineCode);
        Assert.Equal(@"C:\r.nc", r.Payload.DownloadPath);
        Assert.Equal("W1", r.Payload.WorkOrderNumber);
        Assert.Equal("P1", r.Payload.PartNumber);
        Assert.Equal("0100", r.Payload.JobStepNumber);
        Assert.Equal("op", r.Payload.Username);
    }

    [Fact]
    public void Parses_long_flags()
    {
        var r = ArgParser.Parse(new[] { "--machine", "Z", "--downloadpath", "x", "--workorder", "W", "--partnumber", "P" });
        Assert.True(r.Ok);
        Assert.Equal("Z", r.Payload!.MachineCode);
        Assert.Equal("x", r.Payload.DownloadPath);
    }

    [Fact]
    public void Missing_required_returns_error()
    {
        var r = ArgParser.Parse(new[] { "-m", "Z28061", "-d", @"C:\r.nc", "-w", "W1" }); // no -p
        Assert.False(r.Ok);
        Assert.Contains("partnumber", r.Error, System.StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void Optional_flags_may_be_omitted()
    {
        var r = ArgParser.Parse(new[] { "-m", "Z", "-d", "x", "-w", "W", "-p", "P" });
        Assert.True(r.Ok);
        Assert.Null(r.Payload!.Username);
        Assert.Null(r.Payload.JobStepNumber);
    }

    [Fact]
    public void Unknown_flag_returns_error()
    {
        var r = ArgParser.Parse(new[] { "-z", "x" });
        Assert.False(r.Ok);
    }

    [Fact]
    public void Flag_without_value_returns_error()
    {
        var r = ArgParser.Parse(new[] { "-m" });
        Assert.False(r.Ok);
    }
}
