<?php

namespace App\Classes\Modules\ControllersLogic\Orders;

use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\Services\UpdatesOrderWarehouse;
use App\Classes\ValueObjects\Constants\HttpStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateWarehouseLogic
{

    /** @var UpdatesOrderWarehouse */
    private $updatesOrderWarehouse;

    /**
     * UpdateWarehouseLogic constructor.
     * @param UpdatesOrderWarehouse $updatesOrderWarehouse
     */
    public function __construct(UpdatesOrderWarehouse $updatesOrderWarehouse)
    {
        $this->updatesOrderWarehouse = $updatesOrderWarehouse;
    }

    /**
     * @param String $orderId
     * @param Request $request
     * @return JsonResponse
     * @throws InternalServerErrorException
     */
    public function execute(String $orderId, Request $request){

        try {


            $this->updatesOrderWarehouse->execute($orderId, $request->input('warehouse_id'), 0);
            $this->updatesOrderWarehouse->execute($orderId, $request->input('warehouse_id'), 1);

            return new JsonResponse(null, HttpStatus::REQUEST_ACCEPTED);

        } catch (\Exception $exception){
            throw new InternalServerErrorException("Failed to set warehouse because {$exception->getMessage()}");
        }



    }
}