namespace Indotalent.Infrastructure.Database.MsSQL;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

public static class MsSQLConfiguration
{
    public static IServiceCollection AddMsSQLContext<TContext>(this IServiceCollection services, string connectionString)
        where TContext : DbContext
    {
        services.AddDbContext<TContext>(options =>
            options.UseSqlServer(connectionString, m =>
                m.MigrationsAssembly(typeof(TContext).Assembly.FullName)));
        return services;
    }

    public static void InitializeDatabase<TContext>(IServiceProvider serviceProvider) where TContext : DbContext
    {
        using var scope = serviceProvider.CreateScope();
        var context = scope.ServiceProvider.GetRequiredService<TContext>();
        context.Database.EnsureCreated();
    }
}