<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;

use App\Classes\Exceptions\ResourceNotFoundException;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\Orders\Services\FetchesOrder;

use App\Classes\Modules\PackingLists\Services\UpdatesPackingListOwner;
use App\Classes\Modules\Transports\Services\UpdatesTransportStatus;
use App\Classes\Modules\Unity\Services\CreatesContract;
use App\Classes\Modules\Unity\Processors\ActivateContractProcessor;
use App\Classes\Modules\Unity\Processors\CreateContractEntityProcessor;
use App\Classes\Modules\Unity\Processors\AssignContractEntityProcessor;
use App\Classes\Modules\PackingLists\Services\UpdatesPackingListContractReference;
use App\Classes\Modules\Steps\Services\CreatesStep;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PackingListType;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;

use App\Classes\Modules\Steps\DataTransferObjects\StepsObject;

use App\Http\Resources\PackingListResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AssignPackingListOrderLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Assign Packing List Order',
            'message' => 'You have successfully created a Packing List Order'
        ];
    }

    /** @var FetchesPackingList */
    private $fetchesPackingList;


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

    /** @var UpdatesPackingListOwner */
    private $updatesPackingListOwner;

    /** @var UpdatesTransportStatus */
    private $updatesTransportStatus;

    /** @var CreatesContract */
    private $unityCreateContract;

    /** @var ActivateContractProcessor */
    private $unityActivateContract;

    /** @var CreateContractEntityProcessor */
    private $unityCreateContractEntity;

    /** @var AssignContractEntityProcessor */
    private $unityAssignContractEntity;

    /** @var UpdatesPackingListContractReference */
    private $updatesPackingListContractReference;

    /** @var CreatesStep */
    private $createsStep;

    /**
     * CreateRemarkLogic constructor.
     * @param FetchesPackingList $fetchesPackingList
     * @param FetchesOrder $fetchesOrder
     * @param UpdatesPackingListOwner $updatesPackingListOwner
     * @param UpdatesTransportStatus $updatesTransportStatus
     * @param CreatesContract $unityCreateContract
     * @param ActivateContractProcessor $unityActivateContract
     * @param CreateContractEntityProcessor $unityCreateContractEntity
     * @param AssignContractEntityProcessor $unityAssignContractEntity
     * @param UpdatesPackingListContractReference $updatesPackingListContractReference
     * @param CreatesStep $createsStep
     */
    public function __construct(FetchesPackingList $fetchesPackingList, FetchesOrder $fetchesOrder, UpdatesPackingListOwner $updatesPackingListOwner, UpdatesTransportStatus $updatesTransportStatus, CreatesContract $unityCreateContract,  ActivateContractProcessor $unityActivateContract, CreateContractEntityProcessor $unityCreateContractEntity, AssignContractEntityProcessor $unityAssignContractEntity, UpdatesPackingListContractReference $updatesPackingListContractReference, CreatesStep $createsStep)
    {
        $this->fetchesPackingList = $fetchesPackingList;
        $this->fetchesOrder = $fetchesOrder;
        $this->updatesPackingListOwner = $updatesPackingListOwner;
        $this->updatesTransportStatus = $updatesTransportStatus;

        $this->unityCreateContract = $unityCreateContract;
        $this->unityActivateContract = $unityActivateContract;
        $this->unityCreateContractEntity = $unityCreateContractEntity;
        $this->unityAssignContractEntity = $unityAssignContractEntity;

        $this->updatesPackingListContractReference = $updatesPackingListContractReference;
        $this->createsStep = $createsStep;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function logic(Request $request) : JsonResponse
    {
        $warehouse_packing_list = $this->fetchesPackingList->execute([
            'id' => $request->route('id'), 
            'type' => PackingListType::WAREHOUSE_RECEIVE_LIST
        ]);

        $order = $this->fetchesOrder->execute(['reference' => $request->route('reference')]);

        $warehouse_packing_list = $this->updatesPackingListOwner->execute($warehouse_packing_list, $order);


        $transport = $warehouse_packing_list->transports()->first();
        $transport = $this->updatesTransportStatus->execute($transport, ApprovalStatus::APPROVED);

        $contract = $this->unityCreateContract->execute();
        $contractReference = $contract->hash_id;
        $contractObligations = $contract->contract_obligation_list;

        $this->unityActivateContract->execute($contractReference);
        $supervisorHashId = $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->unity_hash_id;

        $supervisorContractEntity = $this->unityCreateContractEntity->execute($contractReference, $supervisorHashId);
        $order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->update(['entity_hash_id' => $supervisorContractEntity->hash_id, 'entity_signature' => $supervisorContractEntity->entity_signature_hash_id]);

        $importerContractEntity = $this->unityCreateContractEntity->execute($contractReference, $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->appointee->unity_hash_id);
        $order->orderRoles()->where('role_id', '=', OrderRoleTypes::IMPORTER)->first()->update(['entity_hash_id' => $importerContractEntity->hash_id, 'entity_signature' => $importerContractEntity->entity_signature_hash_id]);

        $this->unityAssignContractEntity->execute($supervisorContractEntity->hash_id, $contractObligations);

        try {
            $packing_list = $this->fetchesPackingList->execute([
                'reference' => $warehouse_packing_list->reference,
                'type' => PackingListType::SHIPPING_PACKING_LIST
            ]);

            $warehouse_packing_list = $this->updatesPackingListOwner->execute($packing_list, $order);

            $this->updatesPackingListContractReference->execute($packing_list, $contractReference);

            foreach($contractObligations as $obligation) {
                $stepObject = new StepsObject($order->orderRoles()->where('role_id', '=', OrderRoleTypes::SUPERVISOR)->first()->appointee->id, $obligation->reference, $obligation->sequence, $obligation->hash_id);
                $this->createsStep->execute($packing_list, $stepObject);
            }
        } catch (ResourceNotFoundException $exception) {

        }


        return $this->resourceResponse(new PackingListResource($warehouse_packing_list));
    }
}
