using Microsoft.Extensions.DependencyInjection;
using Tiku.Application;
using Tiku.Infrastructure;
using Tiku.Infrastructure.Persistence;
using Tiku.IntegrationTests.Infrastructure;

namespace Tiku.IntegrationTests;

public sealed class ModulePersistenceBoundaryTests
{
    [Fact]
    public void Module_persistence_capabilities_share_one_scoped_db_context()
    {
        var services = new ServiceCollection();
        services.AddLogging();
        services.AddApplication();
        services.AddInfrastructure(DockerPostgresTestDefaults.CreateConnectionString("tiku_module_boundary_test"));

        using var provider = services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true });
        using var scope = provider.CreateScope();
        var concrete = scope.ServiceProvider.GetRequiredService<TikuDbContext>();
        var capabilityTypes = new[]
        {
            typeof(IIdentityPersistence),
            typeof(ITenancyPersistence),
            typeof(ITenantAdministrationPersistence),
            typeof(ICatalogPersistence),
            typeof(IQuestionBankPersistence),
            typeof(IContentAssetPersistence),
            typeof(ILearningPersistence),
            typeof(ICommercePersistence),
            typeof(IPointsPersistence),
            typeof(IGrowthPersistence),
            typeof(IJobsOperationsPersistence),
            typeof(IPlatformControlPlanePersistence),
            typeof(IPlatformAdministrationPersistence),
            typeof(IPlatformQuestionBankAdministrationPersistence),
            typeof(IPlatformTenantCapabilitiesPersistence),
            typeof(IPlatformBillingPersistence),
            typeof(IOwnerActivationPersistence),
            typeof(IBootstrapPersistence)
        };

        foreach (var capabilityType in capabilityTypes)
            Assert.Same(concrete, scope.ServiceProvider.GetRequiredService(capabilityType));
    }
}
