<?php

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


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\PackingLists\Services\Packages\FetchesPackage;
use App\Classes\Modules\PackingLists\Standards\Rules\Packages\CanFetchPackage;
use App\Http\Resources\PackageResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchPackageLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchPackage */
    private $canFetchPackage;

    /** @var FetchesPackage */
    private $fetchesPackage;

    /**
     * FetchPackageControllersLogic constructor.
     * @param CanFetchPackage $canFetchPackage
     * @param FetchesPackage $fetchesPackage
     */
    public function __construct(CanFetchPackage $canFetchPackage, FetchesPackage $fetchesPackage)
    {
        $this->canFetchPackage = $canFetchPackage;
        $this->fetchesPackage = $fetchesPackage;
    }


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

            $this->canFetchPackage->passes();

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

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

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

    }

}
