using Quartz;
using OTSSignsOrchestrator.Server.Health.Checks;

namespace OTSSignsOrchestrator.Server.Health;

/// <summary>
/// Quartz job that runs the <see cref="AuthentikGlobalHealthCheck"/> every 2 minutes
/// on a separate schedule from the per-instance health checks.
/// </summary>
[DisallowConcurrentExecution]
public sealed class AuthentikGlobalHealthJob : IJob
{
    private readonly AuthentikGlobalHealthCheck _check;
    private readonly ILogger<AuthentikGlobalHealthJob> _logger;

    public AuthentikGlobalHealthJob(
        AuthentikGlobalHealthCheck check,
        ILogger<AuthentikGlobalHealthJob> logger)
    {
        _check = check;
        _logger = logger;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        try
        {
            var result = await _check.RunGlobalAsync(context.CancellationToken);
            _logger.LogInformation("Authentik global health: {Status} — {Message}",
                result.Status, result.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Authentik global health job failed");
        }
    }
}
