<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\FetchesPackingList;
use App\Classes\Modules\PackingLists\Standards\Rules\CanFetchPackingList;
use App\Http\Resources\PackingListResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchPackingListLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchPackingList */
    private $canFetchPackingList;

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

    /**
     * FetchPackingListControllersLogic constructor.
     * @param CanFetchPackingList $canFetchPackingList
     * @param FetchesPackingList $fetchesPackingList
     */
    public function __construct(CanFetchPackingList $canFetchPackingList, FetchesPackingList $fetchesPackingList)
    {
        $this->canFetchPackingList = $canFetchPackingList;
        $this->fetchesPackingList = $fetchesPackingList;
    }


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

            $this->canFetchPackingList->passes();

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

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

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

    }

}
