using Microsoft.Extensions.Options;

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

public static class GalaxyRepositoryServiceCollectionExtensions
{
    /// <summary>Registers Galaxy Repository services in the dependency injection container.</summary>
    /// <param name="services">The service collection.</param>
    /// <returns>The service collection for chaining.</returns>
    public static IServiceCollection AddGalaxyRepository(this IServiceCollection services)
    {
        services
            .AddOptions<GalaxyRepositoryOptions>()
            .BindConfiguration(GalaxyRepositoryOptions.SectionName)
            .ValidateOnStart();

        services.AddSingleton(sp =>
            new GalaxyRepository(sp.GetRequiredService<IOptions<GalaxyRepositoryOptions>>().Value));
        services.AddSingleton<IGalaxyRepository>(sp => sp.GetRequiredService<GalaxyRepository>());

        services.AddSingleton<IGalaxyDeployNotifier, GalaxyDeployNotifier>();
        services.AddSingleton<IGalaxyHierarchySnapshotStore, GalaxyHierarchySnapshotStore>();
        services.AddSingleton<IGalaxyHierarchyCache, GalaxyHierarchyCache>();
        services.AddHostedService<GalaxyHierarchyRefreshService>();

        return services;
    }
}
