<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Companies\Services\FetchesCompanyModule;
use App\Classes\Modules\Orders\DataTransferObjects\OrderRoleObject;
use App\Classes\Modules\Orders\Services\CreatesOrderRole;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\WarehouseReferences;
use App\Models\CompanyModule;
use App\Models\Order;
use Illuminate\Http\Request;

class CreateOrderRolesProcessor
{


    /** @var FetchesCompanyModule */
    private $fetchesCompanyModule;

    /** @var CreatesOrderRole */
    private $createsOrderRole;

    /** @var FetchesCompany */
    private $fetchesCompany;

    /**
     * CreateOrderRolesProcessor constructor.
     * @param FetchesCompanyModule $fetchesCompanyModule
     * @param CreatesOrderRole $createsOrderRole
     * @param FetchesCompany $fetchesCompany
     */
    public function __construct(FetchesCompanyModule $fetchesCompanyModule, CreatesOrderRole $createsOrderRole, FetchesCompany $fetchesCompany)
    {
        $this->fetchesCompanyModule = $fetchesCompanyModule;
        $this->createsOrderRole = $createsOrderRole;
        $this->fetchesCompany = $fetchesCompany;
    }


    /**
     * @param CompanyModule $originWarehouse
     * @param Order $order
     * @return bool
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(CompanyModule $originWarehouse, Order $order){

        $address = $order->addresses()->first();

        $importer = $order->companyModule;

        /** @var CompanyModule $destinationWarehouse */
        $destinationWarehouse = $this->fetchesCompanyModule->execute(['reference' => $originWarehouse->reference === WarehouseReferences::YD_GUANG_ZHOU ? WarehouseReferences::YD_DESTINATION_WAREHOUSE : WarehouseReferences::VT_DESTINATION_WAREHOUSE[$address->state_id]]);

        /** @var CompanyModule $freightForwarder */
        $freightForwarder = $originWarehouse->company->companyModules()->freightForwarders()->first();

        /** @var CompanyModule $supervisor */
        $supervisor = $this->fetchesCompany->execute(['reference' => 'CIEF'])->companyModules()->freightForwarders()->first();

        $roleObject = new OrderRoleObject($order, $supervisor, OrderRoleTypes::SUPERVISOR);
        $this->createsOrderRole->execute($roleObject);

        $roleObject = new OrderRoleObject($order, $importer, OrderRoleTypes::IMPORTER);
        $this->createsOrderRole->execute($roleObject);

        $roleObject = new OrderRoleObject($order, $originWarehouse, OrderRoleTypes::ORIGIN_WAREHOUSE);
        $this->createsOrderRole->execute($roleObject);

        $roleObject = new OrderRoleObject($order, $freightForwarder, OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER);
        $this->createsOrderRole->execute($roleObject);

        $roleObject = new OrderRoleObject($order, $freightForwarder, OrderRoleTypes::DESTINATION_FREIGHT_FORWARDER);
        $this->createsOrderRole->execute($roleObject);

        $roleObject = new OrderRoleObject($order, $destinationWarehouse, OrderRoleTypes::DESTINATION_WAREHOUSE);
        $this->createsOrderRole->execute($roleObject);


        $roleObject = new OrderRoleObject($order, $freightForwarder, OrderRoleTypes::LAST_MILE_DELIVERY_DRIVER);
        $this->createsOrderRole->execute($roleObject);

        return true;
    }
}
