using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ZB.MOM.WW.Configuration;

namespace ZB.MOM.WW.ScadaBridge.KpiHistory;

/// <summary>
/// Composition root for the KPI History component.
/// </summary>
/// <remarks>
/// <para>
/// Scaffolds the component: it binds and validates
/// <see cref="KpiHistoryOptions"/>. The recorder actor itself is created via
/// <c>Props</c> in the Host on the active central node, not registered
/// here — mirroring the Notification Outbox singleton wiring.
/// </para>
/// </remarks>
public static class ServiceCollectionExtensions
{
    /// <summary>Configuration section bound to <see cref="KpiHistoryOptions"/>.</summary>
    public const string OptionsSection = "ScadaBridge:KpiHistory";

    /// <summary>
    /// Registers the KPI History component services: the validated
    /// <see cref="KpiHistoryOptions"/> binding. Idempotent re-registration of the
    /// validator is handled by the shared <c>AddValidatedOptions</c> helper
    /// (<c>TryAddEnumerable</c>); the options binding itself is call-once per
    /// <see cref="IServiceCollection"/>.
    /// </summary>
    /// <param name="services">The service collection to register into.</param>
    /// <param name="configuration">Application configuration used to bind <see cref="KpiHistoryOptions"/>.</param>
    /// <returns>The same <see cref="IServiceCollection"/> for chaining.</returns>
    public static IServiceCollection AddKpiHistory(this IServiceCollection services, IConfiguration configuration)
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configuration);

        // Binds the "ScadaBridge:KpiHistory" section, registers the validator,
        // and enables ValidateOnStart in one call — same shape as AddAuditLog.
        services.AddValidatedOptions<KpiHistoryOptions, KpiHistoryOptionsValidator>(configuration, OptionsSection);

        return services;
    }
}
