using ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Shared;

/// <summary>
/// Coverage for <see cref="DurationInput"/>, the number+unit codec behind the
/// script form's "Min time between runs" field.
/// </summary>
public class DurationInputTests
{
    // ── Split: TimeSpan -> (value, unit) ───────────────────────────────────

    [Fact]
    public void Split_Null_ReturnsBlankWithSecondsUnit()
    {
        var (value, unit) = DurationInput.Split(null);

        Assert.Null(value);
        Assert.Equal("sec", unit);
    }

    [Fact]
    public void Split_Zero_ReturnsBlank()
    {
        var (value, _) = DurationInput.Split(TimeSpan.Zero);

        Assert.Null(value);
    }

    [Fact]
    public void Split_WholeMinutes_UsesMinuteUnit()
    {
        var (value, unit) = DurationInput.Split(TimeSpan.FromMinutes(5));

        Assert.Equal("5", value);
        Assert.Equal("min", unit);
    }

    [Fact]
    public void Split_WholeSeconds_UsesSecondUnit()
    {
        var (value, unit) = DurationInput.Split(TimeSpan.FromSeconds(30));

        Assert.Equal("30", value);
        Assert.Equal("sec", unit);
    }

    [Fact]
    public void Split_SubSecond_UsesMillisecondUnit()
    {
        var (value, unit) = DurationInput.Split(TimeSpan.FromMilliseconds(250));

        Assert.Equal("250", value);
        Assert.Equal("ms", unit);
    }

    // ── Compose: (value, unit) -> TimeSpan? ────────────────────────────────

    [Fact]
    public void Compose_Blank_ReturnsNull() =>
        Assert.Null(DurationInput.Compose(null, "sec"));

    [Fact]
    public void Compose_Zero_ReturnsNull() =>
        Assert.Null(DurationInput.Compose("0", "sec"));

    [Fact]
    public void Compose_Negative_ReturnsNull() =>
        Assert.Null(DurationInput.Compose("-5", "sec"));

    [Fact]
    public void Compose_SecondsValue_BuildsDuration() =>
        Assert.Equal(TimeSpan.FromSeconds(30), DurationInput.Compose("30", "sec"));

    [Fact]
    public void Compose_MinutesValue_BuildsDuration() =>
        Assert.Equal(TimeSpan.FromMinutes(5), DurationInput.Compose("5", "min"));

    [Fact]
    public void Compose_MillisecondsValue_BuildsDuration() =>
        Assert.Equal(TimeSpan.FromMilliseconds(250), DurationInput.Compose("250", "ms"));

    // ── Round-trip ─────────────────────────────────────────────────────────

    [Theory]
    [InlineData(250)]
    [InlineData(30000)]
    [InlineData(300000)]
    public void RoundTrip_PreservesDuration(long milliseconds)
    {
        var original = TimeSpan.FromMilliseconds(milliseconds);

        var (value, unit) = DurationInput.Split(original);
        var reparsed = DurationInput.Compose(value, unit);

        Assert.Equal(original, reparsed);
    }
}
