<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

role_module_boot();

$roleRecord = find_role_or_abort((int) ($_GET['id'] ?? 0));
$permissionIds = role_permission_ids((int) $roleRecord['id']);
$permissions = $permissionIds === []
    ? []
    : db_fetch_all(
        'SELECT permission_name, module_name FROM permissions WHERE id IN (' . implode(',', array_map('intval', $permissionIds)) . ') ORDER BY module_name ASC, permission_name ASC'
    );
$assignedUsers = db_fetch_all('SELECT id, full_name, email, status FROM users WHERE role_id = :role_id ORDER BY full_name ASC', ['role_id' => $roleRecord['id']]);
$pageTitle = 'View Role';

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Role profile</p>
            <h2 class="h4 mb-0"><?php echo e($roleRecord['name']); ?></h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/roles/edit.php?id=' . (int) $roleRecord['id'])); ?>">Edit</a>
            <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/roles/delete.php?id=' . (int) $roleRecord['id'])); ?>">Delete</a>
        </div>
    </div>
    <p class="text-muted mb-0"><?php echo e($roleRecord['description'] ?: 'No description provided.'); ?></p>
</section>

<section class="row g-4">
    <div class="col-xl-7">
        <div class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <h3 class="h5 mb-0">Assigned permissions</h3>
            </div>
            <?php if (is_system_super_admin_role($roleRecord)): ?>
                <div class="alert alert-info mb-0">Super Admin bypasses all permission checks and retains full access automatically.</div>
            <?php elseif ($permissions === []): ?>
                <p class="text-muted mb-0">No permissions are assigned to this role yet.</p>
            <?php else: ?>
                <div class="row g-2">
                    <?php foreach ($permissions as $permission): ?>
                        <div class="col-md-6">
                            <div class="feature-tile"><i class="bi bi-shield-check"></i>
                                <div>
                                    <h4><?php echo e($permission['permission_name']); ?></h4>
                                    <p><?php echo e(ucwords(str_replace('_', ' ', $permission['module_name']))); ?></p>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
    <div class="col-xl-5">
        <div class="panel-card h-100">
            <div class="panel-card-header mb-3">
                <h3 class="h5 mb-0">Users in role</h3>
            </div>
            <?php if ($assignedUsers === []): ?>
                <p class="text-muted mb-0">No users are currently assigned to this role.</p>
            <?php else: ?>
                <ul class="status-list mb-0">
                    <?php foreach ($assignedUsers as $assignedUser): ?>
                        <li>
                            <span>
                                <strong><?php echo e($assignedUser['full_name']); ?></strong><br>
                                <small class="text-muted"><?php echo e($assignedUser['email']); ?></small>
                            </span>
                            <span class="badge <?php echo e($assignedUser['status'] === 'active' ? 'badge-soft-success' : 'badge-soft-danger'); ?>"><?php echo e(ucfirst($assignedUser['status'])); ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>