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

namespace Application.Features.Api.Crew.DeleteWidgetConfig;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        var config = await db.CrewWidgetConfig
            .FirstOrDefaultAsync(c => c.ID == request.ID && c.ChannelID == request.ChannelID, ct);

        if (config is null)
        {
            throw new KeyNotFoundException("설정을 찾을 수 없습니다.");
        }

        db.CrewWidgetConfig.Remove(config);
        await db.SaveChangesAsync(ct);
    }
}