using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using AipGateway.API.Application.Modules;
using AipGateway.API.Infrastructure;
using AipGateway.API.Application.Pipeline.Authorization;
using AipGateway.API.Application.UtilityServices;
using System.Reflection;
using FluentValidation;
using AipGateway.API.Application.Pipeline.Middlewares.Logging;
using Microsoft.Extensions.Logging;

namespace AipGateway.API.Domain
{
    public static class Dependencyinjection
    {
        public static IServiceCollection InjectDependencies(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddJWTAuthorization(configuration)
                    .AddRolePermissionAuthorization();

            services.AddDomainLayerServices()
                    .AddInfrastructureLayerServices(configuration)
                    .AddApplicationLayerServices();


            ServiceContainer.provider = services.BuildServiceProvider();
            ILoggerFactory loggerFactory = ServiceContainer.provider.GetService<ILoggerFactory>();
            ILogger<LoggingDbProcess> logger = loggerFactory.CreateLogger<LoggingDbProcess>();
            ServiceContainer.loggingDbProcess = new LoggingDbProcess(logger);
            return services;
        }

        private static ILogger<T> CreateLogger<T>(ILoggerFactory factory)
        {
            return new Logger<T>(factory);
        }

        public static IServiceCollection AddApplicationLayerServices(this IServiceCollection services)
        {
            services//.AddAutoMapper(Assembly.GetExecutingAssembly())
                    .AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

            services.AddModules()
                    .UtilityServices();

            return services;
        }
    }
}
