<?php
/**
 * Admin — Role Management
 */
if (!hasRole('admin')) {
    header('Location: ../dashboard.php?error=forbidden');
    exit;
}

$db = getDB();
$roleCounts = [];
foreach (ROLES as $key => $info) {
    $stmt = $db->prepare("SELECT COUNT(*) FROM users WHERE role = :role AND status = 'active'");
    $stmt->execute([':role' => $key]);
    $roleCounts[$key] = (int)$stmt->fetchColumn();
}
?>

<div class="page-header">
    <h1>Roles &amp; Permissions</h1>
    <a href="dashboard.php?page=admin" class="btn btn-sm btn-secondary">&larr; Back to Admin</a>
</div>

<div class="card" style="margin-bottom:24px;">
    <div class="card-header">
        <h2 class="card-title">Role Hierarchy</h2>
    </div>
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th>Level</th>
                    <th>Role</th>
                    <th>Label</th>
                    <th>Active Users</th>
                    <th>Permissions</th>
                </tr>
            </thead>
            <tbody>
            <?php
            $sortedRoles = ROLES;
            uasort($sortedRoles, fn($a, $b) => $b['level'] - $a['level']);
            foreach ($sortedRoles as $key => $info):
            ?>
                <tr>
                    <td><span class="badge badge-blue"><?= $info['level'] ?></span></td>
                    <td><code><?= htmlspecialchars($key) ?></code></td>
                    <td><strong><?= htmlspecialchars($info['label']) ?></strong></td>
                    <td><?= $roleCounts[$key] ?? 0 ?></td>
                    <td style="font-size:13px; color:var(--ios-secondary);">
                        <?php
                        $perms = [];
                        if ($info['level'] >= 9) $perms[] = 'Full system access';
                        if ($info['level'] >= 6) $perms[] = 'Admin panel';
                        if ($info['level'] >= 5) $perms[] = 'Approve users & OT';
                        if ($info['level'] >= 4) $perms[] = 'View all OT records';
                        if ($info['level'] >= 3) $perms[] = 'Standard portal access';
                        if ($info['level'] >= 2) $perms[] = 'HR/Finance reports';
                        if ($info['level'] >= 1) $perms[] = 'Read-only access';
                        echo implode(' &middot; ', $perms);
                        ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<div class="card">
    <div class="card-header">
        <h2 class="card-title">Module Access Matrix</h2>
    </div>
    <div class="table-wrapper">
        <table>
            <thead>
                <tr>
                    <th>Module</th>
                    <?php foreach ($sortedRoles as $key => $info): ?>
                        <th style="text-align:center; font-size:11px;"><?= htmlspecialchars($info['label']) ?></th>
                    <?php endforeach; ?>
                </tr>
            </thead>
            <tbody>
            <?php
            $modules = [
                'Dashboard Home' => 1,
                'Office Dashboard' => 3,
                'Overtime Taskforce' => 3,
                'Department Calendar' => 1,
                'Active Incidents' => 3,
                'ESO' => 3,
                'Target Solutions' => 3,
                'Admin Panel' => 5,
                'User Management' => 5,
            ];
            foreach ($modules as $modName => $minLevel): ?>
                <tr>
                    <td><strong><?= $modName ?></strong></td>
                    <?php foreach ($sortedRoles as $key => $info): ?>
                        <td style="text-align:center;">
                            <?php if ($info['level'] >= $minLevel): ?>
                                <span style="color:var(--ios-green); font-weight:700;">&#10003;</span>
                            <?php else: ?>
                                <span style="color:var(--ios-tertiary);">&mdash;</span>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
