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

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

/// <summary>
/// Regression tests for HealthMonitoring-015. A heartbeat-only registered site has
/// a <c>null</c> <c>LastReportReceivedAt</c> ("no full report yet"). The health
/// dashboard passes that value straight into <see cref="TimestampDisplay"/>, so the
/// component's <c>Value</c> must accept <c>DateTimeOffset?</c> and render a
/// <c>null</c> as a human-readable placeholder ("never") instead of the
/// <c>DateTimeOffset.MinValue</c> year-0001 sentinel. Non-null callers must keep
/// rendering the formatted timestamp exactly as before.
/// </summary>
public class TimestampDisplayTests : BunitContext
{
    [Fact]
    public void Render_NonNullValue_ShowsFormattedTimestamp()
    {
        var value = new DateTimeOffset(2026, 5, 17, 14, 30, 45, TimeSpan.Zero);

        var cut = Render<TimestampDisplay>(parameters => parameters
            .Add(p => p.Value, (DateTimeOffset?)value)
            .Add(p => p.Format, "HH:mm:ss"));

        var span = cut.Find("span");
        Assert.Equal(value.LocalDateTime.ToString("HH:mm:ss"), span.TextContent.Trim());
        Assert.Contains("2026-05-17 14:30:45 UTC", span.GetAttribute("title")!);
    }

    [Fact]
    public void Render_NullValue_ShowsNeverPlaceholder()
    {
        var cut = Render<TimestampDisplay>(parameters => parameters
            .Add(p => p.Value, (DateTimeOffset?)null)
            .Add(p => p.Format, "HH:mm:ss"));

        Assert.Contains("never", cut.Markup, StringComparison.OrdinalIgnoreCase);
    }

    [Fact]
    public void Render_NullValue_DoesNotRenderYear0001Sentinel()
    {
        var cut = Render<TimestampDisplay>(parameters => parameters
            .Add(p => p.Value, (DateTimeOffset?)null));

        // The year-0001 DateTimeOffset.MinValue sentinel must never reach the UI.
        Assert.DoesNotContain("0001", cut.Markup);
    }
}
