<?php

namespace App\Classes\Modules\PackingLists\Processors;

use App\Classes\Modules\Orders\Services\FetchesDataFromVTPortal;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\PackingLists\Services\ListsPackingLists;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackageType;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Models\PackingList;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class FetchPackingListProcessor
{

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

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

    /**
     * @return array
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute($params){

        $packingLists = PackingList::where('type', '=', PackingListType::SHIPPING_PACKING_LIST)
            ->whereIn('status', [ApprovalStatus::PENDING_VERIFICATION, ApprovalStatus::SUSPENDED])
            ->whereDoesntHave('containers', function($query){
                $query->where('status', '=', ApprovalStatus::COMPLETED);
            })->orderBy('created_at','desc')->where('owner_id',$params['orderId'])->get();

            //dd($packingLists);
        $packingListsArray=[];
        foreach($packingLists as $packingList){

            $address = $packingList->owner->addresses()->first();
            $packingList->fulladdress = $address->street_one.', '.$address->street_two.', '.$address->district->name.', '.$address->post_code.' '.$address->state->name.', '.$address->country->name;

            $container = $packingList->containers()->first();
            $packingList->container_reference = $container->reference;
            $packingList->container_number = $container->container_number;

            $packingListsArray[]=$packingList;

        }

        return $packingListsArray;

    }


}
