<?php

namespace App\Classes\Modules\ControllersLogic\Orders\Attachments;

use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Orders\DataTransferObjects\PackingListObject;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesPackages;
use App\Classes\Modules\Orders\Services\Attachments\UpdatesPackingList;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Traits\ChecksIfPackingListIsComplete;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackagesLogic
{

    use ChecksIfPackingListIsComplete;

    /** @var UpdatesPackages */
    private $updatesPackages;

    /**
     * UpdatePackagesLogic constructor.
     * @param UpdatesPackages $updatesPackages
     */
    public function __construct(UpdatesPackages $updatesPackages)
    {
        $this->updatesPackages = $updatesPackages;
    }


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

        try {

            $this->updatesPackages->execute($orderId, $request->input('packages'));

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

        } catch (\Exception $exception){
            throw new InternalServerErrorException("Failed to update order's packages because {$exception->getMessage()}");
        }



    }
}