using Quartz;
using SecureSharing.Business.Interfaces;

namespace SecureSharing.Quartz;

public sealed class MessageDeletionJob : IJob
{
    private readonly string _basePath;
    private readonly string _basePathTmp;
    private readonly IMessageService _messageService;

    public MessageDeletionJob(IMessageService messageService, IWebHostEnvironment webHostEnvironment)
    {
        _messageService = messageService;
        _basePath = Path.Combine(webHostEnvironment.WebRootPath.Split('/')[0], "files");
        _basePathTmp = Path.Combine(webHostEnvironment.WebRootPath.Split('/')[0], "filestmp");
    }

    public async Task Execute(IJobExecutionContext context)
    {
        try
        {
            var foldersToRemove = await _messageService.DeleteExpiredMessages();

            foreach (var path in foldersToRemove.Select(folder => Path.Combine(_basePath, folder)))
                Directory.Delete(path, true);

            Directory.Delete(_basePathTmp, true);
        }
        catch (Exception ex)
        {
            /*Log*/
        }
    }
}