<?php
// Update the non_musking_api_key to the current one
header('Content-Type: text/html; charset=utf-8');

echo "<h2>Updating Non-Musking API Key</h2>\n";
echo "<p>This script will update the non_musking_api_key to the current value: C30027916975f051ed0e01.45424521</p>\n";

// Database configuration (same as in config/database.php)
$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 "<p>✅ Successfully connected to database: {$dbname}</p>\n";
    
    // Check if settings table exists
    $stmt = $pdo->prepare("SHOW TABLES LIKE 'settings'");
    $stmt->execute();
    $tableExists = $stmt->fetch();
    
    if (!$tableExists) {
        echo "<p>❌ Settings table does not exist</p>\n";
        exit;
    } else {
        echo "<p>✅ Settings table exists</p>\n";
    }
    
    // Get the current API key value
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_api_key'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result) {
        $old_api_key = $result['setting_value'];
        echo "<p>📋 Current API key in database: " . htmlspecialchars($old_api_key) . "</p>\n";
    } else {
        echo "<p>⚠️ API key not found in database</p>\n";
        $old_api_key = 'NOT_FOUND';
    }
    
    // Update the API key to the current one
    $new_api_key = 'C30027916975f051ed0e01.45424521';
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_api_key', ?) ON DUPLICATE KEY UPDATE setting_value = ?");
    $stmt->execute([$new_api_key, $new_api_key]);
    
    echo "<p>✅ API key updated successfully!</p>\n";
    echo "<p>🔄 Changed from: " . htmlspecialchars($old_api_key) . "</p>\n";
    echo "<p>🔄 Changed to: " . htmlspecialchars($new_api_key) . "</p>\n";
    
    // Also update the sms_api_key if it exists
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'sms_api_key'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result) {
        $old_sms_api_key = $result['setting_value'];
        echo "<p>📋 Current sms_api_key in database: " . htmlspecialchars($old_sms_api_key) . "</p>\n";
        
        $stmt = $pdo->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = 'sms_api_key'");
        $stmt->execute([$new_api_key]);
        
        echo "<p>✅ sms_api_key updated to the same value</p>\n";
    }
    
    // Verify the update
    $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'non_musking_api_key'");
    $stmt->execute();
    $result = $stmt->fetch();
    
    if ($result && $result['setting_value'] === $new_api_key) {
        echo "<p>✅ <strong>Verification successful:</strong> API key has been updated to the current value</p>\n";
    } else {
        echo "<p>❌ <strong>Verification failed:</strong> API key was not updated correctly</p>\n";
    }
    
    echo "<h3>Other Related SMS Settings:</h3>\n";
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%api_key%' OR setting_key LIKE '%api%' ORDER BY setting_key");
    $stmt->execute();
    $results = $stmt->fetchAll();
    
    echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>\n";
    echo "<tr style='background-color: #f2f2f2;'><th>Setting Key</th><th>Setting Value</th></tr>\n";
    
    foreach ($results as $row) {
        $key = htmlspecialchars($row['setting_key']);
        $value = htmlspecialchars($row['setting_value']);
        echo "<tr><td>{$key}</td><td>{$value}</td></tr>\n";
    }
    
    echo "</table>\n";
    
    echo "<h3>Conclusion:</h3>\n";
    echo "<p><strong>✅ API key has been successfully updated to the current value: C30027916975f051ed0e01.45424521</strong></p>\n";
    echo "<p>The SMS service should now use the correct API key for authentication.</p>\n";

} catch (PDOException $e) {
    echo "<p>❌ Database connection error: " . htmlspecialchars($e->getMessage()) . "</p>\n";
}
?>