<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\Modules\Accounts\Services\FetchesUser;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CheckEmailLogic extends AbstractControllerLogic
{

    protected function notification():array {
        return [
            'title' => 'User found',
            'message' => 'Account Already Exists'
        ];
    }

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


    /**
     * CheckEmailLogic constructor.
     * @param FetchesUser $fetchesUser
     */
    public function __construct(FetchesUser $fetchesUser)
    {
        $this->fetchesUser = $fetchesUser;
    }


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

        try {

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

            return $this->response();

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

    }

}