using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Modules.Rating.Api.Database;
using Modules.Rating.Api.Repositories;
using System.Reflection;

namespace Modules.Rating.Api
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddRates(this IServiceCollection services, string? connectionString)
        {
            if (string.IsNullOrWhiteSpace(connectionString)) throw new ArgumentNullException(nameof(connectionString));
            services.AddDbContext<RatingDbContext>(options =>
            {
                options.UseNpgsql(connectionString, q =>
                {
                    q.MigrationsAssembly(Assembly.GetAssembly(typeof(RatingDbContext))!.GetName().Name);
                    q.MigrationsHistoryTable("__Migrations");
                });
                //options.UseNpgsql(connectionString);
                //AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); //Включаем UTC-время.
                //options.UseLazyLoadingProxies(); // lazy loading
            });

            AddRepositories(services);
            return services;
        }

        private static void AddRepositories(IServiceCollection services)
        {
            services.AddScoped<RatingRepository>();
        }
    }
}
