<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\General\Interfaces\Addressable;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Companies\Services\FetchesCompany;
use App\Classes\Modules\Orders\DataTransferObjects\OrderObject;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Classes\Modules\Remarks\DataTransferObjects\RemarkObject;
use App\Classes\Modules\Contacts\Processors\CreateContactProcessor;
use App\Classes\Modules\Remarks\Processors\CreateRemarkProcessor;

use App\Classes\Modules\Orders\Services\CreatesOrder;
use App\Classes\Modules\Orders\Services\GeneratesOrderNumber;
use App\Classes\Modules\Orders\Standards\Rules\CanCreateOrder;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderType;
use App\Models\Address;
use App\Models\Company;
use App\Models\CompanyModule;
use App\Models\Order;

class CreateOrderProcessor
{

    /** @var CanCreateOrder */
    private $canCreateOrder;

    /** @var CreatesOrder */
    private $createsOrder;

    /** @var CreatesAddress */
    private $createAddress;

    /** @var CreateOrderRolesProcessor */
    private $createOrderRolesProcessor;

    /** @var CreateContactProcessor */
    private $createContactProcessor;

    /** @var CreateRemarkProcessor */
    private $createRemarkProcessor;

    /**
     * CreateOrderProcessor constructor.
     * @param CanCreateOrder $canCreateOrder
     * @param CreatesOrder $createsOrder
     * @param CreatesAddress $createAddress
     * @param CreateOrderRolesProcessor $createOrderRolesProcessor
     */
    public function __construct(CanCreateOrder $canCreateOrder, CreatesOrder $createsOrder, CreatesAddress $createAddress, CreateOrderRolesProcessor $createOrderRolesProcessor, CreateContactProcessor $createContactProcessor, CreateRemarkProcessor $createRemarkProcessor)
    {
        $this->canCreateOrder = $canCreateOrder;
        $this->createsOrder = $createsOrder;
        $this->createAddress = $createAddress;
        $this->createOrderRolesProcessor = $createOrderRolesProcessor;
        $this->createContactProcessor = $createContactProcessor;
        $this->createRemarkProcessor = $createRemarkProcessor;
    }


    /**
     * @param Company $company
     * @param CompanyModule $originWarehouse
     * @param Address $address
     * @param int|null $orderNumber
     * @return Addressable
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Company $company, CompanyModule $originWarehouse, Address $address, int $orderNumber){

        /** @var CompanyModule $importer */
        $importer = $company->companyModules()->importers()->first();

        $orderObject = new OrderObject($orderNumber, OrderType::SHARED_CONTAINER, $company->status === ApprovalStatus::APPROVED ? ApprovalStatus::APPROVED : ApprovalStatus::PENDING_VERIFICATION);

        $this->canCreateOrder->passes($orderObject);

        /** @var Order $order */
        $order = $this->createsOrder->execute($importer, $orderObject);

        $addressObject = new AddressObject($address->street_one, $address->street_two, $address->country_id, $address->state_id, $address->district_id, $address->postcode,$address->reference);

        $order_address = $this->createAddress->execute($order, $addressObject);

        $contact = $address->contacts()->first();
        $contactObject = new ContactObject(
            $contact->reference, 
            $contact->phone, 
            '', 
            ''
        );

        if($contactObject->getPhone()){
            $order_remark =  $this->createContactProcessor->execute($contactObject, $order_address);
        }

        $remark = $address->remarks()->first();

        $remarkObject = new RemarkObject(
            $remark->content, 
            Auth()->user()->id
        );

        if($remarkObject->getContent()){
            $this->createRemarkProcessor->execute($order_address, $remarkObject);
        }

        $this->createOrderRolesProcessor->execute($originWarehouse, $order);

        return $order;
    }
}
