using ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Kpi;

namespace ZB.MOM.WW.ScadaBridge.Commons.Tests.Kpi;

public class KpiSampleTests
{
    [Fact]
    public void KpiSample_ConstructedWithRequiredMembers_RoundTripsValues()
    {
        var capturedAt = new DateTime(2026, 6, 15, 12, 30, 0, DateTimeKind.Utc);

        var sample = new KpiSample
        {
            Id = 42,
            Source = KpiSources.NotificationOutbox,
            Metric = "QueueDepth",
            Scope = KpiScopes.Global,
            ScopeKey = null,
            Value = 17,
            CapturedAtUtc = capturedAt,
        };

        Assert.Equal(42, sample.Id);
        Assert.Equal("NotificationOutbox", sample.Source);
        Assert.Equal("QueueDepth", sample.Metric);
        Assert.Equal("Global", sample.Scope);
        Assert.Null(sample.ScopeKey);
        Assert.Equal(17, sample.Value);
        Assert.Equal(capturedAt, sample.CapturedAtUtc);
        Assert.Equal(DateTimeKind.Utc, sample.CapturedAtUtc.Kind);
    }

    [Fact]
    public void KpiSample_ScopedSample_CarriesScopeKey()
    {
        var sample = new KpiSample
        {
            Source = KpiSources.SiteHealth,
            Metric = "OldestPendingAgeSeconds",
            Scope = KpiScopes.Site,
            ScopeKey = "site-a",
            Value = 12.5,
            CapturedAtUtc = DateTime.UtcNow,
        };

        Assert.Equal(KpiScopes.Site, sample.Scope);
        Assert.Equal("site-a", sample.ScopeKey);
        Assert.Equal(12.5, sample.Value);
    }

    [Fact]
    public void KpiScopesAndSources_ConstantValues_EqualTheirNames()
    {
        Assert.Equal("NotificationOutbox", KpiSources.NotificationOutbox);
        Assert.Equal("SiteCallAudit", KpiSources.SiteCallAudit);
        Assert.Equal("AuditLog", KpiSources.AuditLog);
        Assert.Equal("SiteHealth", KpiSources.SiteHealth);

        Assert.Equal("Global", KpiScopes.Global);
        Assert.Equal("Site", KpiScopes.Site);
        Assert.Equal("Node", KpiScopes.Node);
    }

    [Fact]
    public void KpiSeriesPoint_IsValueRecordStruct_WithComponentEquality()
    {
        var bucket = new DateTime(2026, 6, 15, 0, 0, 0, DateTimeKind.Utc);

        var a = new KpiSeriesPoint(bucket, 3.0);
        var b = new KpiSeriesPoint(bucket, 3.0);

        Assert.Equal(bucket, a.BucketStartUtc);
        Assert.Equal(3.0, a.Value);
        Assert.Equal(a, b);
    }
}
