using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Domain.Entities.Donations;

namespace Application.Features.Api.DonationRemote.ResendAlert;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        var alert = await db.DonationAlert.FindAsync([request.AlertID], ct);
        if (alert is null)
        {
            throw new KeyNotFoundException("알림을 찾을 수 없습니다.");
        }

        alert.Resend();

        var attempt = DonationAlertAttempt.Create(alert.DonationID, alert.ID);

        await db.DonationAlertAttempt.AddAsync(attempt, ct);
        await db.SaveChangesAsync(ct);
    }
}