namespace ZB.MOM.WW.OtOpcUa.Driver.FOCAS.Supervisor;

/// <summary>
///     Tracks missed heartbeats from the FOCAS Host. 2s cadence + 3 consecutive misses =
///     host declared dead (~6s detection). Same defaults as Galaxy Tier-C so operators
///     see the same cadence across hosts on the /hosts Admin page.
/// </summary>
public sealed class HeartbeatMonitor
{
    public int MissesUntilDead { get; init; } = 3;

    public TimeSpan Cadence { get; init; } = TimeSpan.FromSeconds(2);

    public int ConsecutiveMisses { get; private set; }
    public DateTime? LastAckUtc { get; private set; }

    public void RecordAck(DateTime utcNow)
    {
        ConsecutiveMisses = 0;
        LastAckUtc = utcNow;
    }

    /// <summary>Records a missed heartbeat; returns <c>true</c> when the death threshold is crossed.</summary>
    public bool RecordMiss()
    {
        ConsecutiveMisses++;
        return ConsecutiveMisses >= MissesUntilDead;
    }
}
