using System;
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace Skcks.Study.AbpProject.EntityFrameworkCore;

/* This class is needed for EF Core console commands
 * (like Add-Migration and Update-Database commands) */
public class AbpProjectDbContextFactory : IDesignTimeDbContextFactory<AbpProjectDbContext>
{
    public AbpProjectDbContext CreateDbContext(string[] args)
    {
        var configuration = BuildConfiguration();
        
        AbpProjectEfCoreEntityExtensionMappings.Configure();

        var builder = new DbContextOptionsBuilder<AbpProjectDbContext>()
            .UseSqlServer(configuration.GetConnectionString("Default"));
        
        return new AbpProjectDbContext(builder.Options);
    }

    private static IConfigurationRoot BuildConfiguration()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Skcks.Study.AbpProject.DbMigrator/"))
            .AddJsonFile("appsettings.json", optional: false);

        return builder.Build();
    }
}
