namespace MealPlan.Api.Services;

/// <summary>
/// Periodically purges expired and revoked refresh tokens from the in-memory store.
/// </summary>
public class RefreshTokenCleanupService : BackgroundService
{
    private readonly IRefreshTokenStore _store;
    private readonly ILogger<RefreshTokenCleanupService> _logger;

    public RefreshTokenCleanupService(
        IRefreshTokenStore store,
        ILogger<RefreshTokenCleanupService> logger)
    {
        _store = store;
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            try
            {
                _store.RemoveExpired();
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, "Refresh token cleanup failed");
            }

            await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
        }
    }
}
