<?php

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


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

class FetchContainerLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchContainer */
    private $canFetchContainer;

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

    /**
     * FetchContainerControllersLogic constructor.
     * @param CanFetchContainer $canFetchContainer
     * @param FetchesContainer $fetchesContainer
     */
    public function __construct(CanFetchContainer $canFetchContainer, FetchesContainer $fetchesContainer)
    {
        $this->canFetchContainer = $canFetchContainer;
        $this->fetchesContainer = $fetchesContainer;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {

            $this->canFetchContainer->passes();

            $query = $this->fetchesContainer->execute(['id' => $request->route('id'), 'with_packing_lists' => true]);

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

        } catch (\Exception $exception){
            throw new ErrorException($exception->getMessage(), $exception->getCode());
        }

    }

}
