<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Auth\Events\Verified;
use App\Models\User;

class AuthController extends Controller
{
    // public function verify(Request $request)
    // {
    //     $user = UserProfile::find($request->route('id'));

    //     if (!hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
    //         return response()->json(['message' => 'Invalid verification link'], 400);
    //     }

    //     if ($user->hasVerifiedEmail()) {
    //         return response()->json(['message' => 'Email already verified'], 400);
    //     }

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

    //     return response()->json(['message' => 'Email verified successfully']);
    // }
    public function verify(Request $request)
    {
        $user = User::find($request->route('id'));

        if (!$user || !hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
            return view('auth.verify', ['message' => 'Invalid verification link', 'status' => false]);
        }

        if ($user->hasVerifiedEmail()) {
            return view('auth.verify', ['message' => 'Email already verified', 'status' => false]);
        }

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

        return view('auth.verify', ['message' => 'Email verified successfully', 'status' => true]);
    }

}
