<?php

$layoutNav = is_array($layoutNav ?? null) ? $layoutNav : [];
$securityNav = is_array($layoutNav['security.nav'] ?? null) ? $layoutNav['security.nav'] : [];
$canViewChecks = !empty($securityNav['can_view_checks']);
$canManageTemplates = !empty($securityNav['can_manage_templates']);
$canManageSettings = !empty($securityNav['can_manage_settings']);

$checksActive = navActive(['security/checks', 'security/checks/create', 'security/checks/edit'], true);
$templatesActive = navActive(['security/templates', 'security/templates/create', 'security/templates/edit'], true);
$settingsActive = navActive('security/settings', true);
$reportDesignActive = navActive('security/report-design', true);

$securityNavGroups = [
    [
        'key' => 'security-operations',
        'label' => t('Operations'),
        'icon' => 'bi-clipboard-check',
        'items' => [
            [
                'label' => t('Security checks'),
                'path' => 'security/checks',
                'active' => $checksActive,
                'visible' => $canViewChecks,
            ],
        ],
    ],
    [
        'key' => 'security-administration',
        'label' => t('Administration'),
        'icon' => 'bi-sliders',
        'items' => [
            [
                'label' => t('Check templates'),
                'path' => 'security/templates',
                'active' => $templatesActive,
                'visible' => $canManageTemplates,
            ],
            [
                'label' => t('Settings'),
                'path' => 'security/settings',
                'active' => $settingsActive,
                'visible' => $canManageSettings,
            ],
            [
                'label' => t('Report design'),
                'path' => 'security/report-design',
                'active' => $reportDesignActive,
                'visible' => $canManageSettings,
            ],
        ],
    ],
];
?>
<ul>
    <?php foreach ($securityNavGroups as $group):
        $visibleItems = array_values(array_filter($group['items'], static fn (array $item): bool => !empty($item['visible'])));
        if (!$visibleItems) {
            continue;
        }
        $groupIsActive = false;
        foreach ($visibleItems as $item) {
            if (!empty(($item['active'] ?? [])['isActive'])) {
                $groupIsActive = true;
                break;
            }
        }
    ?>
        <li class="app-sidebar-group app-sidebar-admin-group">
            <details data-details-key="<?php e($group['key']); ?>"<?php if ($groupIsActive): ?> open<?php endif; ?>>
                <summary>
                    <i class="bi <?php e($group['icon']); ?>"></i>
                    <span><?php e($group['label']); ?></span>
                </summary>
                <ul>
                    <?php foreach ($visibleItems as $item):
                        $active = $item['active'] ?? ['class' => '', 'aria' => ''];
                    ?>
                        <li>
                            <a href="<?php e(lurl($item['path'])); ?>" class="<?php e($active['class'] ?? ''); ?>" <?php echo $active['aria'] ?? ''; // raw-html-ok: pre-built ARIA attribute from navActive() ?>>
                                <?php e($item['label']); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </details>
        </li>
    <?php endforeach; ?>
</ul>
