using ZB.MOM.WW.ScadaBridge.HealthMonitoring;

namespace ZB.MOM.WW.ScadaBridge.AuditLog.Site;

/// <summary>
/// Bridges <see cref="IAuditWriteFailureCounter"/>
/// (incremented by <see cref="FallbackAuditWriter"/> every time the primary
/// SQLite writer throws) into <see cref="ISiteHealthCollector"/> so the count
/// surfaces in the site health report payload as
/// <c>SiteHealthReport.SiteAuditWriteFailures</c>.
/// </summary>
/// <remarks>
/// <para>
/// Registered by <see cref="ServiceCollectionExtensions.AddAuditLogHealthMetricsBridge"/>;
/// callers must register <c>AddHealthMonitoring()</c> first so
/// <see cref="ISiteHealthCollector"/> resolves. The default <see cref="AddAuditLog"/>
/// registration keeps <see cref="NoOpAuditWriteFailureCounter"/> for nodes
/// where Site Health Monitoring is not wired (the silent-sink contract — audit
/// write failures must NEVER abort the user-facing action, alog.md §7).
/// </para>
/// </remarks>
public sealed class HealthMetricsAuditWriteFailureCounter : IAuditWriteFailureCounter
{
    private readonly ISiteHealthCollector _collector;

    /// <summary>
    /// Initializes a new <see cref="HealthMetricsAuditWriteFailureCounter"/> backed by the given health collector.
    /// </summary>
    /// <param name="collector">The site health collector to increment on each audit write failure.</param>
    public HealthMetricsAuditWriteFailureCounter(ISiteHealthCollector collector)
    {
        _collector = collector ?? throw new ArgumentNullException(nameof(collector));
    }

    /// <inheritdoc/>
    public void Increment() => _collector.IncrementSiteAuditWriteFailures();
}
