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

namespace Application.Features.Admin.Forum.PostImage.ToggleDisable;

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

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

        foreach (var image in images)
        {
            image.IsDisabled = request.IsDisabled;
            image.DisabledAt = request.IsDisabled ? DateTime.UtcNow : null;
        }

        await db.SaveChangesAsync(ct);
    }
}