using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations;
using System;
using System.Linq;
using System.Reflection;

namespace Mirea.Api.DataAccess.Persistence.Common;

public static class ConfigurationResolver
{
    public static Type GetConfigurationType<TEntity>(DatabaseProvider provider)
        where TEntity : class
    {
        var entityType = typeof(TEntity);
        var providerNamespace = typeof(Mark).Namespace + "." + Enum.GetName(provider);

        var assembly = Assembly.GetExecutingAssembly();
        var configurationType = assembly.GetTypes()
            .FirstOrDefault(t => t.Namespace != null && t.Namespace.StartsWith(providerNamespace) && t.Name == $"{entityType.Name}Configuration");

        return configurationType ??
               throw new InvalidOperationException($"Configuration type not found for entity {entityType.Name} and provider {provider}.");
    }
}