<?php
$path = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?? '';
$active = static fn(string $prefix): string => str_starts_with($path, $prefix) ? ' active' : '';
$icon = static function(string $name): string {
    $paths = [
        'dashboard'=>'<rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><rect x="14" y="14" width="7" height="7" rx="1"/>',
        'patients'=>'<circle cx="9" cy="8" r="3"/><path d="M3.5 19a5.5 5.5 0 0 1 11 0"/><path d="M16 6.5a3 3 0 0 1 0 5.5M17 16a4 4 0 0 1 3 3"/>',
        'therapists'=>'<circle cx="12" cy="8" r="3"/><path d="M5 20a7 7 0 0 1 14 0"/>',
        'sessions'=>'<circle cx="12" cy="12" r="8.5"/><path d="M12 7v5l3 2"/>',
        'assessments'=>'<rect x="5" y="3" width="14" height="18" rx="2"/><path d="M8 8h8M8 12h8M8 16h5"/>',
        'reports'=>'<path d="M6 3h9l3 3v15H6z"/><path d="M15 3v4h4M9 12h6M9 16h6"/>',
        'plans'=>'<rect x="4" y="4" width="16" height="16" rx="2"/><path d="m8 12 2.5 2.5L16 9"/>',
        'services'=>'<circle cx="12" cy="12" r="8.5"/><path d="M12 7v10M7 12h10"/>',
        'invoices'=>'<path d="M6 3h12v18l-6-3-6 3z"/><path d="M9 8h6M9 12h6"/>',
        'payments'=>'<rect x="3" y="6" width="18" height="12" rx="2"/><path d="M3 10h18M7 15h3"/>',
        'users'=>'<circle cx="12" cy="8" r="3"/><path d="M5 20a7 7 0 0 1 14 0"/>',
        'audit'=>'<circle cx="12" cy="12" r="8.5"/><path d="M12 7v5l3 2"/>',
        'settings'=>'<path d="M12 3v3M12 18v3M3 12h3M18 12h3M5.6 5.6l2.1 2.1M16.3 16.3l2.1 2.1M18.4 5.6l-2.1 2.1M7.7 16.3l-2.1 2.1"/><circle cx="12" cy="12" r="3.5"/>',
        'profile'=>'<circle cx="12" cy="8" r="3"/><path d="M5 20a7 7 0 0 1 14 0"/>'
    ];
    return '<svg class="nav-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'.($paths[$name] ?? $paths['dashboard']).'</svg>';
};
?>
<aside class="sidebar" data-sidebar>
    <div class="sidebar-brand"><span class="sidebar-brand-mark">GT</span><div><strong>Grow Tool</strong><small>Therapies CRM</small></div></div>
    <nav aria-label="Primary navigation">
        <a class="nav-item<?= $active('/crm/public/') === ' active' && $path === '/crm/public/' ? ' active' : '' ?>" href="/crm/public/"><?= $icon('dashboard') ?><span>Dashboard</span></a>
        <?php if (\App\Core\Auth::hasPermission('patients.view') && !\App\Core\Auth::hasRole('THERAPIST')): ?><a class="nav-item<?= $active('/crm/public/patients') ?>" href="/crm/public/patients"><?= $icon('patients') ?><span>Patients</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('therapists.view')): ?><a class="nav-item<?= $active('/crm/public/therapists') ?>" href="/crm/public/therapists"><?= $icon('therapists') ?><span>Therapists</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('sessions.view')): ?><a class="nav-item<?= $active('/crm/public/sessions') ?>" href="/crm/public/sessions"><?= $icon('sessions') ?><span>Sessions</span></a><?php endif; ?><?php if (\App\Core\Auth::hasPermission('appointments.view')): ?><a class="nav-item<?= $active('/crm/public/appointments') ?>" href="/crm/public/appointments"><?= $icon('sessions') ?><span>Appointments</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('assessments.view')): ?><a class="nav-item<?= $active('/crm/public/assessments') ?>" href="/crm/public/assessments"><?= $icon('assessments') ?><span>Assessments</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('reports.view')): ?><a class="nav-item<?= $active('/crm/public/reports') ?>" href="/crm/public/reports"><?= $icon('reports') ?><span>Reports</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('treatment_plans.view')): ?><a class="nav-item<?= $active('/crm/public/treatment-plans') ?>" href="/crm/public/treatment-plans"><?= $icon('plans') ?><span>Treatment Plans</span></a><?php endif; ?>
        <div class="nav-section">Billing</div>
        <?php if (\App\Core\Auth::hasPermission('services.view')): ?><a class="nav-item<?= $active('/crm/public/services') ?>" href="/crm/public/services"><?= $icon('services') ?><span>Services</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('invoice.view')): ?><a class="nav-item<?= $active('/crm/public/invoices') ?>" href="/crm/public/invoices"><?= $icon('invoices') ?><span>Invoices</span></a><a class="nav-item<?= $active('/crm/public/billing-rates') ?>" href="/crm/public/billing-rates"><?= $icon('services') ?><span>Patient Rates</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('payment.view')): ?><a class="nav-item<?= $active('/crm/public/payments') ?>" href="/crm/public/payments"><?= $icon('payments') ?><span>Payments & Receipts</span></a><?php endif; ?>
        <div class="nav-section">System</div>
        <a class="nav-item<?= $active('/crm/public/notifications') ?>" href="/crm/public/notifications"><?= $icon('audit') ?><span>Notifications</span></a>
        <?php if (\App\Core\Auth::hasPermission('users.view')): ?><a class="nav-item<?= $active('/crm/public/users') ?>" href="/crm/public/users"><?= $icon('users') ?><span>Users</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('audit.view')): ?><a class="nav-item" href="#"><?= $icon('audit') ?><span>Audit Logs</span></a><?php endif; ?>
        <?php if (\App\Core\Auth::hasPermission('settings.manage')): ?><a class="nav-item<?= $active('/crm/public/settings') ?>" href="/crm/public/settings"><?= $icon('settings') ?><span>Settings</span></a><?php endif; ?>
        <a class="nav-item<?= $active('/crm/public/profile') ?>" href="/crm/public/profile"><?= $icon('profile') ?><span>My Profile</span></a>
    </nav>
</aside>
