<?php

namespace App\Classes\Modules\Orders\Processors;

use App\Classes\Modules\Addresses\Services\FetchesAddress;
use App\Classes\Modules\Orders\Standards\Rules\CanApproveChangeOrderAddress;
use App\Classes\Modules\Orders\Services\UpdatesOrdersAddress;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Classes\ValueObjects\Constants\PackingListType;
use Illuminate\Http\Request;

class ApproveChangeOrderAddressProcessor
{
    /** @var CanApproveChangeOrderAddress */
    private $canApproveChangeOrderAddress;

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

    /** @var UpdatesOrdersAddress */
    private $updatesOrdersAddress;

    /** @var UpdateDoFromVTPortalProcessor  */
    private $updateDoFromVTPortalProcessor;

    /** @var UpdateDoFromYDPortalProcessor */
    private $updateDoFromYDPortalProcessor ;

    /**
     * ApproveChangeOrderAddressProcessor constructor.
     * @param CanApproveChangeOrderAddress $canApproveChangeOrderAddress
     * @param FetchesAddress $fetchesAddress
     * @param UpdatesOrdersAddress $updatesOrdersAddress
     * @param \App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor
     * @param UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor
     */
    public function __construct(CanApproveChangeOrderAddress $canApproveChangeOrderAddress, FetchesAddress $fetchesAddress, UpdatesOrdersAddress $updatesOrdersAddress, \App\Classes\Modules\Orders\Processors\UpdateDoFromVTPortalProcessor $updateDoFromVTPortalProcessor, UpdateDoFromYDPortalProcessor $updateDoFromYDPortalProcessor)
    {
        $this->canApproveChangeOrderAddress = $canApproveChangeOrderAddress;
        $this->fetchesAddress = $fetchesAddress;
        $this->updatesOrdersAddress = $updatesOrdersAddress;
        $this->updateDoFromVTPortalProcessor = $updateDoFromVTPortalProcessor;
        $this->updateDoFromYDPortalProcessor = $updateDoFromYDPortalProcessor;
    }


    /**
     * @param Request $request
     * @return mixed
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\InternalServerErrorException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function execute(Request $request)
    {
        $status = (int) $request->route('status');

        $this->canApproveChangeOrderAddress->passes();

        $address = $this->fetchesAddress->execute(['id' => $request->route('id')]);

        if($status === ApprovalStatus::APPROVED){
            $address->owner->addresses()->update(['status' => ApprovalStatus::EXPIRED]);

            $appointee = $address->owner->orderRoles()->where('role_id', '=', OrderRoleTypes::ORIGIN_FREIGHT_FORWARDER)->first()->appointee->id;
            if($appointee === 2) {
                foreach($address->owner->packingLists()->where('type', PackingListType::SHIPPING_PACKING_LIST) as $packingList){
                    $this->updateDoFromVTPortalProcessor->execute($packingList);
                }
            }

        }

        $address = $this->updatesOrdersAddress->execute($address, $status);

        return $address;
    }
}
