<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Orders\Processors\UpdateDoFromYDPortalProcessor;
use App\Classes\Modules\PackingLists\Services\UpdatesPackingList;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\PackingLists\Services\UpdatesPackingListStatus;
use App\Classes\Modules\PackingLists\Standards\Rules\CanUpdatePackingList;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Http\Resources\PackingListResource;
use App\Models\PackingList;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdatePackingListStatusLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated PackingList Status',
            'message' => 'You have successfully updated the PackingList status'
        ];
    }


    /** @var FetchesPackingList */
    private $fetchesPackingList;

    /** @var UpdatesPackingListStatus */
    private $updatesPackingListStatus;

    /** @var UpdateDoFromVTPortalProcessor */
    private $updateDoFromVTPortalProcessor;

    /** @var UpdateDoFromYDPortalProcessor */
    private $updateDoFromYDPortalProcessor ;

    /**
     * UpdatePackingListStatusLogic constructor.
     * @param FetchesPackingList $fetchesPackingList
     * @param UpdatesPackingListStatus $updatesPackingListStatus
     * @param UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor
     * @param UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor
     */
    public function __construct(FetchesPackingList $fetchesPackingList, UpdatesPackingListStatus $updatesPackingListStatus, UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor)
    {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->updatesPackingListStatus = $updatesPackingListStatus;
        $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
        $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\InternalServerErrorException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function logic(Request $request) : JsonResponse
    {
        $packingList = $this->fetchesPackingList->execute(['id' => $request->route('id')]);

        /** @var PackingList $packingList */
        $packingList = $this->updatesPackingListStatus->execute($packingList, $request->route('status'));


//        $appointee = $address->owner->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id;

        $this->updateDoFromVTPortalProcessor->execute($packingList);
        $this->updateDoFromYDPortalProcessor->execute($packingList);

        return $this->resourceResponse(new PackingListResource($packingList));
    }

}
