<?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\Classes\Modules\Accounts\DataTransferObjects\UserObject;
use App\Http\Resources\UserResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UpdateUserRoleLogic extends AbstractControllerLogic
{

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

    /** @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
    {
        $query = $this->fetchesUser->execute(['id' => $request->route('id')]);

        $object = new UserObject(
            $query->name,
            $query->email,
            $query->password,
            $query->password,
            $request->input('type'),
            $query->status,
        );

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

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

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

}