using IWS5.Importer.Abstractions.Interfaces;

namespace IWS5.Importer.Worker.Workers;

public class ArchiveCleanupWorker(IServiceProvider serviceProvider, IGeneralImportSettingsProvider settingsProvider) : BackgroundService
{
    private readonly IServiceProvider _serviceProvider = serviceProvider;
    private readonly string _archivePath = settingsProvider.Settings.CopyLocation;
    private readonly int _cleanupIntervalHours = settingsProvider.Settings.CleanupIntervalHours;

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            using var scope = _serviceProvider.CreateScope();
            var fileCleaner = scope.ServiceProvider.GetRequiredService<IFileCleaner>();

            await fileCleaner.CleanupAsync(_archivePath, stoppingToken);
            await Task.Delay(TimeSpan.FromHours(_cleanupIntervalHours), stoppingToken);
        }
    }
}
