<?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\UpdatesPackingList;
use App\Classes\ValueObjects\Constants\HttpStatus;
use App\Traits\ChecksIfPackingListIsComplete;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackingListLogic
{

    use ChecksIfPackingListIsComplete;

    /** @var UpdatesPackingList */
    private $updatesPackingList;

    /**
     * UpdatePackingListLogic constructor.
     * @param UpdatesPackingList $updatesPackingList
     */
    public function __construct(UpdatesPackingList $updatesPackingList)
    {
        $this->updatesPackingList = $updatesPackingList;
    }


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

        try {

            $packingListObject = new PackingListObject($request->input('reference_no'), $request->input('packages'));

            $confirmed = $this->checkPackingListIsComplete($packingListObject->getPackages());

            $this->updatesPackingList->execute($orderId, $packingListObject, $confirmed);

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

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



    }
}