<?php
/**
 * layouts/sidebar.php
 *
 * Left sidebar navigation — matches original modX Wayfinder output.
 * Active state is detected automatically from the current request URI.
 * Collapsible sections restore open state via Bootstrap accordion.
 *
 * Requires lib/auth.php to already be included (for getCurrentUser()).
 */
$currentUser = getCurrentUser() ?? ['fullname' => ''];

// Detect current path for active-link highlighting
$currentPath = parse_url($_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH);

/**
 * Return 'active' if $href matches the current path, else ''.
 */
$isActive = fn(string $href): string =>
    str_starts_with($currentPath, $href) ? ' active' : '';

/**
 * Return 'show' if any child href is active (keeps accordion open on load).
 */
$groupActive = function (array $children) use ($currentPath): string {
    foreach ($children as $child) {
        if (str_starts_with($currentPath, $child)) return ' show';
    }
    return '';
};

// Child paths per collapsible group — used to keep the right group open
$weatherChildren   = ['/dashboard/weather/'];
$cropChildren      = [
    '/dashboard/crop-analysis/soil-test-data/',
    '/dashboard/crop-analysis/plant-test-data/',
    '/dashboard/crop-analysis/water-test-data/',
    '/dashboard/crop-analysis/animal-dietary-balance/',
    '/dashboard/crop-analysis/compost-test-data/',
];
$settingsChildren  = [
    '/dashboard/client-settings/soil-recommendations.php',
    '/dashboard/client-settings/product-list.php',
    '/dashboard/client-settings/update-details.php',
];
?>

<nav id="sidenavAccordion" class="sb-sidenav accordion sb-sidenav-dark">
    <div class="sb-sidenav-menu">
        <div class="nav">

            <!-- Planning Calendar -->
            <a href="/dashboard/planning-calendar.php"
               class="nav-link<?= $isActive('/dashboard/planning-calendar.php') ?>">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-map-marker nav_icon"></i>
                </div>
                Planning Calendar
            </a>

            <!-- Report History / Inbox -->
            <a href="/dashboard/inbox.php"
               class="nav-link<?= $isActive('/dashboard/inbox.php') ?>">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-inbox nav_icon"></i>
                </div>
                Report History
            </a>

            <!-- Weather (collapsible) -->
            <a class="nav-link collapsed<?= $groupActive($weatherChildren) ?>"
               href="#"
               data-bs-toggle="collapse"
               data-bs-target="#collapseWeather"
               aria-expanded="<?= $groupActive($weatherChildren) ? 'true' : 'false' ?>"
               aria-controls="collapseWeather">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-cloud nav_icon"></i>
                </div>
                Weather
                <div class="sb-sidenav-collapse-arrow">
                    <i class="fas fa-angle-down"></i>
                </div>
            </a>
            <div class="collapse<?= $groupActive($weatherChildren) ?>"
                 id="collapseWeather"
                 data-bs-parent="#sidenavAccordion">
                <nav class="sb-sidenav-menu-nested nav">
                    <a class="nav-link<?= $isActive('/dashboard/weather/moisture-sensor-setup.php') ?>"
                       href="/dashboard/weather/moisture-sensor-setup.php">
                        <i class="fa fa-eye-dropper nav_icon"></i>&nbsp;Moisture Sensor Setup
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/weather/weather-monitoring.php') ?>"
                       href="/dashboard/weather/weather-monitoring.php">
                        <i class="fas fa-cloud-sun nav_icon"></i>&nbsp;Weather Monitoring
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/weather/moisture-monitoring.php') ?>"
                       href="/dashboard/weather/moisture-monitoring.php">
                        <i class="fas fa-tint nav_icon"></i>&nbsp;Moisture Monitoring
                    </a>
                </nav>
            </div>

            <!-- Pesticide -->
            <a href="/dashboard/pesticide.php"
               class="nav-link<?= $isActive('/dashboard/pesticide.php') ?>">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-bug nav_icon"></i>
                </div>
                Pesticide
            </a>

            <!-- Crop Analysis (collapsible) -->
            <a class="nav-link collapsed<?= $groupActive($cropChildren) ?>"
               href="#"
               data-bs-toggle="collapse"
               data-bs-target="#collapseCropAnalysis"
               aria-expanded="<?= $groupActive($cropChildren) ? 'true' : 'false' ?>"
               aria-controls="collapseCropAnalysis">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-tree nav_icon"></i>
                </div>
                Crop Analysis
                <div class="sb-sidenav-collapse-arrow">
                    <i class="fas fa-angle-down"></i>
                </div>
            </a>
            <div class="collapse<?= $groupActive($cropChildren) ?>"
                 id="collapseCropAnalysis"
                 data-bs-parent="#sidenavAccordion">
                <nav class="sb-sidenav-menu-nested nav">
                    <a class="nav-link<?= $isActive('/dashboard/crop-analysis/soil-test-data/') ?>"
                       href="/dashboard/crop-analysis/soil-test-data/soil-test-data.php">
                        <i class="fas fa-globe-asia nav_icon"></i>&nbsp;Soil Test Data
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/crop-analysis/plant-test-data/') ?>"
                       href="/dashboard/crop-analysis/plant-test-data/">
                        <i class="fab fa-pagelines nav_icon"></i>&nbsp;Plant Test Data
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/crop-analysis/water-test-data/') ?>"
                       href="/dashboard/crop-analysis/water-test-data/">
                        <i class="fa fa-tint nav_icon"></i>&nbsp;Water Test Data
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/crop-analysis/animal-dietary-balance/') ?>"
                       href="/dashboard/crop-analysis/animal-dietary-balance/">
                        <i class="fas fa-dog nav_icon"></i>&nbsp;Animal Dietary Balance
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/crop-analysis/compost-test-data/') ?>"
                       href="/dashboard/crop-analysis/compost-test-data/">
                        <i class="fas fa-cloud nav_icon"></i>&nbsp;Compost Test Data
                    </a>
                </nav>
            </div>

            <!-- Irrigation Controller -->
            <a href="/dashboard/irrigation/"
               class="nav-link<?= $isActive('/dashboard/irrigation/') ?>">
                <div class="sb-nav-link-icon">
                    <i class="fas fa-cloud-sun-rain nav_icon"></i>
                </div>
                Irrigation Controller
            </a>

            <!-- Client Settings (collapsible) -->
            <a class="nav-link collapsed<?= $groupActive($settingsChildren) ?>"
               href="#"
               data-bs-toggle="collapse"
               data-bs-target="#collapseSettings"
               aria-expanded="<?= $groupActive($settingsChildren) ? 'true' : 'false' ?>"
               aria-controls="collapseSettings">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-cog nav_icon"></i>
                </div>
                Client Settings
                <div class="sb-sidenav-collapse-arrow">
                    <i class="fas fa-angle-down"></i>
                </div>
            </a>
            <div class="collapse<?= $groupActive($settingsChildren) ?>"
                 id="collapseSettings"
                 data-bs-parent="#sidenavAccordion">
                <nav class="sb-sidenav-menu-nested nav">
                    <a class="nav-link<?= $isActive('/dashboard/client-settings/soil-recommendations.php') ?>"
                       href="/dashboard/client-settings/soil-recommendations.php">
                        <i class="fa fa-cog nav_icon"></i>&nbsp;Soil Recommendations
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/client-settings/product-list.php') ?>"
                       href="/dashboard/client-settings/product-list.php">
                        <i class="fa fa-cog nav_icon"></i>&nbsp;Product List
                    </a>
                    <a class="nav-link<?= $isActive('/dashboard/client-settings/update-details.php') ?>"
                       href="/dashboard/client-settings/update-details.php">
                        <i class="fa fa-cog nav_icon"></i>&nbsp;Update Details
                    </a>
                </nav>
            </div>

            <!-- Crop Cards -->
            <a href="/dashboard/crop-cards/"
               class="nav-link<?= $isActive('/dashboard/crop-cards/') ?>">
                <div class="sb-nav-link-icon">
                    <i class="fa fa-tree nav_icon"></i>
                </div>
                Crop Cards
            </a>

        </div>
    </div>

    <!-- Footer: logged-in user -->
    <div class="sb-sidenav-footer">
        <div class="small">Logged in as:</div>
        <?= htmlspecialchars($currentUser['fullname'], ENT_QUOTES, 'UTF-8') ?>
    </div>

</nav>
