<?php
echo "<h2>Fix API Key Issue</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 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:</h3>";
    foreach ($current_settings as $setting) {
        $key = $setting['setting_key'];
        $value = $setting['setting_value'];
        $status = ($value === $correct_api_key) ? '✅ CORRECT' : '❌ INCORRECT';
        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 API Keys:</h3>";
    
    // Update non_musking_api_key
    $update_stmt->execute(['non_musking_api_key', $correct_api_key, $correct_api_key]);
    echo "<p>✅ Updated non_musking_api_key</p>";
    
    // Update sms_api_key
    $update_stmt->execute(['sms_api_key', $correct_api_key, $correct_api_key]);
    echo "<p>✅ Updated sms_api_key</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://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:</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);
    
    foreach ($final_results as $result) {
        $key = $result['setting_key'];
        $value = $result['setting_value'];
        $status = ($value === $correct_api_key) ? '✅ CORRECT' : '❌ WRONG';
        echo "<p><strong>$key:</strong> $value - $status</p>";
    }
    
    echo "<h3 style='color:green;'>✅ All API keys updated with correct MRAM values!</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 again</li>";
echo "<li>Visit <a href='advanced_sms_test.php'>Advanced SMS Test</a> to verify</li>";
echo "</ul>";
?>