using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace ZB.MOM.WW.ScadaBridge.ClusterInfrastructure;

/// <summary>
/// DI registration for the Cluster Infrastructure component.
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Registers the Cluster Infrastructure services. This component owns the
    /// cluster <em>configuration contract</em> (<see cref="ClusterOptions"/>); the
    /// Akka.NET bootstrap itself lives in <c>ZB.MOM.WW.ScadaBridge.Host</c>
    /// (see <c>Component-ClusterInfrastructure.md</c>).
    /// <para>
    /// Registering the <see cref="ClusterOptionsValidator"/> means a misconfigured
    /// <c>ScadaBridge:Cluster</c> section (e.g. <c>MinNrOfMembers: 2</c> or a quorum
    /// split-brain strategy) throws an <see cref="OptionsValidationException"/> the
    /// first time <see cref="IOptions{TOptions}"/> is resolved, rather than booting
    /// into a broken cluster.
    /// </para>
    /// </summary>
    /// <param name="services">The service collection to register into.</param>
    /// <returns>The same <paramref name="services"/> instance, for call chaining.</returns>
    public static IServiceCollection AddClusterInfrastructure(this IServiceCollection services)
    {
        services.TryAddEnumerable(
            ServiceDescriptor.Singleton<IValidateOptions<ClusterOptions>, ClusterOptionsValidator>());
        return services;
    }

    // The previous `AddClusterInfrastructureActors` extension
    // was dead surface — its XML doc told callers "do not call", its body
    // unconditionally threw `NotImplementedException`, and no production caller
    // existed anywhere in the solution (verified by grep). The
    // "throw loudly" decision was made while the ownership question was
    // still open; that question is now permanently settled by the
    // "Implementation Note — Code Placement" section of
    // Component-ClusterInfrastructure.md, which records that all actor wiring
    // lives in ZB.MOM.WW.ScadaBridge.Host (AkkaHostedService). Keeping a public extension
    // method that exists only to throw was API-surface noise that an IDE would
    // still suggest via auto-complete, so the method and its companion
    // `AddClusterInfrastructureActors_ThrowsRatherThanSilentlySucceeding` test
    // were both removed.
}
