<?php

namespace App\Classes\Modules\PackingLists\Processors;


use App\Classes\Modules\PackingLists\DataTransferObjects\PackageObject;
use App\Classes\Modules\PackingLists\Services\Packages\CreatesPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\CanCreatePackage;
use App\Classes\ValueObjects\Constants\WarehouseReferences;
use App\Models\Order;
use App\Models\PackingList;

class CreatePackageProcessor
{


    /** @var CanCreatePackage */
    private $canCreatePackage;

    /** @var CreatesPackage */
    private $createsPackage;

    /**
     * CreatePackageProcessor constructor.
     * @param CanCreatePackage $canCreatePackage
     * @param CreatesPackage $createsPackage
     */
    public function __construct(CanCreatePackage $canCreatePackage, CreatesPackage $createsPackage)
    {
        $this->canCreatePackage = $canCreatePackage;
        $this->createsPackage = $createsPackage;
    }


    /**
     * @param PackageObject $object
     * @param PackingList $packingList
     * @return void
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(PackageObject $object, PackingList $packingList){

        $this->canCreatePackage->passes($object);

        $this->createsPackage->execute($object, $packingList);

        /** @var PackingList $replica */
        $replica = $packingList->packingLists()->first();

        if($replica) {
            $modificationValue = 1;

            if($replica->owner->owner instanceof Order){
                $modificationValue = in_array( $replica->owner->owner->companyModule->id, WarehouseReferences::REPLICA_WHITE_LIST) ? 0 : 1;
            }

            $packageObject = new PackageObject($object->getType(), $object->getDescription(), $object->getWidth() + $modificationValue, $object->getHeight() + $modificationValue, $object->getLength() + $modificationValue, $object->getWeight(), $object->getQuantity(), $object->getStatus());
            $package = $this->createsPackage->execute($packageObject, $replica);
        }

        return ;

    }

}