<?php
// Check and fix the API key issue
$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>Checking API Key Issue</h2>";
    
    // Get the current API key values from database
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('non_musking_api_key', 'sms_api_key')");
    $stmt->execute();
    $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "<h2>Current API Keys in Database:</h2>";
    foreach ($results as $result) {
        $key = $result['setting_key'];
        $value = $result['setting_value'];
        $length = strlen($value);
        $expected = 'C30027916974e980709412.23354057';
        $is_correct = ($value === $expected) ? '✅ CORRECT' : '❌ INCORRECT';
        
        echo "<p><strong>$key:</strong></p>";
        echo "<p>Value: $value</p>";
        echo "<p>Length: $length</p>";
        echo "<p>Status: $is_correct</p>";
        echo "<hr>";
    }
    
    // Check if any API key is incorrect
    $incorrect_found = false;
    foreach ($results as $result) {
        if ($result['setting_value'] !== 'C30027916974e980709412.23354057') {
            $incorrect_found = true;
        }
    }
    
    if ($incorrect_found) {
        echo "<h3>Updating Incorrect API Keys:</h3>";
        $update_stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?) ON DUPLICATE KEY UPDATE setting_value = ?");
        
        // Update both API keys to the correct value
        $update_stmt->execute(['non_musking_api_key', 'C30027916974e980709412.23354057', 'C30027916974e980709412.23354057']);
        $update_stmt->execute(['sms_api_key', 'C30027916974e980709412.23354057', 'C30027916974e980709412.23354057']);
        
        echo "<p style='color:green; font-size:18px;'>✅ API Keys updated to correct 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();
        $verify_results = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "<h3>Verification After Update:</h3>";
        foreach ($verify_results as $result) {
            $key = $result['setting_key'];
            $value = $result['setting_value'];
            $is_correct = ($value === 'C30027916974e980709412.23354057') ? '✅ CORRECT' : '❌ STILL INCORRECT';
            echo "<p><strong>$key:</strong> $value - $is_correct</p>";
        }
    } else {
        echo "<h3 style='color:green;'>✅ All API keys are already correct!</h3>";
    }
    
    // Show the expected API key for reference
    echo "<h3>Reference - Correct MRAM API Key:</h3>";
    echo "<p><strong>Expected Value:</strong> C30027916974e980709412.23354057</p>";
    echo "<p><strong>Expected Length:</strong> " . strlen('C30027916974e980709412.23354057') . "</p>";
    echo "<p><strong>Source:</strong> https://sms.mram.com.bd/Developers</p>";

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

echo "<h3>Next Steps:</h3>";
echo "<ul>";
echo "<li>Run this script multiple times if needed until all keys show as correct</li>";
echo "<li>Visit <a href='simple_sms_test.php'>Simple SMS Test</a> to verify</li>";
echo "<li>Try sending a test SMS</li>";
echo "</ul>";
?>