using Meezi.API.Services;

namespace Meezi.API.Jobs;

public class BranchPermanentDeleteJob
{
    private readonly IServiceScopeFactory _scopeFactory;
    private readonly ILogger<BranchPermanentDeleteJob> _logger;

    public BranchPermanentDeleteJob(
        IServiceScopeFactory scopeFactory,
        ILogger<BranchPermanentDeleteJob> logger)
    {
        _scopeFactory = scopeFactory;
        _logger = logger;
    }

    public async Task ExecuteAsync()
    {
        await using var scope = _scopeFactory.CreateAsyncScope();
        var lifecycle = scope.ServiceProvider.GetRequiredService<IBranchLifecycleService>();
        var purged = await lifecycle.PurgeExpiredDeletionsAsync();
        if (purged > 0)
            _logger.LogInformation("Permanently deleted {Count} expired branches", purged);
    }
}
