<?php

namespace App\Livewire\Auth\Modals;

use App\Models\User;
use App\Services\VerificationService;
use Livewire\Component;

class VerifyModal extends Component
{
    public $code;
    public $user;

    public function mount($userId)
    {
        $this->user = User::findOrFail($userId);
    }

    public function verify()
    {
        if (!VerificationService::verify($this->user, $this->code)) {
            $this->addError('code', 'Ungültiger Code');
            return;
        }

        $this->dispatch('closeModal');

        return redirect()->route('dashboard.index');
    }

    public function resend()
    {
        VerificationService::create($this->user);
    }

    public function render()
    {
        return view('livewire.auth.modals.verify-modal');
    }
}
