<?php

namespace App\Classes\Modules\Transports\ControllersLogic;

use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transports\Services\CreatesTransport;
use App\Classes\Modules\Transports\Standards\Rules\CanCreateTransport;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Http\Resources\TransportResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateTransportLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Transport',
            'message' => 'You have successfully created a new Transport'
        ];
    }

    /** @var CanCreateTransport */
    private $canCreateTransport;

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

    /** @var CreatesTransport */
    private $createsTransport;

    /**
     * CreateTransportLogic constructor.
     * @param CanCreateTransport $canCreateTransport
     * @param FetchesPackingList $fetchesPackingList
     * @param CreatesTransport $createsTransport
     */
    public function __construct(CanCreateTransport $canCreateTransport, FetchesPackingList $fetchesPackingList, CreatesTransport $createsTransport)
    {
        $this->canCreateTransport = $canCreateTransport;
        $this->fetchesPackingList = $fetchesPackingList;
        $this->createsTransport = $createsTransport;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {

        $object = new TransportObject(
            $request->input('type'), 
            $request->input('courier'), 
            $request->input('tracking_number'), 
            null, 
            null, 
            pprovalStatus::PENDING_SUBMISSION
        );

        $this->canCreateTransport->passes($object);

        $query = $this->createsTransport->execute($object, $this->fetchesPackingList->execute(['id' => $request->input('packing_list_id')]));

        return $this->resourceResponse(new TransportResource($query));

    }

}
