<?php

namespace App\Classes\Modules\PackingLists\Processors;


use App\Classes\Modules\PackingLists\Services\ListsPackingLists;
use App\Classes\Modules\Jobs\Processors\UpdateJobResultProcessor;
use App\Classes\General\Helper;
use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject;
use App\Http\Resources\ListPackingListJobResource;

class ListPackingListsJobProcessor
{

    /** @var ListsPackingLists */
    private $listsPackingLists;

    /** @var UpdateJobResultProcessor */
    private $updateJobResultProcessor;

    /**
     * ListPackingListsJobProcessor constructor.
     * @param ListsPackingLists $listsPackingLists
     * @param UpdateJobResultProcessor $updateJobResultProcessor
     */
    public function __construct(ListsPackingLists $listsPackingLists, UpdateJobResultProcessor $updateJobResultProcessor)
    {
        $this->listsPackingLists = $listsPackingLists;
        $this->updateJobResultProcessor = $updateJobResultProcessor;
    }

    /**
     * @param ListGenericJobObject $listGenericJobObject
     * @return void
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\JobResourceNotFoundException
     */
    public function execute(ListGenericJobObject $listGenericJobObject) {

        // $query = $this->listsPackingLists->execute($this->listsPackingLists->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page'], 'with_containers_packages' => true]);

        $query = $this->listsPackingLists->execute(array_merge($this->listsPackingLists->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page'], 'with_containers_packages' => true]));

        foreach ($query->items() as &$item) {
            $item['userInfo'] = $listGenericJobObject->getUserInfo();
        }
        $resultCurrent = Helper::collectionResponse(ListPackingListJobResource::collection($query));
        $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
    }
}
