<?php
// Partial: render only the header markup (no <!doctype>, <html>, <head>, or closing body/html).
// Used by static HTML resources that cannot run server-side includes.
require_once __DIR__ . '/functions.php';

$base_url = (defined('BASE_URL') ? BASE_URL : '/');
if (!empty($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === 'catalog.php') {
    $base_url = '../';
}
?>

<header class="header">
    <?php if (!isset($use_tailwind) || !$use_tailwind): ?>
    <div class="container">
    <?php endif; ?>
        <div class="nav-wrapper <?php if (isset($use_tailwind) && $use_tailwind): ?>nav-fullwidth<?php endif; ?>">
            <button type="button" id="mobileMenuButton" class="mobile-menu-toggle" aria-controls="mobileMenu" aria-expanded="false">
                <i class="fas fa-bars" aria-hidden="true"></i>
            </button>

            <div class="logo">
                <a href="<?php echo $base_url; ?>home">
                    <?php
                    $siteLogo = getSiteSetting('site_logo') ?: 'fas fa-newspaper';
                    $siteName = getSiteSetting('site_name') ?: 'Mutalex Academy';

                    if (strpos($siteLogo, 'assets/images/') === 0) {
                        echo '<img src="' . $base_url . htmlspecialchars($siteLogo) . '" alt="' . htmlspecialchars($siteName) . '" class="site-logo">';
                    } else {
                        echo '<i class="' . htmlspecialchars($siteLogo) . '"></i>';
                    }
                    echo htmlspecialchars($siteName);
                    ?>
                </a>
            </div>

            <nav class="nav">
                <a href="<?php echo $base_url; ?>home" class="nav-link">Home</a>
                <a href="<?php echo $base_url; ?>surveys.php" class="nav-link">Surveys</a>

                <a href="<?php echo $base_url; ?>courses/catalog.php" class="nav-link">Courses</a>

                <?php if (isLoggedIn()): ?>
                    <?php
                    $cartCount = 0;
                    try {
                        $db = getDB();
                        $userId = $_SESSION['user_id'];
                        $stmt = $db->prepare("SELECT COUNT(*) as count FROM cart_items WHERE user_id = ? AND item_type = 'course'");
                        $stmt->execute([$userId]);
                        $result = $stmt->fetch(PDO::FETCH_ASSOC);
                        $cartCount = $result['count'] ?? 0;
                    } catch (Exception $e) {
                        $cartCount = 0;
                    }
                    ?>

                    <?php if (basename($_SERVER['SCRIPT_NAME'] ?? '') !== 'home.php'): ?>
                        <button type="button" class="nav-link cart-link" data-bs-toggle="modal" data-bs-target="#cartModal" style="background: none; border: none; cursor: pointer;">
                            <i class="fas fa-shopping-cart me-1"></i>Cart
                            <?php if ($cartCount > 0): ?>
                                <span class="cart-count"><?php echo $cartCount; ?></span>
                            <?php endif; ?>
                        </button>
                    <?php endif; ?>

                    <?php if (basename($_SERVER['SCRIPT_NAME'] ?? '') !== 'home.php'): ?>
                        <a href="<?php echo $base_url; ?>profile.php" class="nav-link">Profile</a>
                    <?php endif; ?>

                    <?php if (hasRole('admin')): ?>
                        <a href="<?php echo $base_url; ?>admin/dashboard.php" class="nav-link">Admin Dashboard</a>
                    <?php elseif (hasRole('instructor')): ?>
                        <a href="<?php echo $base_url; ?>instructor/dashboard.php" class="nav-link">Instructor Dashboard</a>
                    <?php else: ?>
                        <?php renderStudentDashboardLink($base_url); ?>
                    <?php endif; ?>

                    <a href="<?php echo $base_url; ?>logout.php" class="nav-link">Logout</a>
                <?php else: ?>
                    <a href="<?php echo $base_url; ?>login.php" class="nav-link">Login</a>
                <?php endif; ?>
            </nav>
        </div>

    <?php if (!isset($use_tailwind) || !$use_tailwind): ?>
    </div>
    <?php endif; ?>
</header>

