<?php

namespace App\Classes\Modules\Rewards\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Rewards\Services\CreatesReward;
use App\Classes\Modules\Rewards\DataTransferObjects\RewardObject;
use App\Classes\Modules\Rewards\Standards\Rules\CanCreateReward;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CreateRewardLogic extends AbstractControllerLogic
{
    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Create/Update Reward',
            'message' => 'You have successfully created/updated a reward'
        ];
    }

    /** @var CreatesReward */
    private $createsReward;

    /** @var CanCreateReward */
    private $canCreateReward;

    /**
     * CreateRewardLogic constructor.
     */
    public function __construct(CreatesReward $createsMilestone, CanCreateReward $canCreateReward)
    {
        $this->createsReward = $createsMilestone;
        $this->canCreateReward = $canCreateReward;
    }

    /**
     * @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
    {
        $userRewardObject = new RewardObject(
            $request->input('id'),
            $request->input('name'),
            $request->input('description'),
            $request->input('value'),
            $request->input('type'),
            $request->input('is_active'),
            $request->input('order'),
        );

        $this->canCreateReward->passes($userRewardObject);

        $result = $this->createsReward->execute($userRewardObject);

        return $this->response(['data' => $result]);
    }


}
