<?php

namespace App\Classes\Modules\Accounts\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;

use App\Classes\Modules\Accounts\Standards\Rules\CanCreateUser;
use App\Classes\Modules\Accounts\Services\CreatesUser;
use App\Classes\Modules\Accounts\DataTransferObjects\RegistrationObject;
use App\Http\Resources\UserResource;
use App\Classes\ValueObjects\Constants\RoleTypes;
use App\Classes\ValueObjects\Constants\ApprovalStatus;

use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CreateAdminUserLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Created Account',
            'message' => 'You have successfully created a new Account'
        ];
    }

    /** @var CanCreateUser */
    private $canCreateUser;

    /** @var CreatesUser */
    private $createsUser;


    /**
     * CreateUserLogic constructor.
     * @param CanCreateUser $canCreateUser
     * @param CreatesUser $createsUser
     */
    public function __construct(
        CanCreateUser $canCreateUser,
        CreatesUser $createsUser
    )
    {
        $this->canCreateUser = $canCreateUser;
        $this->createsUser = $createsUser;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function logic(Request $request) : JsonResponse
    {

        $user_object = new RegistrationObject($request->input('name'), $request->input('email'),
            $request->input('password'), $request->input('password_confirmation'),
            RoleTypes::ADMIN, ApprovalStatus::APPROVED);

        $this->canCreateUser->passes($user_object);

        $user = $this->createsUser->execute($user_object);

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

    }
}