<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Events\Verified;
use Illuminate\Http\JsonResponse;
use App\Models\User;
class VerificationController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Email Verification Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling email verification for any
    | user that recently registered with the application. Emails may also
    | be re-sent if the user didn't receive the original email message.
    |
    */

    use VerifiesEmails;

    /**
     * Where to redirect users after verification.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       $this->middleware('throttle:6,1')->only('verify', 'resend');
    }

    public function show(Request $request)
    {
        return view('auth.verify');
    }

    public function verify(Request $request)
    {
        $confirmationCode = $request->query('confirmation_code');
        $user = User::where('confirmation_code', $confirmationCode)->first();

        if ($user) {
            $user->update([
                'status' => 1,
                'confirmation_code' => null,
            ]);

            $notification = [
                'status' => 'Success',
                'message' => 'Verified successfully',
            ];
        } else {
            $notification = [
                'status' => 'Error',
                'message' => 'Verification failed',
            ];
        }

        return redirect(route('login'))->with($notification);
    }

    public function resend(Request $request)
    {
 
        $confirmation_code = $request->input('confirmation_code');
        if (!empty(trim($confirmation_code))) {
            $user = User::where('confirmation_code', $confirmation_code)->first();
            if ($user) {

                // Generate a new confirmation code
                $confirmation_code = time().uniqid(true);

                // Update the user record with the new confirmation code
                $user->update([
                    'confirmation_code' => $confirmation_code,
                    'status' => 0, // Reset the status if needed
                ]);

                $user->update([
                    //'confirmation_code' => $confirmationCode,
                    'status' => 0, // Reset the status if needed
                ]);

                // Send the email verification notification
                $user->sendEmailVerificationNotification();

                return $request->wantsJson()
                    ? response()->json(['message' => 'Verification email resent.'], 202)
                    : back()->with(['resent' => true, 'confirmation_code' => $confirmation_code]);
            } 
        } 
        return view('errors.404');         
    }
}
