<?php

namespace App\Classes\Modules\PackageItems\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\Items\FetchesItem;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\Items\CanFetchItem;
use App\Http\Resources\ItemResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchPackageItemLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchItem */
    private $canFetchPackageItem;

    /** @var FetchesItem */
    private $fetchesPackageItem;

    /**
     * FetchPackageItemControllersLogic constructor.
     * @param CanFetchItem $canFetchPackageItem
     * @param FetchesItem $fetchesPackageItem
     */
    public function __construct(CanFetchItem $canFetchPackageItem, FetchesItem $fetchesPackageItem)
    {
        $this->canFetchPackageItem = $canFetchPackageItem;
        $this->fetchesPackageItem = $fetchesPackageItem;
    }


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

            $this->canFetchPackageItem->passes();

            $query = $this->fetchesPackageItem->execute(['id' => $request->route('id')]);

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

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

    }

}
