namespace MyOffice.Migrations.Postgres;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using MyOffice.DbContext;
using MyOffice.Shared;

public class AppDbContextFactoryPostgres : IDesignTimeDbContextFactory<AppDbContext>
{
	public AppDbContext CreateDbContext(string[]? args)
	{
		var builder = new DbContextOptionsBuilder<AppDbContext>();

		var sharedConfiguration = SharedConfiguration.Build();
		var connectionString =
			args?.FirstOrDefault()
			?? sharedConfiguration.GetConnectionString("npgsql");

		builder.UseNpgsql(connectionString, b => b.MigrationsAssembly("MyOffice.Migrations.Postgres"));

		var db = new AppDbContext(AppDbContextProvidersEnum.npgsql, builder.Options);
		db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;

		return db;
	}
}