using Microsoft.Extensions.DependencyInjection;

namespace Fengling.Platform.Infrastructure;

public static class Extensions
{
    public static IServiceCollection AddPlatformCore<TContext>(this IServiceCollection services,
        Action<DbContextOptionsBuilder>? optionsAction = null,
        Action<IServiceCollection>? serviceAction = null
        )
        where TContext : PlatformDbContext
    {
        if (optionsAction != null)
        {
            var isRegistry = services.Any(x => x.ImplementationType == typeof(TContext));
            if (!isRegistry)
            {
                services.AddDbContext<TContext>(optionsAction);
            }
        }
        
        // Platform 服务
        services.AddScoped<ITenantStore, TenantStore<TContext>>();
        services.AddScoped<ITenantManager, TenantManager>();
        
        // Gateway 服务
        services.AddScoped<IRouteStore, RouteStore<TContext>>();
        services.AddScoped<IInstanceStore, InstanceStore<TContext>>();
        services.AddScoped<IRouteManager, RouteManager>();
        
        serviceAction?.Invoke(services);

        return services;
    }
}