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

namespace OTSSignsOrchestrator.Core.Data;

/// <summary>
/// Design-time factory for EF Core migrations tooling.
/// </summary>
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<XiboContext>
{
    public XiboContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<XiboContext>();
        optionsBuilder.UseSqlite("Data Source=design-time.db");

        // Set up a temporary DataProtection provider for design-time use
        var services = new ServiceCollection();
        services.AddDataProtection()
            .SetApplicationName("OTSSignsOrchestrator");
        var sp = services.BuildServiceProvider();
        var dpProvider = sp.GetRequiredService<IDataProtectionProvider>();

        return new XiboContext(optionsBuilder.Options, dpProvider);
    }
}
