<?php

        include('../../config/config.php');
        date_default_timezone_set('Africa/Lagos');
        
        
      include ('../messaging/whatsapp/index.php');
            
            
            
               // Set CORS headers to allow requests from any origin (*)
        header("Access-Control-Allow-Origin: *");
        
        // Allow specific HTTP methods
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
        
        // Allow specific HTTP headers
        header("Access-Control-Allow-Headers: Content-Type, Authorization");
        
        // Allow cookies to be included in the request (if needed)
        header("Access-Control-Allow-Credentials:true");
        
        // Retrieve the raw POST data
        
        
        $username=  mysqli_real_escape_string($link, trim(isset($_POST['username']) ? $_POST['username'] : ''));
       
        
          $sql = ("SELECT * FROM `userlogin` WHERE `UserName`='$username'");
          $result = mysqli_query($link, $sql);
          $row = mysqli_fetch_assoc($result);
          $row_cnt = mysqli_num_rows($result);
    
          if ($row_cnt > 0) 
          {
              
              
            
                  $UserID = $row['UserID'];
                  $UserType = $row['UserType'];
                  $number = $username;
                  
                  
                  
                  
                     $currentDateTime = date('Y-m-d H:i:s');
                    $otp = random_int(1000, 9999);
                    
                    // Get the current timestamp
                    $currentTimestamp = time();
                    
                    // Add 5 minutes (5 * 60 seconds) to the current timestamp
                     $expiryTimestamp = $currentTimestamp + (5 * 60);
                    
                    // Optionally, you can convert the expiry timestamp to a human-readable format
                    
                     $expiryTime = date('Y-m-d H:i:s', $expiryTimestamp);
                      
                     $file = 'https://transformationembassy.com/assets/images/teblogo.png';
                     $msg = rawurlencode("*Change Password OTP*\n\nYour OTP is: $otp. Please enter this code to proceed.\n\nTransformtionEbassy.");
                     $apikey = '7yVA8yWyHtgs';
                     send_whatsapp_msg($number,$apikey, $msg, $file);
        
                    $updatepwd = mysqli_query($link, "UPDATE `Users` SET `OTP`='$otp', `OTPDuration`='$expiryTime' WHERE UserID='$UserID'");
                  
                   
                  
                  if($updatepwd == true)
                  {
                      
                      
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "success",
                            "responseDescription" => "OTP has been sent to your whatsApp",
                             "UserID" => $UserID,
                             "timecount" => $expiryTime
                             
                            
                          );
                      
                      
                  }else
                  {
                      
                      
                          $response = array(
                            "requestSuccessful" => true, // Assuming the request was successful
                            "responseMessage" => "failed",
                            "responseDescription" => "Error!! request not successful please try again later",
                            "UserID" => 'NULL',
                             "timecount" => 'NULL'
                            
                          );
                      
                      
                  }
                    
                     
          }
          else
          {
              
             
                   $response = array(
                        "requestSuccessful" => true, // Assuming the request was successful
                        "responseMessage" => "failed",
                        "responseDescription" => "User not found",
                         "UserID" => 'NULL',
                         "timecount" => 'NULL'
                        
                        
                      );
              
          }
      
      
         $json_response = json_encode($response);
    
        // // Output the JSON response
        echo $json_response;
       

?>