<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

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

class FetchUserLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Fetch Users',
            'message' => 'You have successfully retrieved the user'
        ];
    }

    /** @var CanFetchUser */
    private $canFetchUser;

    /** @var FetchesUser */
    private $fetchessUser;

    /**
     * FetchUserLogic constructor.
     * @param CanFetchUser $canFetchUser
     * @param FetchesUser $fetchessUser
     */
    public function __construct(CanFetchUser $canFetchUser, FetchesUser $fetchessUser)
    {
        $this->canFetchUser = $canFetchUser;
        $this->fetchessUser = $fetchessUser;
    }

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

            $this->canFetchUser->passes();

            $query = $this->fetchessUser->execute(['id' => $request->input('user_id')]);

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

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

}