<?php

namespace App\Classes\Modules\Companies\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Companies\Services\ListsCompanies;
use App\Classes\Modules\Companies\Standards\Rules\CanListCompanies;
use App\Http\Resources\CompanyResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListCompaniesLogic extends AbstractControllerLogic
{

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

    /** @var CanListCompanies */
    private $canListCompanies;

    /** @var ListsCompanies */
    private $listsCompanies;

    /**
     * ListCompaniesControllersLogic constructor.
     * @param CanListCompanies $canListCompanies
     * @param ListsCompanies $listsCompanies
     */
    public function __construct(CanListCompanies $canListCompanies, ListsCompanies $listsCompanies)
    {
        $this->canListCompanies = $canListCompanies;
        $this->listsCompanies = $listsCompanies;
    }


    /**
     * @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->canListCompanies->passes();

        $query = $this->listsCompanies->execute($this->listsCompanies->deserializeFilters($request->input('filters')));
        return $this->collectionResponse(CompanyResource::collection($query));

    }

}