<?php

namespace App\Classes\Modules\Rewards\Services;

use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
use App\Models\Reward;
use App\Models\User;
use App\Models\UserReward;

class CreatesUserReward extends AbstractUpdateRelationshipRecord
{
    /**
     * @param Reward $reward
     * @param User $user
     * @param int $voucherId
     * @return \Illuminate\Database\Eloquent\Model|null
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(?Reward $reward, User $user, int $voucherId) {
        if($reward){
            $model = new UserReward();
            $model->user_id = $user->id;
            $model->voucher_id = $voucherId;
            return $this->handler($reward->users(), $model);
        }
        else{
            $model = new UserReward();
            $model->voucher_id = $voucherId;
            return $this->handler($user->rewards(), $model);
        }
    }
}
