<?php
// debug_menu.php - Create this file to test your menu
session_start();
include 'layouts/dbconn.php';

// First, let's check what's in your session
echo "<h2>Session Debug:</h2>";
echo "<pre>";
print_r($_SESSION);
echo "</pre>";

echo "<h2>Database Role Check:</h2>";
if (isset($_SESSION['user_id'])) {
    $userId = $_SESSION['user_id'];
    
    // Get the exact role from database
    $sql = "SELECT u.user_id, u.username, u.full_name, ur.role_id, ur.role_name 
            FROM users u 
            INNER JOIN user_roles ur ON u.role_id = ur.role_id 
            WHERE u.user_id = ?";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("i", $userId);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        echo "<strong>Database User Info:</strong><br>";
        echo "User ID: " . $user['user_id'] . "<br>";
        echo "Username: " . $user['username'] . "<br>";
        echo "Full Name: " . $user['full_name'] . "<br>";
        echo "Role ID: " . $user['role_id'] . "<br>";
        echo "Role Name: '" . $user['role_name'] . "'<br>";
        
        // Update session with correct role name if needed
        $_SESSION['role_name'] = $user['role_name'];
        echo "<br><strong>Session updated with role: " . $user['role_name'] . "</strong><br>";
    }
}

// Now test the menu system
echo "<hr><h2>Testing Menu System:</h2>";

// Include the menu configuration
if (file_exists('cargo_menu_config.php')) {
    include 'cargo_menu_config.php';
    echo "✓ Menu config file loaded<br>";
} else {
    echo "✗ Menu config file NOT found - make sure 'cargo_menu_config.php' exists<br>";
    echo "Current directory: " . getcwd() . "<br>";
    exit;
}

// Check if functions exist
$functions = ['getMenuConfig', 'renderMenu', 'getUserAssignedPages'];
foreach ($functions as $func) {
    echo ($function_exists($func) ? "✓" : "✗") . " Function $func exists<br>";
}

// Test menu generation
$userRole = $_SESSION['role_name'] ?? 'guest';
$userId = $_SESSION['user_id'] ?? null;

echo "<br><strong>Current Role:</strong> '$userRole'<br>";
echo "<strong>User ID:</strong> $userId<br>";

if (function_exists('getMenuConfig')) {
    echo "<br><h3>Menu Configuration Test:</h3>";
    $menuConfig = getMenuConfig($userRole, [], $conn, $userId);
    
    echo "Number of menu items: " . count($menuConfig) . "<br>";
    
    echo "<details><summary>Click to see full menu config</summary>";
    echo "<pre>" . print_r($menuConfig, true) . "</pre>";
    echo "</details>";
    
    echo "<br><h3>Rendered Menu:</h3>";
    echo "<div style='border: 1px solid #ccc; padding: 10px; background: #f9f9f9;'>";
    
    if (function_exists('renderMenu')) {
        $renderedMenu = renderMenu($userRole, $userId, $conn, true);
        echo $renderedMenu;
    } else {
        echo "renderMenu function not found!";
    }
    
    echo "</div>";
    
} else {
    echo "getMenuConfig function not found!";
}

// Test specific role menus
echo "<br><h3>Direct Role Test:</h3>";
if (function_exists('getMenuConfig')) {
    $adminMenu = getMenuConfig('Administrator', ['*'], $conn, $userId);
    echo "Administrator menu items: " . count($adminMenu) . "<br>";
    
    // Show first few items
    echo "<strong>First 5 menu items for Administrator:</strong><br>";
    for ($i = 0; $i < min(5, count($adminMenu)); $i++) {
        echo ($i + 1) . ". " . $adminMenu[$i]['title'] . " (" . $adminMenu[$i]['type'] . ")<br>";
    }
}
?>

<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h2 { color: #333; border-bottom: 2px solid #ccc; }
h3 { color: #666; }
pre { background: #f5f5f5; padding: 10px; border-radius: 4px; }
details { margin: 10px 0; }
</style>