<?php
function sendSMS($pdo, $phone, $message) {
    $gateway = getActiveSMSProvider($pdo);

    if (!$gateway) {
        return "No active SMS provider found.";
    }

    $apiKey = $gateway['api_key'];
    $username = $gateway['username'];
    $senderId = $gateway['sender_id'];
    $baseUrl = $gateway['base_url'];

    $data = [
        "username" => $username,
        "to" => $phone,
        "message" => $message,
        "from" => $senderId
    ];

    $headers = [
        "Content-Type: application/x-www-form-urlencoded",
        "Accept: application/json",
        "apiKey: $apiKey"
    ];

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $baseUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

// Example usage:
require_once '../includes/config.php'; // Your PDO connection file
$response = sendSMS($pdo, "+254716747641", "Hello! This is a test message.");
print_r($response);
?>
