<?php
session_start();

echo "<h2>Admin Dashboard Debug</h2>";

// Check session data
echo "<h3>Session Data:</h3>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";

// Check individual session variables
echo "<h3>Individual Session Checks:</h3>";
echo "isset(\$_SESSION['user_id']): " . (isset($_SESSION['user_id']) ? 'YES' : 'NO') . "<br>";
echo "!empty(\$_SESSION['user_id']): " . (!empty($_SESSION['user_id']) ? 'YES' : 'NO') . "<br>";
echo "isset(\$_SESSION['logged_in']): " . (isset($_SESSION['logged_in']) ? 'YES' : 'NO') . "<br>";
echo "\$_SESSION['logged_in'] === true: " . (($_SESSION['logged_in'] ?? false) === true ? 'YES' : 'NO') . "<br>";
echo "\$_SESSION['role_name']: " . ($_SESSION['role_name'] ?? 'NOT SET') . "<br>";

// Check the specific condition from your original code
$user_id = $_SESSION['user_id'] ?? 0;
$user_role_id = $_SESSION['role_id'] ?? 0;
$user_role = $_SESSION['role_name'] ?? 'Customer';

echo "<br><h3>Variable Values:</h3>";
echo "user_id: $user_id<br>";
echo "user_role_id: $user_role_id<br>";
echo "user_role: $user_role<br>";

// Check login condition
$is_logged_in = isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
echo "<br><h3>Login Check:</h3>";
echo "is_logged_in: " . ($is_logged_in ? 'YES' : 'NO') . "<br>";

// Check admin privileges
$admin_roles = ['Administrator', 'Support Agent', 'Sales Manager', 'Developer'];
$has_admin_privileges = in_array($user_role, $admin_roles);
echo "has_admin_privileges: " . ($has_admin_privileges ? 'YES' : 'NO') . "<br>";

// Overall check
if (!$is_logged_in) {
    echo "<br>❌ <strong>WOULD REDIRECT TO LOGIN:</strong> User not logged in<br>";
} elseif (!$has_admin_privileges) {
    echo "<br>❌ <strong>WOULD REDIRECT TO DASHBOARD:</strong> User doesn't have admin privileges<br>";
} else {
    echo "<br>✅ <strong>SHOULD ALLOW ACCESS:</strong> User is logged in with admin privileges<br>";
}

// Include database connection and test
echo "<br><h3>Database Connection Test:</h3>";
try {
    require_once 'layouts/dbconn.php';
    if (isset($conn) && !$conn->connect_error) {
        echo "✅ Database connection: SUCCESS<br>";
        
        // Test a simple query
        $stmt = $conn->prepare("SELECT COUNT(*) as count FROM users");
        $stmt->execute();
        $result = $stmt->get_result()->fetch_assoc();
        echo "✅ Database query test: SUCCESS (Found {$result['count']} users)<br>";
        $stmt->close();
    } else {
        echo "❌ Database connection: FAILED<br>";
    }
} catch (Exception $e) {
    echo "❌ Database error: " . $e->getMessage() . "<br>";
}

// Test if admin-dashboard.php exists
echo "<br><h3>File Existence Check:</h3>";
echo "index.php exists: " . (file_exists('index.php') ? 'YES' : 'NO') . "<br>";
echo "dashboard.php exists: " . (file_exists('dashboard.php') ? 'YES' : 'NO') . "<br>";

// Check for any output buffering or headers
echo "<br><h3>Headers and Output:</h3>";
echo "Headers sent: " . (headers_sent() ? 'YES' : 'NO') . "<br>";
if (headers_sent($file, $line)) {
    echo "Headers sent from: $file on line $line<br>";
}

?>

<hr>
<h3>Navigation Test:</h3>
<p><a href="index.php">Try to access admin-dashboard.php</a></p>
<p><a href="dashboard.php">Try to access dashboard.php</a></p>
<p><a href="auth-login.php">Go back to login</a></p>