<?php
// Fix the specific API key that's currently in your system
$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>Fixing Current API Key</h2>";
    
    // The API key you currently have
    $current_api_key = 'C30027916975f051ed0e01.45424521';
    // The correct API key from MRAM portal
    $correct_api_key = 'C30027916974e980709412.23354057';
    
    echo "<h3>API Key Information:</h3>";
    echo "<p><strong>Your Current API Key:</strong> $current_api_key</p>";
    echo "<p><strong>Correct MRAM API Key:</strong> $correct_api_key</p>";
    echo "<p><strong>Your Key Length:</strong> " . strlen($current_api_key) . "</p>";
    echo "<p><strong>Correct Key Length:</strong> " . strlen($correct_api_key) . "</p>";
    
    // Update the database with the correct API key
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    // Update both non-masking and general SMS API keys
    $stmt->execute(['non_musking_api_key', $correct_api_key, $correct_api_key]);
    $stmt->execute(['sms_api_key', $correct_api_key, $correct_api_key]);
    
    echo "<h3>Database Update:</h3>";
    echo "<p style='color:green; font-size:18px; font-weight:bold;'>✅ API Keys updated in database with correct MRAM value!</p>";
    
    // Verify the update
    $verify_stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('non_musking_api_key', 'sms_api_key')");
    $verify_stmt->execute();
    $results = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>Verification:</h3>";
    foreach ($results as $result) {
        $stored_key = $result['setting_value'];
        $match = ($stored_key === $correct_api_key) ? '✅ CORRECT' : '❌ INCORRECT';
        echo "<p><strong>{$result['setting_key']}:</strong> " . substr($stored_key, 0, 20) . "..." . " - $match</p>";
    }
    
    // Also update other MRAM settings to ensure consistency
    $mram_settings = [
        'non_musking_username' => 'C3002791',
        'non_musking_password' => 'hPOWSnw8rf',
        'non_musking_sender_id' => '8809601016579',
        'non_musking_api_url' => 'https://rest.mrmessaging.net/messages',
        'sms_username' => 'C3002791',
        'sms_password' => 'hPOWSnw8rf',
        'sms_sender_id' => '8809601016579',
        'sms_api_url' => 'https://rest.mrmessaging.net/messages',
        'non_musking_enabled' => '1',
        'sms_enabled' => '1'
    ];
    
    echo "<h3>Updating Other MRAM Settings:</h3>";
    foreach ($mram_settings as $key => $value) {
        $stmt->execute([$key, $value, $value]);
        echo "<p>Updated $key: <span style='color:green;'>SUCCESS</span></p>";
    }
    
    echo "<h3>Process Complete!</h3>";
    echo "<p>✅ All MRAM settings have been updated with correct values from the portal.</p>";
    echo "<p>🚀 Your system should now be properly configured to send SMS via MRAM service.</p>";

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

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