<?php
echo "<h2>Fix Non-Masking API Key</h2>";

// The correct API key from MRAM Developers portal
$correct_api_key = 'C30027916975f051ed0e01.45424521';

echo "<h3>Correct API Key from MRAM Portal:</h3>";
echo "<p><strong>API Key:</strong> $correct_api_key</p>";
echo "<p><strong>Length:</strong> " . strlen($correct_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 non_musking_api_key
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_api_key'");
    $stmt->execute();
    $current_value = $stmt->fetchColumn();
    
    echo "<h3>Current non_musking_api_key:</h3>";
    echo "<p><strong>Value:</strong> " . ($current_value ?: 'NOT SET') . "</p>";
    echo "<p><strong>Length:</strong> " . (isset($current_value) ? strlen($current_value) : 0) . "</p>";
    
    if ($current_value !== $correct_api_key) {
        echo "<p style='color:red; font-size:16px;'><strong>STATUS:</strong> ❌ INCORRECT - Will be updated</p>";
        
        // Update the non_musking_api_key
        $update_stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $update_stmt->execute(['non_musking_api_key', $correct_api_key, $correct_api_key]);
        
        echo "<p style='color:green; font-size:18px;'>✅ non_musking_api_key updated successfully!</p>";
        
        // Verify the update
        $verify_stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_api_key'");
        $verify_stmt->execute();
        $updated_value = $verify_stmt->fetchColumn();
        
        echo "<h3>Verification:</h3>";
        echo "<p><strong>Updated Value:</strong> $updated_value</p>";
        echo "<p><strong>Match Expected:</strong> " . ($updated_value === $correct_api_key ? '✅ YES' : '❌ NO') . "</p>";
    } else {
        echo "<p style='color:green; font-size:16px;'><strong>STATUS:</strong> ✅ ALREADY CORRECT - No update needed</p>";
    }
    
    // Also update the sms_api_key for consistency
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_api_key'");
    $stmt->execute();
    $current_sms_value = $stmt->fetchColumn();
    
    if ($current_sms_value !== $correct_api_key) {
        $update_stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        $update_stmt->execute(['sms_api_key', $correct_api_key, $correct_api_key]);
        echo "<p style='color:green;'>✅ sms_api_key also updated for consistency</p>";
    } else {
        echo "<p style='color:green;'>✅ sms_api_key already correct</p>";
    }

} 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 again</li>";
echo "<li>Visit <a href='enhanced_sms_test.php'>Enhanced SMS Test</a> to verify</li>";
echo "</ul>";
?>