using Application.Abstractions.Data;
using Application.Abstractions.Messaging;
using Domain.Entities.Donations.ValueObject;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Api.DonationRemote.SkipCurrent;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        var playing = await db.DonationAlert.Where(a => a.Donation!.ChannelID == request.ChannelID && a.Status == AlertStatus.Playing).FirstOrDefaultAsync(ct);
        if (playing is null)
        {
            throw new KeyNotFoundException("재생 중인 알림이 없습니다.");
        }

        playing.MarkSkipped();
        await db.SaveChangesAsync(ct);
    }
}