<?php
// Comprehensive SMS settings updater
$host = 'localhost';
$dbname = 'school_management';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ]);

    echo "<h2>Updating All SMS Settings</h2>";
    
    // MRAM Official Configuration
    $mram_config = [
        'non_musking_enabled' => '1',
        'non_musking_api_url' => 'https://rest.mrmessaging.net/messages',
        'non_musking_api_key' => 'C30027916974e980709412.23354057',
        'non_musking_username' => 'C3002791',
        'non_musking_password' => 'hPOWSnw8rf',
        'non_musking_sender_id' => '8809601016579',
        'sms_enabled' => '1',
        'sms_api_url' => 'https://rest.mrmessaging.net/messages',
        'sms_api_key' => 'C30027916974e980709412.23354057',
        'sms_username' => 'C3002791',
        'sms_password' => 'hPOWSnw8rf',
        'sms_sender_id' => '8809601016579',
        'sms_test_mode' => '0',
        'sms_send_to_guardian' => '1'
    ];
    
    echo "<h3>MRAM Official Configuration:</h3>";
    echo "<ul>";
    foreach ($mram_config as $key => $value) {
        if (strpos($key, '_password') !== false) {
            echo "<li><strong>$key:</strong> ******** (hidden)</li>";
        } else {
            echo "<li><strong>$key:</strong> $value</li>";
        }
    }
    echo "</ul>";
    
    // Update all settings
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    echo "<h3>Updating Database Settings:</h3>";
    foreach ($mram_config as $key => $value) {
        $stmt->execute([$key, $value, $value]);
        echo "<p>✅ Updated $key</p>";
    }
    
    // Verify all updates
    $keys = array_keys($mram_config);
    $placeholders = str_repeat('?,', count($keys) - 1) . '?';
    $verify_stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ($placeholders)");
    $verify_stmt->execute($keys);
    $results = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>Verification Results:</h3>";
    $all_correct = true;
    foreach ($results as $result) {
        $key = $result['setting_key'];
        $stored_value = $result['setting_value'];
        $expected_value = $mram_config[$key];
        
        if ($stored_value === $expected_value) {
            echo "<p style='color:green;'>✅ $key: CORRECT</p>";
        } else {
            echo "<p style='color:red;'>❌ $key: MISMATCH (Stored: $stored_value, Expected: $expected_value)</p>";
            $all_correct = false;
        }
    }
    
    if ($all_correct) {
        echo "<h3 style='color:green; font-size:24px;'>🎉 SUCCESS: All SMS settings updated correctly!</h3>";
        echo "<p>✅ Your system is now properly configured for MRAM SMS service</p>";
        echo "<p>✅ 401 authentication errors should be resolved</p>";
        echo "<p>✅ SMS sending should work properly</p>";
    } else {
        echo "<h3 style='color:red;'>⚠️ WARNING: Some settings are still incorrect!</h3>";
    }

} catch (Exception $e) {
    echo "<p style='color: red;'>Database error: " . $e->getMessage() . "</p>";
}

echo "<h3>Next Steps:</h3>";
echo "<ul>";
echo "<li>Visit <a href='simple_sms_test.php'>Simple SMS Test</a> to verify</li>";
echo "<li>Go to Admin Panel → SMS Settings to review</li>";
echo "<li>Try sending a test SMS</li>";
echo "</ul>";
?>