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

// Check admin access
if (!isAuthenticated() || !$_SESSION['is_admin']) {
    redirect(APP_URL . '/login.php');
}

$db = getDB();

// Add failed_login_attempts and last_failed_login columns to users table if they don't exist
$stmt = $db->prepare("SHOW COLUMNS FROM users LIKE 'failed_login_attempts'");
$stmt->execute();
$columnExists = $stmt->fetch();

if (!$columnExists) {
    try {
        $db->exec("ALTER TABLE users ADD COLUMN failed_login_attempts INT DEFAULT 0 AFTER last_activity");
        $db->exec("ALTER TABLE users ADD COLUMN last_failed_login DATETIME NULL AFTER failed_login_attempts");
        echo "Added failed_login_attempts and last_failed_login columns to users table<br>";
    } catch (PDOException $e) {
        echo "Error adding failed login columns: " . $e->getMessage() . "<br>";
    }
} else {
    echo "failed_login_attempts and last_failed_login columns already exist in users table<br>";
}

// Add auto_logout_enabled and auto_logout_minutes columns to settings table if they don't exist
$stmt = $db->prepare("SHOW COLUMNS FROM settings LIKE 'auto_logout_enabled'");
$stmt->execute();
$columnExists = $stmt->fetch();

if (!$columnExists) {
    try {
        $db->exec("ALTER TABLE settings ADD COLUMN auto_logout_enabled BOOLEAN DEFAULT 1 AFTER maintenance_mode");
        $db->exec("ALTER TABLE settings ADD COLUMN auto_logout_minutes INT DEFAULT 5 AFTER auto_logout_enabled");
        echo "Added auto_logout_enabled and auto_logout_minutes columns to settings table<br>";
    } catch (PDOException $e) {
        echo "Error adding auto logout columns: " . $e->getMessage() . "<br>";
    }
} else {
    echo "auto_logout_enabled and auto_logout_minutes columns already exist in settings table<br>";
}

// Initialize settings if they don't exist
$stmt = $db->prepare("SELECT COUNT(*) FROM settings WHERE id = 1");
$stmt->execute();
$settingsExist = $stmt->fetchColumn();

if (!$settingsExist) {
    try {
        $db->exec("INSERT INTO settings (id, site_name, site_description, maintenance_mode, auto_logout_enabled, auto_logout_minutes) VALUES (1, 'QuickRe', 'Secure Messaging Platform', 0, 1, 5)");
        echo "Initialized settings table with default values<br>";
    } catch (PDOException $e) {
        echo "Error initializing settings: " . $e->getMessage() . "<br>";
    }
} else {
    echo "Settings table already initialized<br>";
}

// Verify current settings
$stmt = $db->prepare("SELECT auto_logout_enabled, auto_logout_minutes FROM settings WHERE id = 1");
$stmt->execute();
$settings = $stmt->fetch();

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>";
?>
