<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

user_module_boot();

$userRecord = find_user_or_abort((int) ($_GET['id'] ?? 0));
$pageTitle = 'Delete User';
$isProtectedLastSuperAdmin = $userRecord['role_name'] === 'Super Admin' && active_super_admin_count((int) $userRecord['id']) === 0;

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">Users</p>
            <h2 class="h4 mb-0">Delete user</h2>
        </div>
    </div>
    <p>You are about to remove <strong><?php echo e($userRecord['full_name']); ?></strong> from the JLFC backend.</p>
    <?php if ($isProtectedLastSuperAdmin): ?>
        <div class="alert alert-warning">This user is the last active Super Admin and cannot be deleted.</div>
        <a class="btn btn-light" href="<?php echo e(base_url('app/modules/users/view.php?id=' . (int) $userRecord['id'])); ?>">Back</a>
    <?php else: ?>
        <form method="post" action="<?php echo e(base_url('app/modules/users/actions.php')); ?>">
            <?php echo csrf_input(); ?>
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="id" value="<?php echo e((string) $userRecord['id']); ?>">
            <button class="btn btn-danger" type="submit">Delete user</button>
            <a class="btn btn-light" href="<?php echo e(base_url('app/modules/users/view.php?id=' . (int) $userRecord['id'])); ?>">Cancel</a>
        </form>
    <?php endif; ?>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>