<?php
$items = [];
$add = static function(string $permission, string $href, string $label, string $icon) use (&$items) {
    if (\App\Core\Auth::hasPermission($permission)) $items[] = compact('href','label','icon');
};
$add('patients.view','/crm/public/patients','Patients','patients');
$add('therapists.view','/crm/public/therapists','Therapists','therapists');
$add('sessions.view','/crm/public/sessions','Sessions','sessions');
$add('appointments.view','/crm/public/appointments','Appointments','sessions');
$add('assessments.view','/crm/public/assessments','Assessments','assessments');
$add('reports.view','/crm/public/reports','Reports','reports');
$add('treatment_plans.view','/crm/public/treatment-plans','Treatment Plans','plans');
$add('services.view','/crm/public/services','Services','services');
$add('invoice.view','/crm/public/invoices','Invoices','invoices');
$add('invoice.view','/crm/public/billing-rates','Patient Rates','services');
$add('payment.view','/crm/public/payments','Payments & Receipts','payments');
$add('users.view','/crm/public/users','Users','users');
$add('settings.manage','/crm/public/settings','Settings','settings');

// Therapists work with assigned patients through their clinical workflow; they do not
// get the patient-management navigation entry.
if (\App\Core\Auth::hasRole('THERAPIST')) {
    $items = array_values(array_filter($items, static fn($item) => $item['icon'] !== 'patients'));
}

$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"/>',
        '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="mobile-nav-svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">'.($paths[$name] ?? $paths['dashboard']).'</svg>';
};

$user = $_SESSION['user'] ?? [];
$userName = trim((string)($user['full_name'] ?? $user['name'] ?? 'My Profile'));
$userInitials = '';
foreach (preg_split('/\s+/', $userName) as $part) { if ($part !== '') $userInitials .= strtoupper(substr($part, 0, 1)); }
$userInitials = substr($userInitials ?: 'U', 0, 2);
?>
<div class="mobile-drawer-backdrop" data-mobile-backdrop hidden></div>
<aside class="mobile-drawer" data-mobile-drawer aria-label="Mobile navigation" aria-hidden="true">
    <div class="mobile-drawer-header">
        <div class="sidebar-brand"><span class="sidebar-brand-mark">GT</span><div><strong>Grow Tool</strong><small>Therapies CRM</small></div></div>
        <button class="icon-button" type="button" aria-label="Close navigation" data-mobile-nav-close>×</button>
    </div>

    <a class="mobile-profile-card" href="/crm/public/profile">
        <span class="mobile-profile-avatar"><?= htmlspecialchars($userInitials, ENT_QUOTES, 'UTF-8') ?></span>
        <span class="mobile-profile-copy"><strong><?= htmlspecialchars($userName, ENT_QUOTES, 'UTF-8') ?></strong><small>My Profile</small></span>
        <?= $icon('profile') ?>
    </a>

    <a class="mobile-home-link" href="/crm/public/"><?= $icon('dashboard') ?><strong>Dashboard</strong></a>
    <div class="mobile-nav-section">Workspace</div>
    <nav>
        <?php foreach ($items as $item): ?>
            <a class="mobile-nav-item" href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>">
                <?= $icon($item['icon']) ?>
                <span><?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?></span>
                <span class="mobile-chevron" aria-hidden="true">›</span>
            </a>
        <?php endforeach; ?>
        <a class="mobile-nav-item" href="/crm/public/profile">
            <?= $icon('profile') ?><span>My Profile</span><span class="mobile-chevron" aria-hidden="true">›</span>
        </a>
    </nav>
</aside>
<nav class="mobile-bottom-nav" aria-label="Quick navigation">
    <a href="/crm/public/"><?= $icon('dashboard') ?><span>Home</span></a>
    <?php if(\App\Core\Auth::hasPermission('patients.view') && !\App\Core\Auth::hasRole('THERAPIST')): ?><a href="/crm/public/patients"><?= $icon('patients') ?><span>Patients</span></a><?php endif; ?>
    <?php if(\App\Core\Auth::hasPermission('sessions.view')): ?><a href="/crm/public/sessions"><?= $icon('sessions') ?><span>Sessions</span></a><?php endif; ?>
    <?php if(\App\Core\Auth::hasPermission('assessments.view')): ?><a href="/crm/public/assessments"><?= $icon('assessments') ?><span>Assessments</span></a><?php endif; ?>
    <button type="button" data-mobile-nav-toggle><?= $icon('settings') ?><span>More</span></button>
</nav>
