<?php

namespace App\Classes\Modules\PackingLists\Processors;

use App\Classes\Modules\Orders\Services\FetchesDataFromVTPortal;
use App\Classes\Modules\Schedules\DataTransferObjects\ScheduleObject;
use App\Classes\Modules\Schedules\Services\CreatesSchedule;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Classes\Modules\Transports\Services\CreatesTransport;
use App\Classes\Modules\Unity\Services\UpdatesContractObligation;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\TransportType;
use App\Models\PackingList;
use App\Models\Transport;
use App\Models\Container;
use Carbon\Carbon;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Log;

class FetchInboundCustomClearedProcessor
{

    /** @var FetchesDataFromVTPortal */
    private $fetchesDataFRomVTPortal;

    /** @var CreatesTransport */
    private $createsTransport;

    /** @var CreatesSchedule */
    private $createsSchedule;

    /** @var UpdatesContractObligation */
    private $updatesContractObligations;

    /**
     * FetchDeliveryListFromVTPortalProcessor constructor.
     * @param FetchesDataFromVTPortal $fetchesDataFRomVTPortal
     * @param CreatesTransport $createsTransport
     * @param CreatesSchedule $createsSchedule
     * @param UpdatesContractObligation $updatesContractObligations
     */
    public function __construct(FetchesDataFromVTPortal $fetchesDataFRomVTPortal, CreatesTransport $createsTransport, CreatesSchedule $createsSchedule, UpdatesContractObligation $updatesContractObligations)
    {
        $this->fetchesDataFRomVTPortal = $fetchesDataFRomVTPortal;
        $this->createsTransport = $createsTransport;
        $this->createsSchedule = $createsSchedule;
        $this->updatesContractObligations = $updatesContractObligations;
    }


    /**
     * @return array
     */
    public function execute(){

        $packingLists = PackingList::query()->with(['steps' => function ($query) {
            $query->where('steps.status', '<>', 3);
        }])
        ->where('type', PackingListType::SHIPPING_PACKING_LIST)
        ->whereHas('containers', function($query) {
            return $query->whereHas('transports', function($query){
                return $query->where('type', '=', TransportType::SEA)->where('status', '=', ApprovalStatus::COMPLETED)->whereHas('schedules', function($query){
                    return $query->where('etd', '<', Carbon::now());
                });
            });
        })->whereDoesntHave('transports', function($query){
            return $query->where('type', '=', TransportType::LAND)->where('status', '=', ApprovalStatus::APPROVED)->whereHas('schedules', function($query){
                return $query->where('etd', '<', Carbon::now());
            });
        })->get();

        return $packingLists ;

    }


}
