<?php
$current = basename($_SERVER['PHP_SELF']);
$unread  = getUnreadCount($conn, $_SESSION['user_id']);
$av      = $conn->query("SELECT avatar FROM users WHERE id={$_SESSION['user_id']}")->fetch_assoc();
?>
<div class="sidebar" id="sidebar">

    <!-- Logo -->
    <div class="sidebar-logo">
        <div class="sidebar-logo-inner">
            <img src="/lostfound_relink/cyn_logo.png" alt="Cauayan City Hall">
            <div>
                <span class="sidebar-brand">🔗 RELINK</span>
                <span class="sidebar-sub">Cauayan City Hall</span>
            </div>
        </div>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">

        <div class="nav-section">Main</div>
        <a href="/lostfound_relink/user/dashboard.php" class="nav-item <?= $current=='dashboard.php'?'active':'' ?>">
            <span class="nav-icon">🏠</span>
            <span class="nav-label">Dashboard</span>
        </a>

        <div class="nav-section">Reports</div>
        <a href="/lostfound_relink/user/report_lost.php" class="nav-item <?= $current=='report_lost.php'?'active':'' ?>">
            <span class="nav-icon">🔍</span>
            <span class="nav-label">Report Lost Item</span>
        </a>
        <a href="/lostfound_relink/user/report_found.php" class="nav-item <?= $current=='report_found.php'?'active':'' ?>">
            <span class="nav-icon">📦</span>
            <span class="nav-label">Report Found Item</span>
        </a>
        <a href="/lostfound_relink/user/my_reports.php" class="nav-item <?= $current=='my_reports.php'?'active':'' ?>">
            <span class="nav-icon">📋</span>
            <span class="nav-label">My Reports</span>
        </a>

        <div class="sidebar-divider"></div>
        <div class="nav-section">Discover</div>
        <a href="/lostfound_relink/public/browse.php" class="nav-item <?= $current=='browse.php'?'active':'' ?>">
            <span class="nav-icon">🌐</span>
            <span class="nav-label">Browse Items</span>
        </a>
        <a href="/lostfound_relink/user/messages.php" class="nav-item <?= $current=='messages.php'?'active':'' ?>">
            <span class="nav-icon">💬</span>
            <span class="nav-label">Messages</span>
            <?php
            $umsg = $conn->query("SELECT COUNT(*) as c FROM messages m JOIN conversation_participants cp ON cp.conversation_id=m.conversation_id WHERE cp.user_id={$_SESSION['user_id']} AND m.sender_id != {$_SESSION['user_id']} AND m.seen_status='unseen'");
            $umsg_count = $umsg ? $umsg->fetch_assoc()['c'] : 0;
            if($umsg_count > 0): ?><span class="nav-badge"><?= $umsg_count ?></span><?php endif; ?>
        </a>
        <a href="/lostfound_relink/user/notifications.php" class="nav-item <?= $current=='notifications.php'?'active':'' ?>">
            <span class="nav-icon">🔔</span>
            <span class="nav-label">Notifications</span>
            <?php if($unread > 0): ?><span class="nav-badge"><?= $unread ?></span><?php endif; ?>
        </a>

        <div class="sidebar-divider"></div>
        <div class="nav-section">Account</div>
        <a href="/lostfound_relink/user/settings.php" class="nav-item <?= $current=='settings.php'?'active':'' ?>">
            <span class="nav-icon">⚙️</span>
            <span class="nav-label">Settings</span>
        </a>

    </nav>

    <!-- Footer / Profile -->
    <div class="sidebar-footer">
        <div class="sidebar-profile">
            <div class="sidebar-avatar">
                <?php if(!empty($av['avatar'])): ?>
                <img src="/lostfound_relink/<?= htmlspecialchars($av['avatar']) ?>" alt="Avatar">
                <?php else: ?>
                <?= strtoupper(substr($_SESSION['name'] ?? 'U', 0, 1)) ?>
                <?php endif; ?>
            </div>
            <div class="sidebar-profile-info">
                <div class="sidebar-profile-name"><?= htmlspecialchars($_SESSION['name'] ?? 'User') ?></div>
                <div class="sidebar-profile-role">User</div>
            </div>
        </div>
        <a href="/lostfound_relink/auth/logout.php" class="sidebar-logout">
            <svg xmlns="http://www.w3.org/2000/svg" width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
                <path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/>
                <polyline points="16 17 21 12 16 7"/>
                <line x1="21" y1="12" x2="9" y2="12"/>
            </svg>
            Sign Out
        </a>
    </div>

</div>
