<?php
/**
 * Debug Role Check - Temporary file to check role status
 */

require_once __DIR__ . '/../includes/init.php';
requireLogin();

echo "<h2>Role Debug Information</h2>";
echo "<pre>";
echo "User ID: " . ($_SESSION['user_id'] ?? 'NOT SET') . "\n";
echo "Role ID: " . ($_SESSION['role_id'] ?? 'NOT SET') . "\n";
echo "Role Code (Session): " . ($_SESSION['role_code'] ?? 'NOT SET') . "\n";
echo "Role Name: " . ($_SESSION['role_name'] ?? 'NOT SET') . "\n";
echo "\n";

// Check from database
if (isset($_SESSION['user_id'])) {
    try {
        $db = getDB();
        $stmt = $db->prepare("
            SELECT u.user_id, u.username, r.role_id, r.role_code, r.role_name
            FROM users u
            INNER JOIN roles r ON u.role_id = r.role_id
            WHERE u.user_id = ?
        ");
        $stmt->execute([$_SESSION['user_id']]);
        $user = $stmt->fetch();
        
        if ($user) {
            echo "Database Check:\n";
            echo "User ID: " . $user['user_id'] . "\n";
            echo "Username: " . $user['username'] . "\n";
            echo "Role ID: " . $user['role_id'] . "\n";
            echo "Role Code: " . $user['role_code'] . "\n";
            echo "Role Name: " . $user['role_name'] . "\n";
            echo "\n";
            echo "Is Super Admin? " . ($user['role_code'] === 'super_admin' ? 'YES' : 'NO') . "\n";
        }
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
}

echo "\nFunction Checks:\n";
echo "isSuperAdmin(): " . (isSuperAdmin() ? 'TRUE' : 'FALSE') . "\n";
echo "canCreateCustomRoles(): " . (canCreateCustomRoles() ? 'TRUE' : 'FALSE') . "\n";
echo "</pre>";

echo "<br><a href='" . BASE_URL . "/modules/settings/index.php'>Back to Settings</a>";

