<?php
/** @var string $url Current URL for active state */
/** @var string $role Session role name */
$role = session()->get('user_role') ?? 'client';
$rbac = $rbac ?? new \App\Services\RbacService(\Config\Database::connect());

$show = fn (string $perm) => $rbac->can($role, $perm);

$link = function (string $href, string $icon, string $label, string $perm, ?string $match = null) use ($url, $show) {
    if (!$show($perm)) {
        return '';
    }
    $match = $match ?? $href;
    $active = str_contains($url, $match) ? 'active' : '';

    return '<a href="' . base_url($href) . '" class="nav-link-item ' . $active . '"><i class="bi ' . $icon . '"></i>' . esc($label) . '</a>';
};

$section = fn (string $label) => '<div class="nav-section-label">' . esc($label) . '</div>';
?>

<?= $section('Dashboard') ?>
<?= $link('dashboard', 'bi-speedometer2', 'Overview', 'dashboard') ?>
<?= $link('dashboard/kpi', 'bi-graph-up-arrow', 'KPI Analytics', 'dashboard.kpi', 'dashboard/kpi') ?>

<?php if ($show('helpdesk')): ?>
<?= $section('Help Desk') ?>
<?= $link('helpdesk', 'bi-headset', 'All Tickets', 'helpdesk') ?>
<?= $link('helpdesk?status=pending', 'bi-inbox', 'New / Pending', 'helpdesk', 'status=pending') ?>
<?= $link('helpdesk?status=reviewed', 'bi-check2-square', 'Verified', 'helpdesk', 'status=reviewed') ?>
<?= $link('helpdesk?status=rejected', 'bi-x-circle', 'Rejected', 'helpdesk', 'status=rejected') ?>
<?= $link('helpdesk?status=converted', 'bi-arrow-right-circle', 'Converted', 'helpdesk', 'status=converted') ?>
<?php endif; ?>

<?php if ($show('workorders')): ?>
<?= $section('Work Orders') ?>
<?= $link('workorders/create', 'bi-plus-circle', 'Create', 'workorders', 'workorders/create') ?>
<?= $link('workorders', 'bi-tools', 'Active', 'workorders', 'workorders') ?>
<?= $link('workorders?status=assigned', 'bi-person-check', 'Assigned', 'workorders', 'status=assigned') ?>
<?= $link('workorders?status=completed', 'bi-check-circle', 'Completed', 'workorders', 'status=completed') ?>
<?= $link('workorders/schedule', 'bi-calendar-check', 'PM Schedule', 'workorders', 'schedule') ?>
<?php endif; ?>

<?php if ($show('job-cards')): ?>
<?= $section('Job Cards') ?>
<?= $link('job-cards', 'bi-card-checklist', 'Active Jobs', 'job-cards') ?>
<?= $link('job-cards?status=completed', 'bi-check2-all', 'Completed', 'job-cards', 'status=completed') ?>
<?php endif; ?>

<?php if ($show('estimations')): ?>
<?= $section('Estimations') ?>
<?= $link('estimations', 'bi-calculator', 'All Estimations', 'estimations') ?>
<?= $link('estimations/create', 'bi-plus-lg', 'New Estimation', 'estimations', 'estimations/create') ?>
<?php endif; ?>

<?php if ($show('inventory')): ?>
<?= $section('Inventory') ?>
<?= $link('inventory', 'bi-box-seam', 'Stock', 'inventory') ?>
<?= $link('inventory/movement', 'bi-arrow-left-right', 'Movements', 'inventory', 'movement') ?>
<?php endif; ?>

<?php if ($show('procurement')): ?>
<?= $section('Procurement') ?>
<?= $link('procurement', 'bi-cart', 'Dashboard', 'procurement') ?>
<?= $link('procurement/request/create', 'bi-file-earmark-plus', 'New PR', 'procurement', 'procurement/request') ?>
<?= $link('procurement/rfq/create', 'bi-envelope-paper', 'New RFQ', 'procurement', 'procurement/rfq') ?>
<?= $link('procurement/order/create', 'bi-receipt', 'New PO', 'procurement', 'procurement/order') ?>
<?= $link('vendors', 'bi-truck', 'Vendors', 'vendors') ?>
<?php endif; ?>

<?php if ($show('finance') || $show('finance.invoices')): ?>
<?= $section('Finance') ?>
<?= $link('finance', 'bi-graph-up', 'Overview', 'finance') ?>
<?php if ($show('finance.invoices')): ?>
<?= $link('finance/invoices', 'bi-receipt', 'Invoices', 'finance.invoices') ?>
<?= $link('finance/payments', 'bi-cash-coin', 'Payments', 'finance.payments') ?>
<?= $link('finance/ledger', 'bi-journal-text', 'Ledger', 'finance.ledger') ?>
<?php endif; ?>
<?php if ($show('finance.expenses')): ?>
<?= $link('finance/expenses', 'bi-credit-card', 'Expenses', 'finance.expenses') ?>
<?php endif; ?>
<?php if ($show('finance.petty_cash')): ?>
<?= $link('finance/petty-cash', 'bi-wallet', 'Petty Cash', 'finance.petty_cash') ?>
<?= $link('finance/petty-cash?status=pending', 'bi-hourglass', 'PC — Pending', 'finance.petty_cash', 'status=pending') ?>
<?= $link('finance/petty-cash?status=issued', 'bi-cash', 'PC — Issued', 'finance.petty_cash', 'status=issued') ?>
<?= $link('finance/petty-cash?status=reconciliation', 'bi-clipboard-check', 'PC — Reconciliation', 'finance.petty_cash', 'status=reconciliation') ?>
<?php endif; ?>
<?php if ($show('finance.reimbursements')): ?>
<?= $link('finance/reimbursements', 'bi-arrow-return-left', 'Reimbursements', 'finance.reimbursements') ?>
<?php endif; ?>
<?php if ($show('finance.contracts')): ?>
<?= $link('finance/contracts', 'bi-file-earmark-text', 'Contracts', 'finance.contracts') ?>
<?php endif; ?>
<?php endif; ?>

<?php if ($show('facilities')): ?>
<?= $section('Properties') ?>
<?= $link('facilities', 'bi-building', 'Facilities', 'facilities') ?>
<?= $link('asset-register', 'bi-cpu', 'Assets', 'assets') ?>
<?php endif; ?>

<?php if ($show('employees')): ?>
<?= $section('Workforce') ?>
<?= $link('employees', 'bi-people', 'Employees', 'employees') ?>
<?= $link('employees/attendance', 'bi-clock-history', 'Attendance', 'employees', 'attendance') ?>
<?php endif; ?>

<?php if ($show('costing')): ?>
<?= $section('Costing') ?>
<?= $link('costing', 'bi-currency-exchange', 'Maintenance Costing', 'costing') ?>
<?php endif; ?>

<?php if ($show('compliance') || $show('utility')): ?>
<?= $section('Monitoring') ?>
<?= $link('utility', 'bi-lightning-charge', 'Utility & Energy', 'utility') ?>
<?= $link('compliance', 'bi-shield-check', 'Compliance', 'compliance') ?>
<?php endif; ?>

<?php if ($show('ai')): ?>
<?= $section('AI Insights') ?>
<?= $link('ai', 'bi-robot', 'AI Dashboard', 'ai') ?>
<?= $link('ai/predictive', 'bi-graph-up', 'Predictive', 'ai', 'ai/predictive') ?>
<?= $link('ai/risk', 'bi-shield-exclamation', 'Risk Analysis', 'ai', 'ai/risk') ?>
<?php endif; ?>

<?php if ($show('reports')): ?>
<?= $section('Reports') ?>
<?= $link('reports', 'bi-bar-chart-line', 'Reports Hub', 'reports') ?>
<?= $link('reports/kpi', 'bi-speedometer', 'KPI Analytics', 'reports.kpi', 'reports/kpi') ?>
<?= $link('reports/finance', 'bi-cash-stack', 'Finance Reports', 'reports.finance', 'reports/finance') ?>
<?= $link('reports/workorders', 'bi-tools', 'Work Orders', 'reports', 'reports/workorders') ?>
<?= $link('reports/sla', 'bi-clock', 'SLA Report', 'reports', 'reports/sla') ?>
<?php endif; ?>

<?php if ($show('settings.users') || $show('settings.companies') || $show('settings.roles')): ?>
<?= $section('System') ?>
<?= $link('settings', 'bi-gear', 'Settings', 'settings.users') ?>
<?= $link('settings/users', 'bi-person-badge', 'Users', 'settings.users', 'settings/users') ?>
<?= $link('settings/companies', 'bi-buildings', 'Companies', 'settings.companies', 'settings/companies') ?>
<?= $link('settings/roles', 'bi-shield-lock', 'Roles & Permissions', 'settings.roles', 'settings/roles') ?>
<?= $link('settings/workflow', 'bi-diagram-3', 'Workflow Config', 'settings.workflow', 'settings/workflow') ?>
<?= $link('settings/activity-log', 'bi-activity', 'Activity Log', 'settings.activity', 'activity-log') ?>
<?= $link('settings/login-history', 'bi-door-open', 'Login History', 'settings.login_history', 'login-history') ?>
<?php endif; ?>

<?= $section('Account') ?>
<?= $link('profile', 'bi-person-circle', 'My Profile', 'profile') ?>
<?= $link('notifications', 'bi-bell', 'Notifications', 'notifications', 'notifications') ?>
