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

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

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

        await db.SaveChangesAsync(ct);
    }
}