<?php

namespace App\Classes\Modules\Bookings\Processors;

use App\Classes\Modules\Bookings\Services\ListsBookings;
use App\Classes\Modules\Jobs\Processors\UpdateJobResultProcessor;
use App\Classes\General\Helper;
use App\Classes\Modules\Jobs\DataTransferObjects\ListGenericJobObject;
use App\Http\Resources\ListBookingJobResource;

class ListBookingsJobProcessor
{

    /** @var ListsBookings */
    private $listsBookings;

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

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