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

namespace Application.Features.Api.Crew.UpdateMember;

internal sealed class Handler(IAppDbContext db) : ICommandHandler<Command>
{
    public async Task Handle(Command request, CancellationToken ct)
    {
        var member = await db.CrewMember
            .FirstOrDefaultAsync(m => m.ID == request.CrewMemberID, ct);

        if (member is null)
        {
            throw new KeyNotFoundException("크루원을 찾을 수 없습니다.");
        }

        member.Update(request.Nickname, request.Role, request.SortOrder, request.IsActive);
        await db.SaveChangesAsync(ct);
    }
}