<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\RedirectResponse;

class VerifyEmailController extends Controller
{
    public function __invoke(EmailVerificationRequest $request): RedirectResponse
    {
        if ($request->user()->hasVerifiedEmail()) {
            $url = $request->user()->role === 'admin' ? 'admin.dashboard' : 'user.dashboard';
            return redirect()->intended(route($url, absolute: false));
        }

        if ($request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
        }

        if ($request->user()->role === 'user') {
            return redirect()->route('user.bantuan')->with('verified_first_time', true);
        }

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