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

echo "<h2>Updating MRAM SMS API Endpoint to Official Domain</h2>";

try {
    // Create settings table if it doesn't exist
    $pdo->exec("
        CREATE TABLE IF NOT EXISTS settings (
            id INT PRIMARY KEY AUTO_INCREMENT,
            setting_key VARCHAR(100) UNIQUE,
            setting_value TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        )
    ");
    echo "✓ Settings table verified<br>";

    // Update to the correct MRAM API endpoint as per project memory
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");

    // Update API URL to the working MR Messaging endpoint
    $stmt->execute(['non_musking_api_url', 'https://rest.mrmessaging.net/messages', 'https://rest.mrmessaging.net/messages']);
    
    // Also update the general SMS API URL if needed
    $stmt->execute(['sms_api_url', 'https://rest.mrmessaging.net/messages', 'https://rest.mrmessaging.net/messages']);

    echo "✅ API URL updated to working MR Messaging endpoint: https://rest.mrmessaging.net/messages<br>";
    echo "✅ Configuration updated<br>";
    
    echo "<br><strong>Updated Configuration:</strong><br>";
    echo "• API URL: https://rest.mrmessaging.net/messages<br>";
    echo "• This is the working MR Messaging endpoint that should work with your credentials<br>";
    
    echo "<br><strong>Next Steps:</strong><br>";
    echo "1. <a href='admin/sms_settings.php'>Review and adjust your SMS settings if needed</a><br>";
    echo "2. <a href='admin/test_non_musking_settings.php'>Test the configuration</a><br>";
    echo "3. Try sending a test SMS to verify it works<br>";

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