<?php

namespace App\Classes\Modules\PackageItems\ControllersLogic;


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

class ListPackageItemsLogic extends AbstractControllerLogic
{

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

    /** @var CanListItems */
    private $canListPackageItems;

    /** @var ListsPackageItems */
    private $listsPackageItems;

    /**
     * ListPackageItemsLogic constructor.
     * @param CanListItems $canListPackageItems
     * @param ListsPackageItems $listsPackageItems
     */
    public function __construct(CanListItems $canListPackageItems, ListsPackageItems $listsPackageItems)
    {
        $this->canListPackageItems = $canListPackageItems;
        $this->listsPackageItems = $listsPackageItems;
    }


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

            $this->canListPackageItems->passes();

            $query = $this->listsPackageItems->execute($this->listsPackageItems->deserializeFilters($request->input('filters')));

            return $this->collectionResponse(ItemResource::collection($query));

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

    }

}
