<?php
/**
 * components/navigation.php
 *
 * Navigation component that replaces modX Personalize and Wayfinder snippets.
 * Handles authentication-based navigation display.
 *
 * Usage: include __DIR__.'/components/navigation.php';
 */

require_once __DIR__.'/../lib/auth.php';

// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Determine navigation based on authentication status
$isLoggedIn = isLoggedIn();

// Navigation configuration
$siteName = 'Crop Management Platform';

if ($isLoggedIn) {
    // Logged-in user navigation
    $navItems = [
        [ 'href' => '/dashboard/dashboard.php', 'label' => 'Dashboard' ],
        [ 'href' => '/dashboard/crop-analysis/soil-analysis.php', 'label' => 'Soil Analysis' ],
        [ 'href' => '/dashboard/crop-analysis/soil-report.php', 'label' => 'Reports' ],
        [ 'href' => '/login/change-password.php', 'label' => 'Account' ],
        [ 'href' => '/login/logout.php', 'label' => 'Logout' ],
    ];

    $sidebarItems = [
        [ 'href' => '/dashboard/dashboard.php', 'label' => 'Home', 'icon' => 'fas fa-home' ],
        [
            'label' => 'Soil Analysis',
            'icon' => 'fas fa-seedling',
            'children' => [
                [ 'href' => '/dashboard/crop-analysis/soil-test-data.php', 'label' => 'New Test' ],
                [ 'href' => '/dashboard/crop-analysis/soil-analysis.php', 'label' => 'View Results' ],
                [ 'href' => '/dashboard/crop-analysis/soil-report.php', 'label' => 'Reports' ],
            ]
        ],
        [ 'href' => '/dashboard/inbox.php', 'label' => 'Inbox', 'icon' => 'fas fa-inbox' ],
        [ 'href' => '/dashboard/planning-calendar.php', 'label' => 'Calendar', 'icon' => 'fas fa-calendar' ],
        [ 'href' => '/login/change-password.php', 'label' => 'Account', 'icon' => 'fas fa-user-cog' ],
    ];
} else {
    // Guest navigation
    $navItems = [
        [ 'href' => '/', 'label' => 'Home' ],
        [ 'href' => '/login/login.php', 'label' => 'Login' ],
        [ 'href' => '/login/register.php', 'label' => 'Register' ],
    ];

    $sidebarItems = []; // No sidebar for guests
}

// Determine active item based on current URL
$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH) ?? '';
$activeItem = '';

foreach ($navItems as $item) {
    if ($item['href'] === $currentPath) {
        $activeItem = $item['label'];
        break;
    }
}

// Function to render navbar
function renderNavbar() {
    global $siteName, $navItems, $activeItem;
    ?>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/"><?= htmlspecialchars($siteName, ENT_QUOTES, 'UTF-8') ?></a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#topNavbar" aria-controls="topNavbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="topNavbar">
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <?php foreach ($navItems as $item): ?>
                        <li class="nav-item">
                            <a class="nav-link<?= ($activeItem === $item['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>">
                                <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </nav>
    <?php
}

// Function to render sidebar
function renderSidebar() {
    global $sidebarItems, $activeItem;

    if (empty($sidebarItems)) {
        return; // No sidebar for guests
    }
    ?>
    <div class="sb-sidenav-menu">
        <div class="nav">
            <?php foreach ($sidebarItems as $item): ?>
                <?php if (isset($item['children'])): ?>
                    <!-- Parent item with children -->
                    <a class="nav-link collapsed" href="#" data-bs-toggle="collapse" data-bs-target="#nav-<?= md5($item['label']) ?>" aria-expanded="false" aria-controls="nav-<?= md5($item['label']) ?>">
                        <div class="sb-nav-link-icon"><i class="<?= htmlspecialchars($item['icon'], ENT_QUOTES, 'UTF-8') ?>"></i></div>
                        <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
                        <div class="sb-sidenav-collapse-arrow"><i class="fas fa-angle-down"></i></div>
                    </a>
                    <div class="collapse" id="nav-<?= md5($item['label']) ?>" aria-labelledby="heading-<?= md5($item['label']) ?>">
                        <nav class="sb-sidenav-menu-nested nav">
                            <?php foreach ($item['children'] as $child): ?>
                                <a class="nav-link<?= ($activeItem === $child['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($child['href'], ENT_QUOTES, 'UTF-8') ?>">
                                    <?= htmlspecialchars($child['label'], ENT_QUOTES, 'UTF-8') ?>
                                </a>
                            <?php endforeach; ?>
                        </nav>
                    </div>
                <?php else: ?>
                    <!-- Regular item -->
                    <a class="nav-link<?= ($activeItem === $item['label'] ? ' active' : '') ?>" href="<?= htmlspecialchars($item['href'], ENT_QUOTES, 'UTF-8') ?>">
                        <div class="sb-nav-link-icon"><i class="<?= htmlspecialchars($item['icon'], ENT_QUOTES, 'UTF-8') ?>"></i></div>
                        <?= htmlspecialchars($item['label'], ENT_QUOTES, 'UTF-8') ?>
                    </a>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
    </div>
    <?php
}

// Auto-render based on context
// This allows the component to be included and automatically render the appropriate navigation
if (!isset($skipAutoRender) || !$skipAutoRender) {
    if ($isLoggedIn) {
        renderNavbar();
    } else {
        renderNavbar(); // Could render different navbar for guests if needed
    }
}
?>