<?php

namespace App\Classes\Modules\Contacts\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Contacts\Services\ListsContacts;
use App\Classes\Modules\Contacts\Standards\Rules\CanListContacts;
use App\Http\Resources\ContactResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListContactsLogic extends AbstractControllerLogic
{

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

    /** @var CanListContacts */
    private $canListContacts;

    /** @var ListsContacts */
    private $listsContacts;

    /**
     * ListContactsControllerLogic constructor.
     * @param CanListContacts $canListContacts
     * @param ListsContacts $listsContacts
     */
    public function __construct(CanListContacts $canListContacts, ListsContacts $listsContacts)
    {
        $this->canListContacts = $canListContacts;
        $this->listsContacts = $listsContacts;
    }


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

            $this->canListContacts->passes();

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

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

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

    }

}