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

namespace Application.Features.Admin.MemberGrade.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.MemberGrade.Where(c => request.IDs.Contains(c.ID)).Select(c => c.Image).ToListAsync(ct);

            foreach (var img in images)
            {
                if (img != null)
                {
                    fileStorage.DeleteByUrl(img);
                }
            }

            await db.MemberGrade.Where(c => request.IDs.Contains(c.ID)).ExecuteDeleteAsync(ct);
        }
    }
}