using Havit.Data.EntityFrameworkCore;
using Havit.Data.Patterns.DataSeeds;
using Havit.Data.Patterns.DataSeeds.Profiles;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace DiplomovaPrace.Web.Server.Infrastructure.Tools
{
	public static class DatabaseMigration
	{
		public static void UpgradeDatabaseSchemaAndData(this IApplicationBuilder app)
		{
			using (IServiceScope serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
			{
				IDbContext context = serviceScope.ServiceProvider.GetService<IDbContext>();
				context.Database.Migrate();

				IDataSeedRunner dataSeedRunner = serviceScope.ServiceProvider.GetService<IDataSeedRunner>();
				dataSeedRunner.SeedData<DefaultProfile>();
			}
		}
	}
}
