namespace BBM.ChatGLM.EntityFrameworkCore
{
    public class EntityFrameworkCoreChatGLMDbSchemaMigrator
        : IChatGLMDbSchemaMigrator, ITransientDependency
    {
        private readonly IServiceProvider _serviceProvider;

        public EntityFrameworkCoreChatGLMDbSchemaMigrator(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public async Task MigrateAsync()
        {
            /* We intentionally resolving the ChatGLMMigrationsDbContext
             * from IServiceProvider (instead of directly injecting it)
             * to properly get the connection string of the current tenant in the
             * current scope.
             */

            await _serviceProvider
                .GetRequiredService<ChatGLMDbContext>()
                .Database
                .MigrateAsync();
        }
    }
}