<?php
$activeTeacherNav = $activeTeacherNav ?? '';
function navIcon(string $name): string {
    $icons = [
        '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"/>',
        'library' => '<path d="M4 19.5A2.5 2.5 0 0 1 6.5 17H20"/><path d="M6.5 2H20v19H6.5A2.5 2.5 0 0 1 4 18.5v-14A2.5 2.5 0 0 1 6.5 2Z"/><path d="M8 7h8M8 11h6"/>',
        'lesson' => '<path d="M2 3h6a4 4 0 0 1 4 4v14a3 3 0 0 0-3-3H2z"/><path d="M22 3h-6a4 4 0 0 0-4 4v14a3 3 0 0 1 3-3h7z"/>',
        'activity' => '<path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
        'game' => '<rect x="2" y="6" width="20" height="12" rx="2"/><path d="M12 12h.01M17 12h.01M7 12h.01"/>',
        'avatar' => '<circle cx="12" cy="8" r="4"/><path d="M4 21a8 8 0 0 1 16 0"/><path d="M17 4l1-2M7 4 6 2"/>',
        'worksheet' => '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M8 8h8M8 12h8M8 16h5"/><path d="M3 3l4 4M21 3l-4 4M3 21l4-4M21 21l-4-4"/>',
        'students' => '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M22 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75"/>',
        'teachers' => '<circle cx="9" cy="7" r="4"/><path d="M2 21v-2a7 7 0 0 1 14 0v2"/><path d="M18 8h4M20 6v4"/>',
        'assignment' => '<path d="M9 11l3 3L22 4"/><path d="M21 12v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11"/>',
        'analytics' => '<path d="M3 3v18h18"/><path d="m7 16 4-5 4 3 5-7"/>',
        'ai' => '<path d="m12 3-1.7 4.3L6 9l4.3 1.7L12 15l1.7-4.3L18 9l-4.3-1.7L12 3Z"/><path d="m5 15-.9 2.1L2 18l2.1.9L5 21l.9-2.1L8 18l-2.1-.9L5 15ZM19 13l-1.2 2.8L15 17l2.8 1.2L19 21l1.2-2.8L23 17l-2.8-1.2L19 13Z"/>',
        'location' => '<path d="M3 21h18"/><path d="M6 21V8l6-5 6 5v13"/><path d="M9 21v-6h6v6"/><path d="M9 10h.01M15 10h.01"/>',
        'settings' => '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .34 1.88l.06.06-2.83 2.83-.06-.06A1.7 1.7 0 0 0 15 19.4a1.7 1.7 0 0 0-1 .6 1.7 1.7 0 0 0-.4 1.1V21h-4v-.09a1.7 1.7 0 0 0-1.1-1.56 1.7 1.7 0 0 0-1.88.34l-.06.06-2.83-2.83.06-.06A1.7 1.7 0 0 0 4.6 15a1.7 1.7 0 0 0-.6-1 1.7 1.7 0 0 0-1.1-.4H3v-4h.09A1.7 1.7 0 0 0 4.65 8.5a1.7 1.7 0 0 0-.34-1.88l-.06-.06 2.83-2.83.06.06A1.7 1.7 0 0 0 9 4.6a1.7 1.7 0 0 0 1-.6 1.7 1.7 0 0 0 .4-1.1V3h4v.09A1.7 1.7 0 0 0 15.5 4.65a1.7 1.7 0 0 0 1.88-.34l.06-.06 2.83 2.83-.06.06A1.7 1.7 0 0 0 19.4 9c.15.37.36.71.6 1 .29.32.68.52 1.1.6H21v4h-.09A1.7 1.7 0 0 0 19.4 15Z"/>',
        'logout' => '<path d="M10 17l5-5-5-5"/><path d="M15 12H3"/><path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/>',
    ];
    return '<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">' . ($icons[$name] ?? '') . '</svg>';
}
$sidebarUserRole = strtolower(trim((string)($_SESSION['user_role'] ?? '')));
$isAdmin = ($sidebarUserRole === 'admin');
?>
<aside class="sidebar" id="teacherSidebar">
    <!-- Brand -->
    <div class="sidebar-brand">
        <a href="../dashboard/" class="brand-lockup" aria-label="EnglishSpeak Dashboard">
            <span class="brand-logo">E</span>
            <span class="brand-text">
                <strong>English<span>Speak</span></strong>
                <small><?= $isAdmin ? 'Admin' : 'Teacher' ?></small>
            </span>
        </a>
        <button class="sidebar-close" id="sidebarClose" type="button" aria-label="Close menu">&times;</button>
    </div>

    <!-- Collapse Toggle -->
    <button class="sidebar-toggle" id="sidebarToggle" type="button" aria-label="Toggle sidebar" title="Collapse sidebar">
        <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><path d="m15 18-6-6 6-6"/></svg>
    </button>

    <!-- Navigation -->
    <nav class="sidebar-nav" aria-label="Teacher navigation">
        <!-- Dashboard -->
        <div class="nav-group">
            <span class="nav-group-label">Dashboard</span>
            <a href="../dashboard/" class="nav-item <?= $activeTeacherNav === 'dashboard' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('dashboard') ?></span><strong>Dashboard</strong>
            </a>
        </div>

        <!-- Content -->
        <div class="nav-group">
            <span class="nav-group-label">Content</span>
            <a href="../books/" class="nav-item <?= $activeTeacherNav === 'content' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('library') ?></span><strong>Content Library</strong>
            </a>
            <a href="../word-library/" class="nav-item <?= $activeTeacherNav === 'word-library' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('lesson') ?></span><strong>Word Library</strong>
            </a>
            <a href="../flashcards/" class="nav-item <?= $activeTeacherNav === 'flashcards' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('lesson') ?></span><strong>Flashcard Sets</strong>
            </a>
            <a href="../sentence-library/" class="nav-item <?= $activeTeacherNav === 'sentence-library' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('activity') ?></span><strong>Sentence Library</strong>
            </a>
            <a href="../sentence-sets/" class="nav-item <?= $activeTeacherNav === 'sentence-sets' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('activity') ?></span><strong>Sentence Sets</strong>
            </a>
            <a href="../dialogue-sets/" class="nav-item <?= $activeTeacherNav === 'dialogue-sets' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('activity') ?></span><strong>Dialogue Sets</strong>
            </a>
            <a href="../topic-library/" class="nav-item <?= $activeTeacherNav === 'topic-library' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('library') ?></span><strong>Topic Library</strong>
            </a>

            <?php if ($isAdmin): ?>
            <a href="../avatar-builder.php" class="nav-item <?= $activeTeacherNav === 'avatar-builder' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('avatar') ?></span><strong>Avatar Builder</strong>
            </a>
            <?php endif; ?>
        </div>

        <!-- Students -->
        <div class="nav-group">
            <span class="nav-group-label">Students</span>
            <a href="../students/" class="nav-item <?= $activeTeacherNav === 'students' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('students') ?></span><strong>Students</strong>
            </a>
            <a href="../reports/" class="nav-item <?= $activeTeacherNav === 'reports' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('analytics') ?></span><strong>Reports</strong>
            </a>
        </div>

        <?php if ($isAdmin): ?>
        <!-- AI - thử nghiệm, chỉ Admin nhìn thấy -->
        <div class="nav-group">
            <span class="nav-group-label">AI</span>
            <a href="../ai/" class="nav-item <?= $activeTeacherNav === 'ai' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('ai') ?></span><strong>AI Studio</strong>
                <span class="nav-badge">NEW</span>
            </a>
        </div>
        <?php endif; ?>

        <?php if ($isAdmin): ?>
        <!-- Admin -->
        <div class="nav-group">
            <span class="nav-group-label">Admin</span>
            <a href="../teachers/" class="nav-item <?= $activeTeacherNav === 'teachers' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('teachers') ?></span><strong>Giáo viên</strong>
            </a>
            <a href="../schools/" class="nav-item <?= $activeTeacherNav === 'locations' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('location') ?></span><strong>Tỉnh &amp; Trường</strong>
            </a>
        </div>
        <?php endif; ?>

        <!-- System -->
        <div class="nav-group">
            <span class="nav-group-label">System</span>
            <a href="../settings/" class="nav-item <?= $activeTeacherNav === 'settings' ? 'active' : '' ?>">
                <span class="nav-icon"><?= navIcon('settings') ?></span><strong><?= $isAdmin ? 'Settings' : 'Đổi mật khẩu' ?></strong>
            </a>
            <a href="../logout.php" class="nav-item logout">
                <span class="nav-icon"><?= navIcon('logout') ?></span><strong>Logout</strong>
            </a>
        </div>
    </nav>

</aside>