<?php
/**
 * SMS dispatcher for patient registration using Zenoph Notify SDK
 * Absolute paths used to avoid any autoload issues
 */

error_reporting(E_ALL);
ini_set('display_errors', 0);

// Absolute path to the SDK autoloader
$zenophLoader = "/home2/kqmucpub/public_html/dripdental/application/views/patient/Zenoph/Notify/AutoLoader.php";

// Verify path existence before loading
if (!file_exists($zenophLoader)) {
    error_log("[SMS] Zenoph AutoLoader not found at: {$zenophLoader}");
    return false;
}

require_once $zenophLoader;

use Zenoph\Notify\Enums\AuthModel;
use Zenoph\Notify\Request\SMSRequest;
use Zenoph\Notify\Enums\SMSType;

/**
 * Send personalized registration SMS
 */
function sendPatientInfoSms($firstName, $middleName, $lastName, $mobile, $patientNo, $regDate) {

    try {
        // Build full name
        $fullName = trim($firstName . " " . ($middleName ? $middleName . " " : "") . $lastName);

        // Log attempt
        error_log("[SMS] Preparing personalized SMS for {$fullName}, PatientNo: {$patientNo}, Phone: {$mobile}");

        // Initialize SDK request
        $request = new SMSRequest();

        // REQUIRED: Host for your SMS account
        $request->setHost("api.notify.biz");   // Replace if your host differs

        // Authentication model
        $request->setAuthModel(AuthModel::API_KEY);

        // Your API Key
        $request->setAuthApiKey("8c4fe6f45f55ea50585e7cf31205dd3cdb6ccbf604af314beae0fe4505e7fbc0");

        // Default sender ID
        $request->setSender("AfritSys");

        // Message template with variables as recommended
        $request->setMessage(
            'Hello {$name}, your Drip Dental Clinic registration is complete. ' .
            'Your Patient Number is {$patient_no} and your registration date is {$reg_date}. ' .
            'Thank you for choosing us.'
        );

        $request->setSMSType(SMSType::GSM_DEFAULT);

        // Values to substitute in message
        $values = array(
            $fullName,
            $patientNo,
            $regDate
        );

        // Add personalized destination
        $request->addPersonalisedDestination(
            $mobile,
            false,
            $values
        );

        // Submit SMS
        $response = $request->submit();

        error_log("[SMS] Personalized SMS successfully submitted to {$mobile}");
        return true;
    }

    catch (Exception $e) {
        error_log("[SMS] Exception while sending to {$mobile}: " . $e->getMessage());
        return false;
    }
}
