<?php

// 1. Configuration
$apiToken = '720|yOWd0DRSjz2DV2K14sU52B32KUyPGaSfTCw96iL6bb59fe22'; 

// Corrected Live Production API Endpoint
$endpoint = 'https://www.bulksmsnigeria.com/api/v2/sms';

// 2. Prepare the Payload
$payload = [
    'from'    => 'Intersite',          // Your registered, whitelisted sender ID
    'to'      => '2349133614644,2349115993482,2349019120565,2348106676105',   // Target phone number
    'body'    => 'Testing this message from sanny', // Message content
    'gateway' => 'premium'          // Forces delivery via the high-priority DND bypass route
];

// 3. Initialize and Configure cURL
$ch = curl_init($endpoint);

// Set necessary cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as a string
curl_setopt($ch, CURLOPT_POST, true);           // Use POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); // Attach JSON payload

// Set HTTP headers exactly as required
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Authorization: Bearer ' . $apiToken,
    'Content-Type: application/json',
    'Accept: application/json'
]);

// 4. Execute the Request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);

curl_close($ch);

// 5. Handle the Response
if ($curlError) {
    // Catches direct network layer failures (DNS issues, timeouts)
    echo "Network Error: " . $curlError . "\n";
} else {
    // Decode the JSON response
    $responseData = json_decode($response, true);

    // Check if the request was successful
    if ($httpCode === 200 && isset($responseData['status']) && $responseData['status'] === 'success') {
        echo "✅ Success! Message sent.\n";
        echo "Message ID: " . $responseData['data']['message_id'] . "\n";
        echo "Cost: " . $responseData['data']['cost'] . " " . $responseData['data']['currency'] . "\n";
        echo "Recipients: " . $responseData['data']['recipients_count'] . "\n";
        
        if (isset($responseData['data']['sandbox_mode']) && $responseData['data']['sandbox_mode']) {
            echo "Note: Sent in SANDBOX mode. No real SMS was delivered.\n";
        }
    } else {
        // Handle API/Validation/Gateway Errors
        echo "❌ Failed to send message.\n";
        echo "HTTP Status: " . $httpCode . "\n";
        
        // Safely check for API error array details
        if (isset($responseData['error']) && is_array($responseData['error'])) {
            $errCode = isset($responseData['error']['code']) ? $responseData['error']['code'] : (isset($responseData['code']) ? $responseData['code'] : 'N/A');
            $errMsg = isset($responseData['error']['message']) ? $responseData['error']['message'] : 'Unknown API error details.';
            
            echo "BulkSMS Error Code: " . $errCode . "\n";
            echo "Description: " . $errMsg . "\n";
        } elseif (isset($responseData['message'])) {
            echo "API Message: " . $responseData['message'] . "\n";
            if (isset($responseData['code'])) {
                echo "BulkSMS Error Code: " . $responseData['code'] . "\n";
            }
        } else {
            echo "Raw Response: " . $response . "\n";
        }
    }
}
?>
