namespace ZB.MOM.WW.MxGateway.Server.Alarms;

/// <summary>Service-collection wiring for the gateway's central alarm monitor.</summary>
public static class AlarmsServiceCollectionExtensions
{
    /// <summary>
    ///     Registers the always-on <see cref="GatewayAlarmMonitor"/> as both
    ///     the <see cref="IGatewayAlarmService"/> singleton and a hosted
    ///     service, so it starts with the gateway host and is shared by the
    ///     gRPC alarm surface and the dashboard.
    /// </summary>
    /// <param name="services">Service collection to register services in.</param>
    /// <returns>The service collection for chaining.</returns>
    public static IServiceCollection AddGatewayAlarms(this IServiceCollection services)
    {
        services.AddSingleton<IAlarmWatchListResolver, AlarmWatchListResolver>();
        services.AddSingleton<GatewayAlarmMonitor>();
        services.AddSingleton<IGatewayAlarmService>(provider => provider.GetRequiredService<GatewayAlarmMonitor>());
        services.AddHostedService(provider => provider.GetRequiredService<GatewayAlarmMonitor>());

        return services;
    }
}
