using MediatR;
using FnbEngine.Domain.AggregatesModel.ReservationAggregate;

namespace FnbEngine.API.Application.Commands;

public class CreateReservationCommandHandler : IRequestHandler<CreateReservationCommand, CreateReservationResult>
{
    private readonly IReservationRepository _reservationRepository;

    public CreateReservationCommandHandler(IReservationRepository reservationRepository)
    {
        _reservationRepository = reservationRepository ?? throw new ArgumentNullException(nameof(reservationRepository));
    }

    public async Task<CreateReservationResult> Handle(CreateReservationCommand request, CancellationToken cancellationToken)
    {
        var reservation = new Reservation(
            request.ShopId,
            request.GuestName,
            request.PartySize,
            request.ReservationTime,
            request.Phone,
            request.TableId,
            request.Note);

        await _reservationRepository.AddAsync(reservation, cancellationToken);
        await _reservationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        return new CreateReservationResult(reservation.Id);
    }
}
