<?php


namespace App\Http\Controllers;

use App\Models\Verification;
use Illuminate\Support\Facades\Auth;

class VerifyController extends Controller
{
    public function verify(Verification $verification)
    {
        $user = $verification->user;

        // 🔥 schon verifiziert
        if ($user->email_verified_at) {
            return redirect()->route('dashboard.index');
        }

        // 🔥 abgelaufen
        if ($verification->isExpired()) {
            return redirect()->route('verify.notice', [
                'user' => $user->id
            ])->with('error', t('auth.verify.expired'));
        }

        // 🔥 schon benutzt
        if ($verification->isVerified()) {
            return redirect()->route('dashboard.index');
        }

        // 🔥 USER UPDATEN
        if ($verification->type === 'email') {
            $user->update([
                'email_verified_at' => now(),
            ]);
        }

        Auth::login($user);

        Verification::where('user_id', $user->id)
            ->where('type', $verification->type)
            ->delete();

        return redirect()->route('dashboard.index')
            ->with('success', t('auth.verify.success'));
    }
}

//
//namespace App\Http\Controllers;
//
//use App\Models\User;
//use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Auth;
//
//class VerifyController extends Controller
//{
//    public function verify(User $user)
//    {
//        if ($user->email_verified_at) {
//            return redirect()->route('dashboard.index');
//        }
//
//        $user->update([
//            'email_verified_at' => now(),
//            'email_verification_code' => null,
//            'email_verification_expires_at' => null,
//            'verification_resends' => 0,
//            'verification_resends_reset_at' => null,
//        ]);
//
//        Auth::login($user);
//
//        return redirect()->route('dashboard.index')
//            ->with('success', t('auth.verify.success'));
//    }
//}
