<?php
$db = mysqli_connect('localhost','brenopai_advteste','qaR2Pd751U','brenopai_advteste');
echo '<pre>';

// User roles
echo "=== user_role table ===\n";
$r = mysqli_query($db,"SELECT * FROM user_role");
while($row=mysqli_fetch_assoc($r)) echo "  id:{$row['id']} name:{$row['name']}\n";

// rel_role_action - what actions each role has
echo "\n=== rel_role_action (por role) ===\n";
$r2 = mysqli_query($db,"SELECT ur.name as role, ra.action_id, a.name as action_name 
    FROM rel_role_action ra 
    LEFT JOIN user_role ur ON ur.id=ra.role_id 
    LEFT JOIN actions a ON a.id=ra.action_id
    ORDER BY ra.role_id, ra.action_id");
$current_role = '';
while($row=mysqli_fetch_assoc($r2)) {
    if($row['role']!==$current_role) { echo "\n  [{$row['role']}]\n"; $current_role=$row['role']; }
    echo "    {$row['action_id']}: {$row['action_name']}\n";
}

// Actions related to financeiro
echo "\n=== actions com 'financ' ou 'report' ===\n";
$r3 = mysqli_query($db,"SELECT id, name FROM actions WHERE name LIKE '%financ%' OR name LIKE '%report%' OR name LIKE '%relat%'");
while($row=mysqli_fetch_assoc($r3)) echo "  {$row['id']}: {$row['name']}\n";

// Check users
echo "\n=== users e roles ===\n";
$r4 = mysqli_query($db,"SELECT u.name, u.username, ur.name as role FROM users u LEFT JOIN user_role ur ON ur.id=u.user_role");
while($row=mysqli_fetch_assoc($r4)) echo "  {$row['name']} ({$row['username']}): {$row['role']}\n";

echo '</pre>';
