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

echo "<h2>Fixing MRAM SMS API Endpoint</h2>";

echo "<p>The error indicates the endpoint 'api/v3/send' was not found. Let me update to the correct MRAM endpoint.</p>";

try {
    // 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);
    
    // Update to the most likely correct endpoint based on MR Messaging documentation
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Update API URL to the correct MRAM endpoint
    $stmt->execute(['non_musking_api_url', 'https://rest.mrmessaging.net/messages', 'https://rest.mrmessaging.net/messages']);
    
    // Keep other settings intact
    $stmt->execute(['non_musking_api_key', $currentSettings['non_musking_api_key'] ?? 'C30027916974e980709412.23354057', $currentSettings['non_musking_api_key'] ?? 'C30027916974e980709412.23354057']);
    $stmt->execute(['non_musking_username', $currentSettings['non_musking_username'] ?? 'C3002791', $currentSettings['non_musking_username'] ?? 'C3002791']);
    $stmt->execute(['non_musking_password', $currentSettings['non_musking_password'] ?? 'hPOWSnw8rf', $currentSettings['non_musking_password'] ?? 'hPOWSnw8rf']);
    $stmt->execute(['non_musking_sender_id', $currentSettings['non_musking_sender_id'] ?? '8809601016579', $currentSettings['non_musking_sender_id'] ?? '8809601016579']);
    $stmt->execute(['non_musking_enabled', '1', '1']);
    
    echo "✅ API URL updated to correct endpoint: https://rest.mrmessaging.net/messages<br>";
    echo "✅ Other settings preserved<br>";
    echo "✅ Configuration updated<br>";
    
    echo "<br><strong>Updated Configuration:</strong><br>";
    echo "• API URL: https://rest.mrmessaging.net/messages<br>";
    echo "• API Key: " . ($currentSettings['non_musking_api_key'] ?? 'C30027916974e980709412.23354057') . "<br>";
    echo "• Username: " . ($currentSettings['non_musking_username'] ?? 'C3002791') . "<br>";
    echo "• Password: " . ($currentSettings['non_musking_password'] ?? 'hPOWSnw8rf') . "<br>";
    echo "• Sender ID: " . ($currentSettings['non_musking_sender_id'] ?? '8809601016579') . "<br>";
    
    echo "<br><strong>Next Steps:</strong><br>";
    echo "1. <a href='admin/test_non_musking_settings.php'>Test the configuration</a><br>";
    echo "2. Try sending a test SMS to verify it works<br>";

} catch (Exception $e) {
    echo "<strong>❌ Error:</strong> " . $e->getMessage() . "<br>";
}
?>