<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic\Containers;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Containers\UpdatesContainer;
use App\Classes\Modules\PackingLists\Services\Containers\FetchesContainer;
use App\Classes\Modules\PackingLists\Standards\Rules\Containers\CanUpdateContainer;
use App\Classes\Modules\PackingLists\DataTransferObjects\ContainerObject;
use App\Http\Resources\ContainerResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateContainerLogic extends AbstractControllerLogic
{

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

    /** @var CanUpdateContainer */
    private $canUpdateContainer;

    /** @var UpdatesContainer */
    private $updatesContainer;

    /** @var FetchesContainer */
    private $fetchesContainer;

    /**
     * UpdateContainerLogic constructor.
     * @param CanUpdateContainer $canUpdateContainer
     * @param UpdatesContainer $updatesContainer
     * @param FetchesContainer $fetchesContainer
     */
    public function __construct(CanUpdateContainer $canUpdateContainer, UpdatesContainer $updatesContainer, FetchesContainer $fetchesContainer)
    {
        $this->canUpdateContainer = $canUpdateContainer;
        $this->updatesContainer = $updatesContainer;
        $this->fetchesContainer = $fetchesContainer;
    }


    /**
     * @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
    {

        $container = $this->fetchesContainer->execute(['id' => $request->route('id')]);
        
        $object = new ContainerObject( 
            $request->input('container_reference'), 
            $container->container_number, 
            $request->input('container_type'), 
            $request->input('seal_reference'),
            $request->input('loading_date'),
            $container->status,
        );

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

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

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

    }

}
