using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;

namespace ZB.MOM.WW.OtOpcUa.Admin.Services;

/// <summary>
///     Surfaces the local-node historian queue health on the Admin UI's
///     <c>/alarms/historian</c> diagnostics page (Phase 7 plan decisions #16/#21).
///     Exposes queue depth / drain state / last-error, and lets the operator retry
///     dead-lettered rows without restarting the node.
/// </summary>
/// <remarks>
///     The sink injected here is the server-process <see cref="IAlarmHistorianSink"/>.
///     When <see cref="NullAlarmHistorianSink"/> is bound (historian disabled for this
///     deployment), <see cref="TryRetryDeadLettered"/> silently returns 0 and
///     <see cref="GetStatus"/> reports <see cref="HistorianDrainState.Disabled"/>.
/// </remarks>
public sealed class HistorianDiagnosticsService(IAlarmHistorianSink sink)
{
    public HistorianSinkStatus GetStatus() => sink.GetStatus();

    /// <summary>
    ///     Operator action from the UI's "Retry dead-lettered" button. Returns the number
    ///     of rows revived so the UI can flash a confirmation. When the live sink doesn't
    ///     implement retry (test doubles, Null sink), returns 0.
    /// </summary>
    public int TryRetryDeadLettered()
    {
        if (sink is SqliteStoreAndForwardSink concrete)
            return concrete.RetryDeadLettered();
        return 0;
    }
}
