<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

role_module_boot();

$roleRecord = find_role_or_abort((int) ($_GET['id'] ?? 0));
$pageTitle = 'Delete Role';
$isProtectedRole = is_system_super_admin_role($roleRecord);
$roleInUse = role_is_in_use((int) $roleRecord['id']);

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">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Roles</p>
            <h2 class="h4 mb-0">Delete role</h2>
        </div>
    </div>
    <p>You are about to delete <strong><?php echo e($roleRecord['name']); ?></strong>.</p>
    <?php if ($isProtectedRole): ?>
        <div class="alert alert-warning">The Super Admin role is system-protected and cannot be deleted.</div>
    <?php elseif ($roleInUse): ?>
        <div class="alert alert-warning">This role is currently assigned to users and cannot be deleted until it is no longer in use.</div>
    <?php else: ?>
        <form method="post" action="<?php echo e(base_url('app/modules/roles/actions.php')); ?>">
            <?php echo csrf_input(); ?>
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="id" value="<?php echo e((string) $roleRecord['id']); ?>">
            <button class="btn btn-danger" type="submit">Delete role</button>
        </form>
    <?php endif; ?>
    <a class="btn btn-light mt-3" href="<?php echo e(base_url('app/modules/roles/view.php?id=' . (int) $roleRecord['id'])); ?>">Back</a>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>