using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace ms_useCaseService.Database;

/// <summary>
/// Factory for creating instances of RulesContext for design time DbContext creation.
/// </summary>
public class RulesFactory : IDesignTimeDbContextFactory<RulesContext>
{
    /// <summary>
    /// Creates a new instance of the RulesContext class using the specified arguments.
    /// </summary>
    /// <param name="args">The arguments to use for creating the context.</param>
    /// <returns>An instance of the RulesContext class.</returns>
    public RulesContext CreateDbContext(string[] args)
    {
        //Getting connection string
        var configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();
        var optionsBuilder = new DbContextOptionsBuilder<RulesContext>();
        optionsBuilder.UseNpgsql(configuration.GetConnectionString("Database")); //Using PostgreSQL
        
        //Initialitzing RulesContext
        var RulesContext = new RulesContext(optionsBuilder.Options);
        RulesContext.SaveChanges();
        return RulesContext;
    }
}