<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;


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

class FetchUserByEmailLogic extends AbstractControllerLogic
{

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

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

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

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


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

        $query = $this->fetchesUser->execute(['email' => $request->route('email')]);

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

    }

}
