namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Proxy.Supervisor;

/// <summary>
///     Tracks missed heartbeats on the dedicated heartbeat pipe per
///     <c>driver-stability.md §"Heartbeat between proxy and host"</c>: 2s cadence, 3 consecutive
///     misses = host declared dead (~6s detection).
/// </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;
    }

    public bool RecordMiss()
    {
        ConsecutiveMisses++;
        return ConsecutiveMisses >= MissesUntilDead;
    }
}
