<?php

namespace App\Http\Controllers\UyelerAuth;

use App\Http\Controllers\Controller;
use App\Http\Middleware\Uyeler;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;

class VerifyEmailController extends Controller
{
    /**
     * Mark the authenticated user's email address as verified.
     *
     * @param  \Illuminate\Foundation\Auth\EmailVerificationRequest  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function __invoke(EmailVerificationRequest $request)
    {
        $user = Uyeler::findOrFail($request->id);

        if ($user->email_verified_at) {
            return route('uye.login');
        }

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

        return redirect()->away('app://open'); // The deep link
    }
}
