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

namespace Application.Features.Admin.Forum.PostReport.UpdateStatus;

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 reports = await db.PostReport.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);

        foreach (var report in reports)
        {
            report.Status = request.Status;

            if (request.Memo is not null)
            {
                report.Memo = request.Memo;
            }

            report.UpdatedAt = DateTime.UtcNow;
        }

        await db.SaveChangesAsync(ct);
    }
}