<?php
echo "<h2>FINAL API Key Fix - Using Current MRAM Portal Value</h2>";

// The ACTUAL current API key from MRAM Developers portal (as provided by user)
$current_mram_api_key = 'C30027916975f051ed0e01.45424521';

// The old API key that was being used (incorrect)
$old_api_key = 'C30027916974e980709412.23354057';

echo "<h3>API Key Information:</h3>";
echo "<p><strong>Current MRAM Portal API Key (CORRECT):</strong> $current_mram_api_key</p>";
echo "<p><strong>Old API Key (INCORRECT):</strong> $old_api_key</p>";
echo "<p><strong>Current Key Length:</strong> " . strlen($current_mram_api_key) . "</p>";
echo "<p><strong>Old Key Length:</strong> " . strlen($old_api_key) . "</p>";

// Database connection
$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,
    ]);

    // Get current values
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('non_musking_api_key', 'sms_api_key')");
    $stmt->execute();
    $current_settings = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>BEFORE Update - Current Database Values:</h3>";
    foreach ($current_settings as $setting) {
        $key = $setting['setting_key'];
        $value = $setting['setting_value'];
        $status = ($value === $current_mram_api_key) ? '✅ CORRECT' : 
                 ($value === $old_api_key) ? '❌ OLD VALUE' : 
                 '❓ UNKNOWN VALUE';
        echo "<p><strong>$key:</strong> $value - $status</p>";
    }
    
    // Update to the CURRENT correct API key (from user's MRAM portal)
    $update_stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    
    echo "<h3>Updating to CORRECT Current MRAM Portal Value:</h3>";
    
    // Update both keys to the ACTUAL current MRAM portal value
    $update_stmt->execute(['non_musking_api_key', $current_mram_api_key, $current_mram_api_key]);
    $update_stmt->execute(['sms_api_key', $current_mram_api_key, $current_mram_api_key]);
    
    echo "<p>✅ Updated non_musking_api_key to current MRAM value</p>";
    echo "<p>✅ Updated sms_api_key to current 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();
    $updated_settings = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h3>AFTER Update - Verification:</h3>";
    $all_correct = true;
    foreach ($updated_settings as $setting) {
        $key = $setting['setting_key'];
        $value = $setting['setting_value'];
        $is_correct = ($value === $current_mram_api_key);
        $status = $is_correct ? '✅ CORRECT' : '❌ WRONG';
        echo "<p><strong>$key:</strong> $value - $status</p>";
        if (!$is_correct) $all_correct = false;
    }
    
    if ($all_correct) {
        echo "<h3 style='color:green; font-size:24px;'>🎉 SUCCESS: All API keys are now set to the correct CURRENT MRAM portal value!</h3>";
        echo "<p>The API keys now match the current MRAM Developers portal value: <strong>$current_mram_api_key</strong></p>";
    } else {
        echo "<h3 style='color:red; font-size:24px;'>❌ ERROR: Some keys 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>Test sending SMS immediately</li>";
echo "<li>Visit <a href='enhanced_sms_test.php'>Enhanced SMS Test</a> to verify</li>";
echo "<li>Make sure to use the correct MRAM portal value: $current_mram_api_key</li>";
echo "</ul>";
?>