<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Mail\VerifyUserEmail;
use Illuminate\Support\Facades\Mail;
class UtilityController extends Controller
{
    function verify($token){
        $verifyUser = User::where('activation_token', $token)->first();
        if(isset($verifyUser) ){
            if($verifyUser->is_verified != 1) {
                $verifyUser->is_verified = 1;
                $verifyUser->save();
                $status = "Your account is verified. You can now login.";
            }else{
                $status = "Your account is already verified. You can now login.";
            }
        }else{
            return redirect('/login')->with('warning', "Sorry your email cannot be identified.");
        }
    
        return redirect('/login')->with('message', $status);
    }
    public function resendActivationlink($email){
       $user = User::where('email', $email)->first();
       if($user){
            $data['name'] = $user->contact_name;
            $data['token'] = $user->activation_token;
            $data['email'] = $user->email;
            $response= Mail::to($user->email)->send(new VerifyUserEmail($data));
            
            return redirect('/login')->with('message', " An email has been sent to your email address containing an activation link. Please check your email and click on the link to activate your account. If you do not click the link your account will remain inactive and you will not be able to login. If you do not receive the email within a few minutes, please check your spam folder.");     
       }else{
           return redirect('/login')->with('warning', "Sorry your email cannot be identified."); 
       }
        
    }
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function refreshCaptcha()
    {
        return response()->json(['captcha'=> captcha_img()]);
    }
}
