<?php
// Simple database check
require_once '../config/config.php';
require_once '../config/db.php';
require_once 'AdminUser.php';

$adminUser = new AdminUser($pdo);

echo "<h3>Database Status</h3>";

// Check if table exists
if (!$adminUser->tableExists()) {
    echo "Creating admin_users table...<br>";
    $adminUser->createTable();
}

// Check users
$stmt = $pdo->query("SELECT * FROM admin_users");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);

echo "<h4>Existing users:</h4>";
if (empty($users)) {
    echo "No users found. Creating default admin...<br>";
    $adminUser->create('admin', 'admin123');
    
    // Recheck
    $stmt = $pdo->query("SELECT * FROM admin_users");
    $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

foreach ($users as $user) {
    echo "ID: {$user['id']}, Username: {$user['username']}, Avatar: " . ($user['avatar'] ?: 'None') . "<br>";
}

// Test authentication
session_start();
if (!isset($_SESSION['admin_user_id'])) {
    $_SESSION['admin_user_id'] = $users[0]['id'];
    $_SESSION['admin_username'] = $users[0]['username'];
    echo "<br>✅ Session initialized with user ID: " . $_SESSION['admin_user_id'] . "<br>";
} else {
    echo "<br>✅ Session already active with user ID: " . $_SESSION['admin_user_id'] . "<br>";
}
?>
<br><br>
<a href="admin-profile.php" style="background: #007cba; color: white; padding: 10px 20px; text-decoration: none;">Go to Admin Profile</a>