<?php

namespace App\Classes\Modules\Documents\Processors;

use App\Classes\Modules\Documents\Services\ListsDocuments;
use App\Classes\Modules\Jobs\Processors\UpdateJobResultProcessor;
use App\Classes\General\Helper;
use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject;
use App\Http\Resources\ListDocumentJobResource;

class ListDocumentsJobProcessor
{

    /** @var ListsDocuments */
    private $listsDocuments;

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

    /**
     * ListDocumentsJobProcessor constructor.
     * @param ListsDocuments $listsDocuments
     * @param UpdateJobResultProcessor $updateJobResultProcessor
     */
    public function __construct(ListsDocuments $listsDocuments, UpdateJobResultProcessor $updateJobResultProcessor)
    {
        $this->listsDocuments = $listsDocuments;
        $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->listsDocuments->execute($this->listsDocuments->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page']]);
        foreach ($query->items() as &$item) {
            $item['userInfo'] = $listGenericJobObject->getUserInfo();
        }
        $resultCurrent = Helper::collectionResponse(ListDocumentJobResource::collection($query));
        $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
    }
}

