using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.DependencyInjection;

using Monitoring.Infrastructure.Abstractions;
using Monitoring.Infrastructure.Data;
using Monitoring.Infrastructure.Services;

namespace Monitoring.AppHost.DbContextFactories;

internal sealed class MonitoringDbContextFactory : IDesignTimeDbContextFactory<DataContext>
{
	public DataContext CreateDbContext(string[] args)
	{
		var builder = DistributedApplication.CreateBuilder(args);

		var pg = builder
			.AddPostgres(Constants.DatabaseModuleName)
			.WithDataVolume(Constants.DatabaseVolumeName);
		var db = pg.AddDatabase(Constants.MonitoringDatabaseName);

		var options = new DbContextOptionsBuilder<DataContext>()
			.UseNpgsql(Constants.MonitoringDatabaseName) // ключ ресурса Aspire
			.Options;

		builder.Services.AddInfrastructureServices();

		var app = builder.Build();
		var passwordService = app.Services.GetRequiredService<IPasswordService>();

		return new DataContext(options, passwordService);
	}
}
