<?php

use CodeIgniter\Database\BaseConnection;

if (!function_exists('sendOtpSms')) {

   
    function sendOtpSms(string $mobile, string $otp)
    {
        $apiKey = env('SMS_API_KEY');
        $senderId = env('SMS_SENDER_ID');
        $templateId = env('SMS_TEMPLATE_ID');
        $serviceName = env('SMS_SERVICE_NAME');
        $appName = env('SMS_APP_NAME');

        // Replace variables {#var#}
        $message = "Hi, Your One Time Code for logging into Nhance {$appName} App is {$otp}. Valid for 3 minutes. Please do not share this with anyone. -NHANCE";

        // Build the API URL
        $url = "https://smsapi.24x7sms.com/api_2.0/SendSMS.aspx?" . http_build_query([
            'APIKEY'        => $apiKey,
            'MobileNo'      => $mobile,
            'SenderID'      => $senderId,
            'Message'       => $message,
            'ServiceName'   => $serviceName,
            'DLTTemplateID' => $templateId,
        ]);

        // Send request
        $response = @file_get_contents($url);

        // If response failed
        if ($response === FALSE) {
            return ['status' => 'failed', 'message' => 'Failed to send SMS.'];
        }


        return ['status' => 'success', 'message' => 'OTP sent successfully', 'api_response' => $response];
    }
}
