<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Addresses\Services\ListsAddresses;
use App\Classes\Modules\Addresses\Standards\Rules\CanListAddresses;
use App\Http\Resources\AddressResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ListAddressesLogic extends AbstractControllerLogic
{

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

    /** @var CanListAddresses */
    private $canListAddresses;

    /** @var ListsAddresses */
    private $listsAddresses;

    /**
     * ListAddressesLogic constructor.
     * @param CanListAddresses $canListAddresses
     * @param ListsAddresses $listsAddresses
     */
    public function __construct(CanListAddresses $canListAddresses, ListsAddresses $listsAddresses)
    {
        $this->canListAddresses = $canListAddresses;
        $this->listsAddresses = $listsAddresses;
    }


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

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

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

    }

}