using Microsoft.Extensions.Options;

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

/// <summary>
/// Arch-review 08 §1.5: <see cref="SiteCallAuditOptions"/> drives stuck-call
/// detection, KPI windowing, the central→site relay Ask timeout, and the two
/// central-singleton scheduler cadences plus their retention window. A
/// zero/negative duration or non-positive count must be rejected at startup with
/// a clear, key-naming message rather than making an Akka scheduler spin or a KPI
/// query silently degrade. The <c>Resolved*</c> computed properties clamp away
/// bad cadence values, so the BASE properties are validated here.
/// </summary>
public class SiteCallAuditOptionsValidatorTests
{
    private static ValidateOptionsResult Validate(SiteCallAuditOptions options) =>
        new SiteCallAuditOptionsValidator().Validate(Options.DefaultName, options);

    [Fact]
    public void DefaultOptions_AreValid()
    {
        var result = Validate(new SiteCallAuditOptions());
        Assert.True(result.Succeeded, result.FailureMessage);
    }

    [Fact]
    public void ZeroStuckAgeThreshold_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { StuckAgeThreshold = TimeSpan.Zero });
        Assert.True(result.Failed);
        Assert.Contains("StuckAgeThreshold", result.FailureMessage);
    }

    [Fact]
    public void ZeroKpiInterval_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { KpiInterval = TimeSpan.Zero });
        Assert.True(result.Failed);
        Assert.Contains("KpiInterval", result.FailureMessage);
    }

    [Fact]
    public void ZeroRelayTimeout_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { RelayTimeout = TimeSpan.Zero });
        Assert.True(result.Failed);
        Assert.Contains("RelayTimeout", result.FailureMessage);
    }

    [Fact]
    public void ZeroReconciliationInterval_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { ReconciliationInterval = TimeSpan.Zero });
        Assert.True(result.Failed);
        Assert.Contains("ReconciliationInterval", result.FailureMessage);
    }

    [Fact]
    public void ZeroReconciliationBatchSize_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { ReconciliationBatchSize = 0 });
        Assert.True(result.Failed);
        Assert.Contains("ReconciliationBatchSize", result.FailureMessage);
    }

    [Fact]
    public void ZeroPurgeInterval_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { PurgeInterval = TimeSpan.Zero });
        Assert.True(result.Failed);
        Assert.Contains("PurgeInterval", result.FailureMessage);
    }

    [Fact]
    public void ZeroRetentionDays_IsRejected()
    {
        var result = Validate(new SiteCallAuditOptions { RetentionDays = 0 });
        Assert.True(result.Failed);
        Assert.Contains("RetentionDays", result.FailureMessage);
    }
}
