<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;


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

class UpdateUserLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated User Profile',
            'message' => 'You have successfully updated the User Profile'
        ];
    }

    /** @var CanUpdateUser */
    private $canUpdateUser;

    /** @var UpdatesUser */
    private $updatesUser;

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

    /**
     * UpdateAddressLogic constructor.
     * @param CanUpdateUser $canUpdateUser
     * @param UpdatesUser $updatesUser
     * @param FetchesUser $fetchesUSer
     */
    public function __construct(CanUpdateUser $canUpdateUser, UpdatesUser $updatesUser, FetchesUser $fetchesUser)
    {
        $this->canUpdateUser = $canUpdateUser;
        $this->updatesUser = $updatesUser;
        $this->fetchesUser = $fetchesUser;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        try {
            logger("user update logic");
            $object = new FullNameObject($request->input('name'));

            $this->canUpdateUser->passes($object);

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

            $query = $this->updatesUser->execute($query, $object);

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


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

    }

}