using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using SharedKernel.Storage;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Admin.Forum.CommentFile.Delete;

public sealed class Handler(IAppDbContext db, IFileStorage fileStorage) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        if (request.IDs is null || request.IDs.Length == 0)
        {
            return;
        }

        var files = await db.CommentFile.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);

        foreach (var file in files)
        {
            fileStorage.DeleteByUrl(file.Url);
        }

        db.CommentFile.RemoveRange(files);

        await db.SaveChangesAsync(ct);
    }
}