<?php
session_start();
include 'include.php';


if($_POST['user_id'] > 0){
   $o = $factory->get_object($_POST['user_id'], "users", "user_id");
   $o->otp = rand(111111, 999999);  // 6 digit OTP

   // Send OTP via WhatsApp template
   $whatsapp_vars = array(
       '{otp}' => $o->otp,
       '{name}' => $o->name,
       '{mobile}' => $o->mobile
   );
   send_whatsapp_template('login_otp', $o->mobile, $whatsapp_vars);

   $o->user_id = $updater->update_object($o, "users");
   $result['user_id'] = $o->user_id;
   // OTP hidden from response - user will receive on WhatsApp only
   $result['error'] = 0;
   $result['error_msg'] = "OTP sent to your WhatsApp!";

} else {
   $result['error'] = 1;
   $result['error_msg'] = "Something went wrong";
}


echo json_encode($result);

?>