<?php

namespace App\Classes\Modules\PackingLists\Processors;


use App\Classes\General\Interfaces\Packable;
use App\Classes\Modules\PerfexCRM\Processors\PackingListToPerfexCRMProcessor;
use App\Classes\Modules\PackingLists\DataTransferObjects\PackingListObject;
use App\Classes\Modules\PackingLists\Services\CreatesPackingList;
use App\Classes\Modules\PackingLists\Standards\Rules\CanCreatePackingList;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\PerfexCRMTasksYDStages;
use App\Classes\ValueObjects\Constants\WarehouseReferences;
use App\Models\Order;
use App\Models\PackingList;
use function PHPUnit\Framework\isInstanceOf;

class CreatePackingListProcessor
{


    /** @var CanCreatePackingList */
    private $canCreatePackingList;

    /** @var CreatesPackingList */
    private $createsPackingList;

    /** @var PackingListToPerfexCRMProcessor */
    private $packingListToPerfexCRMProcessor;

    /**
     * CreatePackingListProcessor constructor.
     * @param CanCreatePackingList $canCreatePackingList
     * @param CreatesPackingList $createsPackingList
     * @param PackingListToPerfexCRMProcessor $packingListToPerfexCRMProcessor
     */
    public function __construct(CanCreatePackingList $canCreatePackingList, CreatesPackingList $createsPackingList, PackingListToPerfexCRMProcessor $packingListToPerfexCRMProcessor)
    {
        $this->canCreatePackingList = $canCreatePackingList;
        $this->createsPackingList = $createsPackingList;
        $this->packingListToPerfexCRMProcessor = $packingListToPerfexCRMProcessor;
    }

    /**
     * @param PackingListObject $object
     * @param Packable $packable
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(PackingListObject $object, Packable $packable){

        $this->canCreatePackingList->passes($object);
        /** @var PackingList $packingList */
        $packingList = $this->createsPackingList->execute($object, $packable);

        $appointee_id = $packingList->owner_id == 1 ? 2307 : $packingList->owner->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id;

        $type = $object->getType() === PackingListType::SHIPPING_PACKING_LIST ? PackingListType::SHIPPING_PACKING_LIST_REPLICA : PackingListType::WAREHOUSE_RECEIVE_LIST_REPLICA;

        /** create packing list replica */
        $object = new PackingListObject($object->getReference().'_01', $appointee_id, $type, ApprovalStatus::PENDING_SUBMISSION);
        $this->createsPackingList->execute($object, $packingList);

        if(config('perfexcrm.is_enabled') == 'true'){
            $this->packingListToPerfexCRMProcessor->execute($packingList, PerfexCRMTasksYDStages::DEFAULT);
        }

        return $packingList;
    }

}
