<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\Modules\Accounts\Services\ListsUsers;
use App\Classes\Modules\Accounts\Standards\Rules\CanListUsers;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Http\Resources\UserResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListUsersLogic extends AbstractControllerLogic
{

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

    /** @var CanListUsers */
    private $canListUsers;

    /** @var ListsUsers */
    private $listsUsers;

    /**
     * ListUsersLogic constructor.
     * @param CanListUsers $canListUsers
     * @param ListsUsers $listsUsers
     */
    public function __construct(CanListUsers $canListUsers, ListsUsers $listsUsers)
    {
        $this->canListUsers = $canListUsers;
        $this->listsUsers = $listsUsers;
    }


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

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

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

}