using Microsoft.Extensions.DependencyInjection;

namespace ScadaLink.HealthMonitoring;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Register site-side health monitoring services (metric collection + periodic reporting).
    /// Call this on site nodes only. For central, call AddCentralHealthAggregation() instead.
    /// </summary>
    public static IServiceCollection AddSiteHealthMonitoring(this IServiceCollection services)
    {
        services.AddSingleton<ISiteHealthCollector, SiteHealthCollector>();
        services.AddHostedService<HealthReportSender>();
        return services;
    }

    /// <summary>
    /// Register shared health monitoring services (safe for both central and site).
    /// Does not start the HealthReportSender — call AddSiteHealthMonitoring() on site nodes for that.
    /// </summary>
    public static IServiceCollection AddHealthMonitoring(this IServiceCollection services)
    {
        services.AddSingleton<ISiteHealthCollector, SiteHealthCollector>();
        return services;
    }

    /// <summary>
    /// Register central-side health aggregation services. Includes the
    /// <see cref="CentralHealthReportLoop"/> that generates a self-report
    /// for the central cluster so it appears on /monitoring/health.
    /// </summary>
    public static IServiceCollection AddCentralHealthAggregation(this IServiceCollection services)
    {
        services.AddSingleton<CentralHealthAggregator>();
        services.AddSingleton<ICentralHealthAggregator>(sp => sp.GetRequiredService<CentralHealthAggregator>());
        services.AddHostedService(sp => sp.GetRequiredService<CentralHealthAggregator>());
        services.AddHostedService<CentralHealthReportLoop>();
        return services;
    }

    public static IServiceCollection AddHealthMonitoringActors(this IServiceCollection services)
    {
        // Placeholder for Akka actor registration (Phase 4+)
        return services;
    }
}
