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

namespace ZB.MOM.WW.ScadaBridge.ManagementService;

public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Registers the management actor holder and management service options.
    /// </summary>
    /// <param name="services">The service collection to register into.</param>
    /// <returns>The <paramref name="services"/> instance for chaining.</returns>
    public static IServiceCollection AddManagementService(this IServiceCollection services)
    {
        services.AddSingleton<ManagementActorHolder>();
        services.AddOptions<ManagementServiceOptions>()
            .BindConfiguration("ScadaBridge:ManagementService")
            .ValidateOnStart();
        services.TryAddEnumerable(
            ServiceDescriptor.Singleton<IValidateOptions<ManagementServiceOptions>,
                ManagementServiceOptionsValidator>());
        return services;
    }
}
