<?php

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


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

class ListPackagesLogic extends AbstractControllerLogic
{

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

    /** @var CanListPackages */
    private $canListPackages;

    /** @var ListsPackages */
    private $listsPackages;

    /**
     * ListPackagesLogic constructor.
     * @param CanListPackages $canListPackages
     * @param ListsPackages $listsPackages
     */
    public function __construct(CanListPackages $canListPackages, ListsPackages $listsPackages)
    {
        $this->canListPackages = $canListPackages;
        $this->listsPackages = $listsPackages;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {
        $this->canListPackages->passes();

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

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

    }

}
