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

namespace Application.Features.Admin.Forum.CommentImage.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 images = await db.CommentImage.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);

        foreach (var image in images)
        {
            fileStorage.DeleteByUrl(image.Url);
        }

        db.CommentImage.RemoveRange(images);

        await db.SaveChangesAsync(ct);
    }
}