<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
iscms_require_roles(['SUPER_ADMIN', 'ADMIN']);

$pageTitle = 'Permissions catalogue';
$activeNav = 'users';

$sql = "SELECT id, permission_key, description, module, action, is_active
        FROM permissions
        ORDER BY module ASC, permission_key ASC
        LIMIT 2000";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Permissions catalogue</h1>
        <p>All permission keys that can be granted to roles.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="role_permissions.php"><i class="ri-key-2-line"></i> Role permissions</a>
            <a class="qa-btn" href="user_roles.php"><i class="ri-shield-user-line"></i> Roles</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-lock-unlock-line"></i> Permissions</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Key</th>
                        <th>Module</th>
                        <th>Action</th>
                        <th>Active</th>
                        <th>Description</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="5"><i class="ri-inbox-line me-2"></i>No permissions</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600"><small><?= iscms_h($r['permission_key']) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['module'] ?? '')) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['action'] ?? '')) ?></small></td>
                            <td><?= !empty($r['is_active']) ? '<span class="badge bg-success-subtle text-success" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><small><?= iscms_val($r['description'] ?? null) ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
