<?php

namespace App\Classes\Modules\Contacts\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Contacts\Services\FetchesContact;
use App\Classes\Modules\Contacts\Standards\Rules\CanFetchContact;
use App\Classes\Modules\Contacts\DataTransferObjects\ContactObject;
use App\Http\Resources\ContactResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchContactLogic extends AbstractControllerLogic
{

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

    /** @var CanFetchContact */
    private $canFetchContact;

    /** @var FetchesContact */
    private $fetchesContact;

    /**
     * FetchContactControllerLogic constructor.
     * @param CanFetchContact $canFetchContact
     * @param FetchesContact $fetchesContact
     */
    public function __construct(CanFetchContact $canFetchContact, FetchesContact $fetchesContact)
    {
        $this->canFetchContact = $canFetchContact;
        $this->fetchesContact = $fetchesContact;
    }


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

            $this->canFetchContact->passes();

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

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

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

    }

}