<?php

namespace App\Classes\Modules\Rewards\Services;

use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Rewards\DataTransferObjects\RewardObject;
use App\Models\Reward;
use App\Classes\Exceptions\MalformedRequestException;
use Illuminate\Database\QueryException;

class CreatesReward extends AbstractUpdateRecord
{
    /**
     * @param RewardObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(RewardObject $object) {

        // $model = new Reward();
        // $model->name = $object->getName();
        // $model->description = $object->getDescription();
        // $model->value = $object->getValue();
        // $model->type = $object->getType();
        // $model->is_active = $object->getIsActive();
        // return $this->handler($model);

        try{
            $data = [
                'id' => $object->getId(),
                'name' => $object->getName(),
                'description' => $object->getDescription(),
                'value' => $object->getValue(),
                'type' => $object->getType(),
                'is_active' => $object->getIsActive(),
                'order' => $object->getOrder(),
            ];

            return Reward::upsert([$data], ['id'], ['name', 'description', 'value', 'type', 'is_active', 'order']);

        } catch (QueryException $exception){
            throw new MalformedRequestException($exception);
        }
    }
}
