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

namespace Kynareth.Models;

public class KynarethDbContextFactory : IDesignTimeDbContextFactory<KynarethDbContext>
{
	public KynarethDbContext CreateDbContext(ILoggerFactory? loggerFactory, IConfiguration config)
	{
		var connStr = config.GetConnectionString("Database");

		var serverVersion = ServerVersion.AutoDetect(connStr);
		
		var optionsBuilder = new DbContextOptionsBuilder<KynarethDbContext>()
			.UseLoggerFactory(loggerFactory)
			.UseMySql(connStr, serverVersion, x => x.EnableRetryOnFailure())
			.UseLazyLoadingProxies();

		return new KynarethDbContext(optionsBuilder.Options);
	}

	public KynarethDbContext CreateDbContext(string[] args = null) {
		var config = new ConfigurationBuilder ()
			.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
			.AddJsonFile("appsettings.json")
			.Build();

		return CreateDbContext(null, config);
	}
}