<?php
// otp_sms_helper.php

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/**
 * Generate a 6-digit numeric OTP
 */
function generate_otp(): string {
    return str_pad((string)random_int(0, 999999), 6, '0', STR_PAD_LEFT);
}

/**
 * DEV/DUMMY SMS sender. Replace with real API (Twilio/MSG91/etc.)
 * We keep a debug trail in session for testing.
 */
function send_sms($mobile, $message): bool {
    if (!$mobile) return false;
    $_SESSION['__sms_debug'][] = [
        'to' => $mobile,
        'message' => $message,
        'time' => date('Y-m-d H:i:s')
    ];
    // TODO: integrate SMS provider here
    return true;
}

    function sendOtp($mobile,$student,$otp)
    {
		$newmobile = trim($mobile);
        $key = "bcd35428b5cd6672771b34c95b025f4c";
        $sender = "LBSSED";
		$messagTemp = "Dear parent, $student is the OTP for $otp outpass.
Regards,

LBS Boarding School, Kota";
        $message = urlencode ($messagTemp);
        $route = "1";
        $templateid = "1407175655734983728";
        $urltoCall = "https://sms.par-ken.com/api/smsapi?key=$key&route=$route&sender=$sender&number=$newmobile&sms=$message&templateid=$templateid";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $urltoCall);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $smsResponse = curl_exec($ch);
        curl_close($ch);

	}
	
function mask_middle4($phone) {
    // keep first 3 and last 3, replace the 4 in the middle
    return substr_replace($phone, 'XXXX', 3, 4);
}