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

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

/// <summary>
/// Regression tests for CentralUI-008. <c>&lt;input type="datetime-local"&gt;</c>
/// yields the value the user typed in their <i>browser-local</i> time zone. The
/// audit-log filter converted it with <c>new DateTimeOffset(value, TimeSpan.Zero)</c>
/// — relabelling the local wall-clock value as UTC, shifting the query window by
/// the user's offset. <see cref="BrowserTime.LocalInputToUtc"/> performs the
/// correct conversion: it applies the browser offset from <c>getTimezoneOffset()</c>.
/// </summary>
public class BrowserTimeTests
{
    [Fact]
    public void LocalInputToUtc_Null_ReturnsNull()
    {
        Assert.Null(BrowserTime.LocalInputToUtc(null, 0));
    }

    [Fact]
    public void LocalInputToUtc_UtcBrowser_LeavesTimeUnchanged()
    {
        // getTimezoneOffset() == 0 for a UTC browser.
        var local = new DateTime(2026, 5, 16, 9, 30, 0);

        var utc = BrowserTime.LocalInputToUtc(local, 0);

        Assert.Equal(new DateTimeOffset(2026, 5, 16, 9, 30, 0, TimeSpan.Zero), utc);
    }

    [Fact]
    public void LocalInputToUtc_PositiveUtcOffsetBrowser_SubtractsOffset()
    {
        // A browser at UTC+2 reports getTimezoneOffset() == -120.
        // The user typing 09:30 local means 07:30 UTC.
        var local = new DateTime(2026, 5, 16, 9, 30, 0);

        var utc = BrowserTime.LocalInputToUtc(local, -120);

        Assert.Equal(new DateTimeOffset(2026, 5, 16, 7, 30, 0, TimeSpan.Zero), utc);
    }

    [Fact]
    public void LocalInputToUtc_NegativeUtcOffsetBrowser_AddsOffset()
    {
        // A browser at UTC-5 (US Eastern, standard time) reports getTimezoneOffset() == 300.
        // The user typing 09:30 local means 14:30 UTC.
        var local = new DateTime(2026, 5, 16, 9, 30, 0);

        var utc = BrowserTime.LocalInputToUtc(local, 300);

        Assert.Equal(new DateTimeOffset(2026, 5, 16, 14, 30, 0, TimeSpan.Zero), utc);
    }

    [Fact]
    public void LocalInputToUtc_NonUtcBrowser_DoesNotEqualNaiveRelabelling()
    {
        // The pre-fix bug: naive new DateTimeOffset(value, TimeSpan.Zero).
        var local = new DateTime(2026, 5, 16, 9, 30, 0);
        var naive = new DateTimeOffset(local, TimeSpan.Zero);

        var correct = BrowserTime.LocalInputToUtc(local, 300);

        Assert.NotEqual(naive, correct);
    }
}
