<?php
// Update all SMS settings with current API key
header('Content-Type: text/html; charset=utf-8');

echo "<h2>Updating All SMS Settings with Current API Key</h2>\n";
echo "<p>This script will update all SMS-related settings to use the current API key: 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";
    }
    
    // Define the current API key
    $current_api_key = 'C30027916975f051ed0e01.45424521';
    $current_username = 'C3002791';
    $current_password = 'hPOWSnw8rf';
    $current_sender_id = '8809601016579';
    $current_api_url = 'https://rest.mrmessaging.net/messages';
    
    // Settings to update
    $settings_to_update = [
        ['non_musking_api_key', $current_api_key, 'Non-musking API Key'],
        ['non_musking_username', $current_username, 'Non-musking Username'],
        ['non_musking_password', $current_password, 'Non-musking Password'],
        ['non_musking_sender_id', $current_sender_id, 'Non-musking Sender ID'],
        ['non_musking_api_url', $current_api_url, 'Non-musking API URL'],
        ['sms_api_key', $current_api_key, 'SMS API Key'],
        ['sms_username', $current_username, 'SMS Username'],
        ['sms_password', $current_password, 'SMS Password'],
        ['sms_sender_id', $current_sender_id, 'SMS Sender ID'],
        ['sms_api_url', $current_api_url, 'SMS API URL'],
    ];
    
    echo "<h3>Updating SMS Settings:</h3>\n";
    echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse;'>\n";
    echo "<tr style='background-color: #f2f2f2;'><th>Setting Key</th><th>Description</th><th>Status</th></tr>\n";
    
    foreach ($settings_to_update as $setting_data) {
        $key = $setting_data[0];
        $value = $setting_data[1];
        $description = $setting_data[2];
        
        // Check current value
        $stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = ?");
        $stmt->execute([$key]);
        $current_result = $stmt->fetch();
        
        if ($current_result) {
            $old_value = $current_result['setting_value'];
            // Update existing setting
            $stmt = $pdo->prepare("UPDATE settings SET setting_value = ? WHERE setting_key = ?");
            $stmt->execute([$value, $key]);
            $status = "🔄 Updated (was: " . htmlspecialchars(substr($old_value, 0, 10)) . "...)";
        } else {
            // Insert new setting
            $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (?, ?)");
            $stmt->execute([$key, $value]);
            $status = "✅ Added";
        }
        
        echo "<tr><td>{$key}</td><td>{$description}</td><td>{$status}</td></tr>\n";
    }
    
    echo "</table>\n";
    
    // Ensure the SMS services are enabled
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('non_musking_enabled', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    $stmt->execute();
    
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('sms_enabled', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    $stmt->execute();
    
    echo "<p>✅ Enabled non-musking and SMS services</p>\n";
    
    // Also ensure bank transaction notifications are enabled
    $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES ('sms_send_bank_transaction_notifications', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    $stmt->execute();
    
    echo "<p>✅ Ensured bank transaction notifications are enabled</p>\n";
    
    // Show all updated settings
    echo "<h3>Verification - All SMS Settings:</h3>\n";
    $stmt = $pdo->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key LIKE '%sms%' OR setting_key LIKE '%api%' OR setting_key LIKE '%non_musking%' 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']);
        $highlight = (strpos($key, 'api_key') !== false) ? 'style="background-color: #ffffcc"' : '';
        echo "<tr {$highlight}><td>{$key}</td><td>{$value}</td></tr>\n";
    }
    
    echo "</table>\n";
    
    echo "<h3>Conclusion:</h3>\n";
    echo "<p><strong>✅ All SMS settings have been updated with the current API key: C30027916975f051ed0e01.45424521</strong></p>\n";
    echo "<p>Both the non-musking SMS service and bank transaction notifications are now configured with the correct API credentials.</p>\n";
    echo "<p>SMS notifications for bank transaction verifications should now work properly.</p>\n";

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