<?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\Middleware\CheckForStorageInvoiceByPackingLists;
use App\Http\Resources\ListPackingListJobResource;
use App\Http\Resources\ListPackingListDetailsJobResource;
use App\Http\Resources\ListPackingListJobWithStorageResource;
use Illuminate\Support\Facades\Log;

class ListPackingListsJobProcessor
{

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

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

    /** @var CheckForStorageInvoiceByPackingLists */
    private $checkForStorageInvoiceByPackingLists;

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

    /**
     * @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($this->listsPackingLists->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page']]);
        // $query = $this->listsPackingLists->execute(array_merge($this->listsPackingLists->deserializeFilters($listGenericJobObject->getPayload()['filters']), ['page' => $listGenericJobObject->getPayload()['page'], 'with_containers_packages' => true]));

        $filtersArray = json_decode($listGenericJobObject->getPayload()['filters'], true);

        $storages = [];
        if(isset($filtersArray['check_for_storage_invoice']))
        {
            $myRequest = new \Illuminate\Http\Request();
            $myRequest->setMethod('POST');
            $myRequest->request->add($listGenericJobObject->getPayload());
            $next = function ($request) {
                return $request;
            };

            $response = $this->checkForStorageInvoiceByPackingLists->handle($myRequest, $next);
            $storages = $response->input('storages');
        }

        foreach ($query->items() as &$item) {
            $item['userInfo'] = $listGenericJobObject->getUserInfo();
            $item['storages'] = $storages;
        }

        if(isset($filtersArray['does_not_have_transaction_type'])){ //For Payment and Billing page > Pending Invoice tab
            $resultCurrent = Helper::collectionResponse(ListPackingListDetailsJobResource::collection($query));
            $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
        }
        else{
            if($storages){ //For Payment and Billing page > Pending Payment tab, Paid Invoice tab
                $resultCurrent = Helper::collectionResponse(ListPackingListJobWithStorageResource::collection($query));
                $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
            }
            else{
                $resultCurrent = Helper::collectionResponse(ListPackingListJobResource::collection($query));
                $this->updateJobResultProcessor->execute($listGenericJobObject, $resultCurrent);
            }
        }
    }
}
