<?php
require_once '../includes/init.php';

// Check admin access
if (!isAuthenticated() || !$_SESSION['is_admin']) {
    jsonResponse(false, 'Admin access required');
    exit;
}

$db = getDB();

// Execute SQL commands directly
try {
    // Add failed login tracking columns to users table
    $db->exec("ALTER TABLE users ADD COLUMN IF NOT EXISTS failed_login_attempts INT DEFAULT 0 AFTER last_activity");
    echo "Added failed_login_attempts column to users table<br>";
    
    $db->exec("ALTER TABLE users ADD COLUMN IF NOT EXISTS last_failed_login DATETIME NULL AFTER failed_login_attempts");
    echo "Added last_failed_login column to users table<br>";
    
    // Add auto logout settings to settings table (key-value format)
    $db->exec("INSERT INTO settings (setting_key, setting_value) VALUES ('auto_logout_enabled', '1') ON DUPLICATE KEY UPDATE setting_value = '1'");
    echo "Added auto_logout_enabled setting to settings table<br>";
    
    $db->exec("INSERT INTO settings (setting_key, setting_value) VALUES ('auto_logout_minutes', '5') ON DUPLICATE KEY UPDATE setting_value = '5'");
    echo "Added auto_logout_minutes setting to settings table<br>";
    
    // Verify current settings
    $stmt = $db->prepare("SELECT setting_key, setting_value FROM settings WHERE setting_key IN ('auto_logout_enabled', 'auto_logout_minutes')");
    $stmt->execute();
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
    
    if ($settings) {
        echo "<br><strong>Current Settings:</strong><br>";
        echo "Auto-Logout Enabled: " . ($settings['auto_logout_enabled'] ? 'Yes' : 'No') . "<br>";
        echo "Auto-Logout Minutes: " . $settings['auto_logout_minutes'] . " minutes<br>";
    }
    
    echo "<br><br>";
    echo "<a href='auto-logout-settings.php'>Go to Auto-Logout Settings</a>";
    echo " | ";
    echo "<a href='dashboard.php'>Go to Dashboard</a>';
    
} catch (PDOException $e) {
    echo "Error updating database schema: " . $e->getMessage() . "<br>";
    echo "Please check your database connection and permissions.";
}
?>
