<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request; 
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use App\Http\Controllers\Controller; 
use App\Models\User;
use Illuminate\Support\Facades\Auth; 
use Hash;
use Crypt;

class ForgotPasswordController extends Controller {
    
    public function index(Request $request)
    {
        $email = request('email'); 
        
        $user = User::select('id', 'email', 'verification_code')->where('email', $email)->first();
        
        if($user){
            
            $verificationCode = mt_rand(1000,9999);
        
            $subject = 'Listing Llama Verification Code: '.$verificationCode;
            $message = '<h1 style="margin-bottom:0px;margin-top: 25px;">Confirm your email address</h1>
                        <p style="font-size: 20px;line-height: 28px;letter-spacing: -0.2px;margin-bottom: 28px;word-break: break-word;">Your verification code is below it will help you forgot your password.</p>
                        <div style="background:#f5f4f5;border-radius:4px;padding:43px 23px;margin-right:50px;margin-bottom:30px">
                             <div style="text-align:center;vertical-align:middle;font-size:30px">'.$verificationCode.'</div>
                        </div>
                        <p style="font-size: 16px;line-height: 24px;letter-spacing: -0.2px;margin-bottom: 28px;">If you didn’t request this email, there’s nothing to worry about — you can safely ignore it.</p>';
                    
            emailFormat($email, $subject, $message);
        
            $user->verification_code = Crypt::encrypt($verificationCode);
            $user->save();
            
        }else{
            return response()->json(['success' => 0, 'message' => 'Email does not exists.']);
        }
        
        return response()->json([
            'success' => 1,
        ]);
    }
    
    public function sendCode(Request $request){
        
        $verificationCode = request('verification_code');
        $userEmail        = request('email');
        $data = [];
        
        if(!$userEmail){
            return response()->json([
                'success' => 0,
                'message' => 'Email not found.'
            ]);
        }
        
        $user = User::select('id', 'email', 'verification_code')->where('email',$userEmail)->first();
        if(!$user){
            return response()->json([
                'success' => 0,
                'message' => 'User not found.'
            ]);
        }
        
        $userVerificationCode = Crypt::decrypt($user->verification_code); 
        
        if($user && !$userVerificationCode){
            return response()->json([
                'success' => 0,
                'message' => 'Verification not found.'
            ]);
        }
        
        if($verificationCode == $userVerificationCode){

            $user->verification_code = NULL;
            $user->save();
            
            $data = [
                'user_id' => Crypt::encrypt($user->id),
                'email'   => $user->email
            ];
            
        }else{
            return response()->json(['success' => 0, 'message' => 'The OTP entered is incorrect. Please enter correct OTP.', 'verificationCode' => $verificationCode]);
        }
        
        return response()->json([
            'success'    => 1,
            'userDetail' => $data
        ]);
        
    }
    
    public function changeForgotPassword(Request $request){
        
        $userId          = Crypt::decrypt(request('id'));
        $email           = request('email');
        $newPassword     = request('new_password');
        $confirmPassword = request('confirm_password');
        
        $user = User::select('id', 'name', 'password')->where('id', $userId)->where('email', $email)->first();
        
        if($newPassword != $confirmPassword){
            return response()->json([
                'success' => 0,
                'error'   => 'The password and confirmation password do not match.'
            ], 200);
        }else{
            $user->password = Hash::make($newPassword);
            $user->save();
            return response()->json([
                'success' => 1,
                'message' => 'Your password has been successfully changed. Use your new password to log in.'
            ]);
        }
    }
    
}