<?php
echo "<h2>Fix API Key Issue - FINAL CORRECT VALUE</h2>";

// The CORRECT current API key from MRAM Developers portal (the one you provided)
$correct_api_key = 'C30027916975f051ed0e01.45424521';

// The old/wrong API key that keeps getting set
$wrong_api_key = 'C30027916974e980709412.23354057';

echo "<h3>API Key Information:</h3>";
echo "<p><strong>CORRECT Current MRAM Portal API Key:</strong> $correct_api_key</p>";
echo "<p><strong>WRONG Old API Key (being set instead):</strong> $wrong_api_key</p>";
echo "<p><strong>Correct Key Length:</strong> " . strlen($correct_api_key) . "</p>";
echo "<p><strong>Wrong Key Length:</strong> " . strlen($wrong_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 API keys
    $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>Current API Keys in Database BEFORE Update:</h3>";
    foreach ($current_settings as $setting) {
        $key = $setting['setting_key'];
        $value = $setting['setting_value'];
        $is_correct = ($value === $correct_api_key);
        $is_wrong = ($value === $wrong_api_key);
        $status = $is_correct ? '✅ CORRECT' : ($is_wrong ? '❌ WRONG (OLD VALUE)' : '❓ UNKNOWN VALUE');
        echo "<p><strong>$key:</strong> $value - $status</p>";
    }
    
    // Update both API keys to the CORRECT value
    $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 non_musking_api_key to the CORRECT current value
    $update_stmt->execute(['non_musking_api_key', $correct_api_key, $correct_api_key]);
    echo "<p>✅ Updated non_musking_api_key to CORRECT value: $correct_api_key</p>";
    
    // Update sms_api_key to the CORRECT current value
    $update_stmt->execute(['sms_api_key', $correct_api_key, $correct_api_key]);
    echo "<p>✅ Updated sms_api_key to CORRECT value: $correct_api_key</p>";
    
    // Also update other related settings to ensure consistency
    $mram_settings = [
        'non_musking_username' => 'C3002791',
        'non_musking_password' => 'hPOWSnw8rf',
        'non_musking_sender_id' => '8809601016579',
        'non_musking_api_url' => 'https://sms.mram.com.bd/smsapi',
        'sms_username' => 'C3002791',
        'sms_password' => 'hPOWSnw8rf',
        'sms_sender_id' => '8809601016579',
        'sms_api_url' => 'https://sms.mram.com.bd/smsapi',
        'non_musking_enabled' => '1',
        'sms_enabled' => '1'
    ];
    
    echo "<h3>Updating Other MRAM Settings:</h3>";
    foreach ($mram_settings as $key => $value) {
        $update_stmt->execute([$key, $value, $value]);
        echo "<p>✅ Updated $key</p>";
    }
    
    // Final verification
    echo "<h3>FINAL VERIFICATION AFTER Update:</h3>";
    $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();
    $final_results = $verify_stmt->fetchAll(PDO::FETCH_ASSOC);
    
    $all_correct = true;
    foreach ($final_results as $result) {
        $key = $result['setting_key'];
        $value = $result['setting_value'];
        $is_correct = ($value === $correct_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: Both API keys are NOW set to the CORRECT current MRAM portal value!</h3>";
        echo "<p><strong>Final value:</strong> $correct_api_key</p>";
    } else {
        echo "<h3 style='color:red; font-size:24px;'>❌ FAILURE: Some keys are still incorrect!</h3>";
    }

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

echo "<h3>Important Notes:</h3>";
echo "<ul>";
echo "<li>The CORRECT API key from MRAM portal is: <strong>$correct_api_key</strong></li>";
echo "<li>The WRONG old API key (causing issues) is: <strong>$wrong_api_key</strong></li>";
echo "<li>Make sure your system uses the correct value!</li>";
echo "</ul>";

echo "<h3>Next Steps:</h3>";
echo "<ul>";
echo "<li>Test sending SMS immediately</li>";
echo "<li>Visit <a href='advanced_sms_test.php'>Advanced SMS Test</a> to verify</li>";
echo "</ul>";
?>