namespace NetinaShop.Core.EntityServices.OrderHandlers;

public class ConfirmOrderStepCommandHandler : IRequestHandler<ConfirmOrderStepCommand,bool>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public ConfirmOrderStepCommandHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<bool> Handle(ConfirmOrderStepCommand request, CancellationToken cancellationToken)
    {
        var order = await _repositoryWrapper.SetRepository<Order>()
            .TableNoTracking
            .FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
        if (order == null)
            throw new AppException("Order not found", ApiResultStatusCode.NotFound);

        order.SetOrderStatus(request.NextOrderStatus);

        _repositoryWrapper.SetRepository<Order>()
            .Update(order);
        await _repositoryWrapper.SaveChangesAsync(cancellationToken);

        return true;
    }
}