<?php
require_once 'config/database.php';

echo "<h2>Testing MRAM API Endpoints</h2>";

// Get current settings
$stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE 'non_musking_%'");
$stmt->execute();
$currentSettings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

$possibleEndpoints = [
    'https://sms.mram.com.bd/api/v3/send',
    'https://sms.mram.com.bd/api/v3/sms/send',
    'https://sms.mram.com.bd/api/v3/sms',
    'https://api.sms.mram.com.bd/v3/send',
    'https://rest.mrmessaging.net/messages',
    'https://sms.mram.com.bd/send',
    'https://sms.mram.com.bd/api/send'
];

echo "<p>Testing various possible MRAM API endpoints:</p><br>";

foreach ($possibleEndpoints as $endpoint) {
    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
        'sender_id' => $currentSettings['non_musking_sender_id'] ?? 'SCHOOL',
        'recipient' => '8801711111111', // Test number
        'message' => 'Test message',
        'type' => 'plain'
    ]));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Accept: application/json',
        'Authorization: App ' . ($currentSettings['non_musking_api_key'] ?? '')
    ]);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlError = curl_error($ch);
    curl_close($ch);
    
    echo "Endpoint: <strong>$endpoint</strong><br>";
    echo "HTTP Code: $httpCode<br>";
    if ($curlError) {
        echo "cURL Error: $curlError<br>";
    } else {
        echo "Response: " . substr($response, 0, 200) . "...<br>";
    }
    echo "<hr>";
}

echo "<p>Based on the responses above, you can identify the correct endpoint.</p>";
?>