using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

using Ablera.Serdica.DBModels.Oidc;

namespace Ablera.Serdica.DBModels.Oidc.Migrations;

public class OidcDbContextFactory : IDesignTimeDbContextFactory<OidcDbContext>
{
    public OidcDbContext CreateDbContext(string[] args)
    {
        // Use the current directory as base path (which is typically the startup project’s folder)
        var basePath = Directory.GetCurrentDirectory();

        // Build configuration from appsettings.json in the startup folder
        var configuration = new ConfigurationBuilder()
            .SetBasePath(basePath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .Build();

        var connectionString = configuration.GetConnectionString("DefaultConnection");

        var optionsBuilder = new DbContextOptionsBuilder<OidcDbContext>();
        var migrationsAssembly = typeof(Ablera.Serdica.DBModels.Oidc.Migrations.OidcDbContextFactory).Assembly.GetName().Name;
        System.Console.WriteLine($"Using migration assembly name: {migrationsAssembly}");
        optionsBuilder.UseOracle(connectionString, b =>
            b.MigrationsAssembly(migrationsAssembly))
            .UseOpenIddict();

        return new OidcDbContext(optionsBuilder.Options);
    }
}
