<?php

namespace App\Classes\Modules\PackingLists\ControllersLogic;


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

class ListPackingListsLogic extends AbstractControllerLogic
{

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

    /** @var CanListPackingLists */
    private $canListPackingLists;

    /** @var ListsPackingLists */
    private $listsPackingLists;

    /**
     * ListPackingListsLogic constructor.
     * @param CanListPackingLists $canListPackingLists
     * @param ListsPackingLists $listsPackingLists
     */
    public function __construct(CanListPackingLists $canListPackingLists, ListsPackingLists $listsPackingLists)
    {
        $this->canListPackingLists = $canListPackingLists;
        $this->listsPackingLists = $listsPackingLists;
    }


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

            $this->canListPackingLists->passes();

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

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

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

    }

}
