using Deal.Contracts.Integrations.Abstractions;
using Deal.Infrastructure.Integrations.Abstractions;
using Deal.Infrastructure.Integrations.Models;
using Deal.Infrastructure.Integrations.Options;
using Deal.Infrastructure.Integrations.Services;
using Deal.Infrastructure.Integrations.Sources;
using Deal.Infrastructure.Persistence;
using Deal.Infrastructure.Persistence.Abstractions;
using Deal.Infrastructure.Persistence.Repositories;
using Deal.Infrastructure.Persistence.Services;
using Deal.Infrastructure.Security;
using Deal.Infrastructure.Security.Abstractions;
using Deal.Infrastructure.Security.Services;
using Deal.Infrastructure.Services;
using Deal.Infrastructure.Tenancy;
using Deal.Modules.Cards.Application.Abstractions;
using Deal.Modules.Cards.Application.Sources;
using Deal.Modules.Discovery.Application.Abstractions;
using Deal.Modules.Kanban.Application.Abstractions;
using Deal.Modules.Pipeline.Application.Abstractions;
using Deal.Modules.Settings.Application.Abstractions;
using Deal.Modules.Telegram.Application;
using Deal.Modules.Tenants.Application.Abstractions;
using Deal.Modules.Tenants.Application.Models;
using Deal.SharedKernel.Tenants.Abstractions;
using Microsoft.Extensions.DependencyInjection;
namespace Deal.Infrastructure;
///
/// DI-регистрация адаптеров персистентности Deal.Infrastructure.
///
public static class ServiceCollectionExtensions
{
///
/// Регистрирует EF-адаптеры портов модулей
///
/// Дефолт-бюджет лениво создаваемых строк tenant_limits. Передаётся из конфигурации/env DEAL_DEFAULT_AI_BUDGET в Program.cs.
public static IServiceCollection AddDealPersistence(this IServiceCollection services, TokenLimitDefaults? tenantLimitDefaults = null)
{
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddSingleton(tenantLimitDefaults ?? TokenBudgetDefaults.Default);
services.AddScoped();
services.AddScoped(provider => provider.GetRequiredService().Create());
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
return services;
}
///
/// Регистрирует адаптеры внешних интеграций
///
/// Конфигурация секции Services:Ml — выбор реализации IMlClient.
/// Конфигурация секции Services:Ai — выбор реализации IAiClassifier/IAiTools.
/// Конфигурация секции Services:Telegram — выбор реализации ITelegramGateway.
public static IServiceCollection AddDealIntegrations(
this IServiceCollection services,
MlServiceOptions mlOptions,
AiServiceOptions aiOptions,
TelegramServiceOptions telegramOptions,
MtlsCertificates? mtlsCertificates = null)
{
services.AddScoped();
if (mlOptions.UseLocal)
{
services.AddScoped();
}
else
{
services.AddSingleton(new MlGrpcConnection(mlOptions, mtlsCertificates));
services.AddSingleton();
services.AddScoped();
services.AddScoped(provider => provider.GetRequiredService());
services.AddScoped(provider => provider.GetRequiredService());
}
services.AddScoped();
if (aiOptions.UseLocal)
{
services.AddScoped();
services.AddScoped();
}
else
{
services.AddSingleton(new AiGrpcConnection(aiOptions, mtlsCertificates));
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped(provider => provider.GetRequiredService().Create());
services.AddScoped();
services.AddScoped();
services.AddScoped(provider => provider.GetRequiredService().Create());
}
if (telegramOptions.UseLocal)
{
services.AddSingleton();
}
else
{
services.AddSingleton(new TelegramGrpcConnection(telegramOptions, mtlsCertificates));
services.AddScoped();
services.AddScoped(provider => provider.GetRequiredService());
}
return services;
}
///
/// Регистрирует сервисы шифрования секретов
///
/// ContentRoot приложения — каталог по умолчанию для файла-ключа data/encryption.key.
public static IServiceCollection AddDealSecurity(this IServiceCollection services, string contentRootPath)
{
EncryptionKeyProvider keyProvider = new(contentRootPath);
services.AddSingleton(keyProvider);
services.AddSingleton();
services.AddSingleton(provider => provider.GetRequiredService().Create());
return services;
}
}