<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\Modules\Orders\Standards\Rules\CanChangeOrderAddress;
use App\Classes\Modules\Orders\Services\FetchesOrder;
use App\Classes\Modules\Addresses\Services\CreatesAddress;
use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Addresses\Services\DeletesAddress;
use App\Classes\Modules\Addresses\DataTransferObjects\AddressObject;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
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 Illuminate\Http\Request;

class RequestChangeOrderAddressProcessor
{
    /** @var CanChangeOrderAddress */
    private $canChangeOrderAddress;

    /** @var FetchesOrder */
    private $fetchesOrder;

    /** @var FetchesAddress */
    private $fetchesAddress;

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

    /** @var DeletesAddress */
    private $deletesAddress;

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

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

    /**
     * ChangeOrderAddressProcessor constructor.
     * @param FetchesOrder $fetchesOrder
     * @param CreatesAddress $createsAddress
     * @param FetchesAddress $fetchesAddress
     * @param DeletesAddress $deletesAddress
     */
    public function __construct(CanChangeOrderAddress $canChangeOrderAddress, FetchesOrder $fetchesOrder, CreatesAddress $createsAddress, FetchesAddress $fetchesAddress, DeletesAddress $deletesAddress, CreateContactProcessor $createContactProcessor, CreateRemarkProcessor $createRemarkProcessor)
    {

        $this->canChangeOrderAddress = $canChangeOrderAddress;
        $this->fetchesOrder = $fetchesOrder;
        $this->createsAddress = $createsAddress;
        $this->fetchesAddress = $fetchesAddress;
        $this->deletesAddress = $deletesAddress;
        $this->createContactProcessor = $createContactProcessor;
        $this->createRemarkProcessor = $createRemarkProcessor;
    }


    public function execute(Request $request)
    {
        $this->canChangeOrderAddress->passes();

        $order = $this->fetchesOrder->execute(['id' => $request->get('order_id')]);
        foreach ($order->addressesPendingVerification as $address) {
            $this->deletesAddress->execute($address);
        }

        $address = $this->fetchesAddress->execute(['id' => $request->get('address_id')]);
        $addressObject = new AddressObject($address->street_one, $address->street_two, $address->country_id, $address->state_id, $address->district_id, $address->postcode, $address->reference, ApprovalStatus::PENDING_VERIFICATION);
       $order_address = $this->createsAddress->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);
        }

        return $order;
    }
}
