<?php

namespace App\Classes\Modules\Addresses\ControllersLogic;


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

class FetchAddressLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchAddress */
    private $canFetchAddress;

    /** @var FetchesAddress */
    private $fetchesAddress;

    /**
     * FetchAddressControllersLogic constructor.
     * @param CanFetchAddress $canFetchAddress
     * @param FetchesAddress $fetchesAddress
     */
    public function __construct(CanFetchAddress $canFetchAddress, FetchesAddress $fetchesAddress)
    {
        $this->canFetchAddress = $canFetchAddress;
        $this->fetchesAddress = $fetchesAddress;
    }


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

        $query = $this->fetchesAddress->execute(['id' => $request->route('id')]);

        return $this->resourceResponse(new AddressResource($query));

    }

}