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

namespace Application.Features.Admin.Faq.Item.Delete
{
    public sealed class Handler(IAppDbContext db, IEditorImageService editorImage, ICacheService cache) : ICommandHandler<Command>
    {
        public async Task Handle(Command request, CancellationToken ct)
        {
            if (request.IDs is null || request.IDs.Length == 0)
            {
                return;
            }

            var answers = await db.FaqItem.Where(c => request.IDs.Contains(c.ID)).Select(c => c.Answer).ToListAsync(ct);

            foreach (var answer in answers)
            {
                await editorImage.CleanupByContentAsync(answer, ct);
            }

            await db.FaqItem.Where(c => request.IDs.Contains(c.ID)).ExecuteDeleteAsync(ct);
            await cache.RemoveByPrefixAsync("faq:item:", ct);
        }
    }
}