<?php

namespace App\Classes\Modules\Accounts\Services;


use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
use App\Models\User;
use App\Models\UserEmailVerification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Str;

class GeneratesEmailVerificationAttempt extends AbstractUpdateRelationshipRecord
{

    /**
     * @param User $user
     * @return Model
     * @throws MalformedRequestException
     */
    public function execute(User $user): Model {
        $model = new UserEmailVerification();
        $model->token = Str::random(25);
        $model->is_complete = false;
        $model->is_active = true;
        $model->is_sent = true;

        return $this->handler($user->emailVerification(), $model);
    }

}
