<?php

namespace App\Classes\Modules\Transports\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transports\Services\UpdatesTransport;
use App\Classes\Modules\Transports\Services\FetchesTransport;
use App\Classes\Modules\Transports\Standards\Rules\CanUpdateTransport;
use App\Classes\Modules\Transports\DataTransferObjects\TransportObject;
use App\Http\Resources\TransportResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateTransportLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Transport',
            'message' => 'You have successfully updated the Transport'
        ];
    }

    /** @var CanUpdateTransport */
    private $canUpdateTransport;

    /** @var UpdatesTransport */
    private $updatesTransport;

    /** @var FetchesTransport */
    private $fetchesTransport;

    /**
     * UpdateTransportLogic constructor.
     * @param CanUpdateTransport $canUpdateTransport
     * @param UpdatesTransport $updatesTransport
     * @param FetchesTransport $fetchesTransport
     */
    public function __construct(CanUpdateTransport $canUpdateTransport, UpdatesTransport $updatesTransport, FetchesTransport $fetchesTransport)
    {
        $this->canUpdateTransport = $canUpdateTransport;
        $this->updatesTransport = $updatesTransport;
        $this->fetchesTransport = $fetchesTransport;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $query = $this->fetchesTransport->execute(['id' => $request->route('id')]);

        $object = new TransportObject(
            $request->input('type'), 
            $request->input('courier'), 
            $request->input('tracking_number'), 
            $request->input('dispatch_date'), 
            $request->input('drop_date'), 
            $request->input('status')
        );

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

        $query = $this->updatesTransport->execute($query, $object);

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

}
