using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Jellyfin.Server.HealthChecks;
/// 
/// Implementation of the  for a .
/// 
/// The type of database context.
public class DbContextFactoryHealthCheck : IHealthCheck
    where TContext : DbContext
{
    private readonly IDbContextFactory _dbContextFactory;
    /// 
    /// Initializes a new instance of the  class.
    /// 
    /// Instance of the  interface.
    public DbContextFactoryHealthCheck(IDbContextFactory contextFactory)
    {
        _dbContextFactory = contextFactory;
    }
    /// 
    public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        ArgumentNullException.ThrowIfNull(context);
        var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
        await using (dbContext.ConfigureAwait(false))
        {
            if (await dbContext.Database.CanConnectAsync(cancellationToken).ConfigureAwait(false))
            {
                return HealthCheckResult.Healthy();
            }
        }
        return HealthCheckResult.Unhealthy();
    }
}