using Microsoft.Extensions.DependencyInjection;
using Tiku.Application.Backoffice;
using Tiku.Application.Jobs;
using Tiku.Application.Security;
using Tiku.Application.Tenancy;
using Tiku.Infrastructure.Backoffice;
using Tiku.Infrastructure.Observability;
using Tiku.Infrastructure.Security;
using Tiku.Infrastructure.Tenancy;

namespace Tiku.Infrastructure;

internal static class PlatformCoreModule
{
    internal static IServiceCollection AddPlatformCoreModule(this IServiceCollection services)
    {
        services.AddScoped<ITenantDirectory, TenantDirectory>();
        services.AddScoped<IPublicTenantConfigurationQuery, PublicTenantConfigurationQuery>();
        services.AddSingleton<IRedisSecurityStore, NullRedisSecurityStore>();
        services.AddSingleton<NullAuthorizationCache>();
        services.AddSingleton<IAccessSecurityCache>(provider => provider.GetRequiredService<NullAuthorizationCache>());
        services.AddSingleton<IAuthorizationSnapshotCache>(provider =>
            provider.GetRequiredService<NullAuthorizationCache>());
        services.AddMemoryCache();
        services.AddScoped<ITenantFrontendConfigService, TenantFrontendConfigService>();
        services.AddScoped<ITenantExternalProviderConfigService, TenantExternalProviderConfigService>();
        services.AddSingleton<ITenantRuntimeCacheInvalidator, TenantRuntimeCacheInvalidator>();
        services.AddSingleton<ITenantPublicCacheInvalidator, NullTenantPublicCacheInvalidator>();
        services.AddHttpClient<IDomainOwnershipVerifier, DnsDomainOwnershipVerifier>();
        services.AddHttpClient<IDomainGatewayProvisioner, HttpDomainGatewayProvisioner>();
        services.AddScoped<ITenantDomainLifecycleService, TenantDomainLifecycleService>();
        services.AddScoped<IBackgroundJobHandler, TenantDomainRecheckJobHandler>();
        services.AddOptions<DomainLifecycleOptions>();
        services.AddSingleton<ITenantExecutionScope, TenantExecutionScope>();
        services.AddScoped<ICurrentAccessContext, CurrentAccessContext>();
        services.AddScoped<IAuthorizationStateInvalidator, AuthorizationStateInvalidator>();
        services.AddScoped<IAuthorizationCacheInvalidationProcessor, AuthorizationCacheInvalidationProcessor>();
        services.AddScoped<ITenantFeatureSnapshotProvider, TenantFeatureSnapshotProvider>();
        services.AddSingleton<TenantFeatureCacheInvalidator>();
        services.AddSingleton<ITenantFeatureCacheInvalidator>(provider =>
            provider.GetRequiredService<TenantFeatureCacheInvalidator>());
        services.AddScoped<IFeatureAccessService, FeatureAccessService>();
        services.AddScoped<IFeatureUsageReconciliationService, FeatureUsageReconciliationService>();
        services.AddOptions<FeatureUsageReconciliationOptions>();
        services.AddScoped<IOperationAuditService, OperationAuditService>();
        services.AddScoped<IDependencyReadinessProbe, DependencyReadinessProbe>();
        services.AddScoped<IBackofficeService, BackofficeService>();
        return services;
    }
}
