<?php
defined('ABSPATH') || exit;

$active_product_slug = (string) ($active_product_slug ?? '');
$active_version_slug = (string) ($active_version_slug ?? '');
$active_page_slug = (string) ($active_page_slug ?? '');
$active_product = is_array($active_product ?? null) ? $active_product : null;
$active_page_link_slugs = (array) ($active_page_link_slugs ?? []);
$products_by_category = [];

foreach ((array) $products as $item) {
    $category = trim((string) ($item['category'] ?? ''));
    $products_by_category[$category ?: __('Weitere Produkte', 'kb-markdown-importer')][] = $item;
}
?>
<div class="kb-docs-wrap kb-docs-app">
    <aside class="kb-app-sidebar" aria-label="<?php esc_attr_e('Dokumentationsnavigation', 'kb-markdown-importer'); ?>">
        <div class="kb-app-sidebar__brand">
            <a href="<?php echo esc_url($url_builder::docsIndex()); ?>"><?php esc_html_e('Dokumentation', 'kb-markdown-importer'); ?></a>
        </div>
        <div class="kb-app-sidebar__search">
            <?php echo do_shortcode('[kb_search]'); ?>
        </div>
        <?php if ($active_product && $active_version_slug && ! empty($active_pages)) : ?>
            <section class="kb-app-active-nav" aria-label="<?php esc_attr_e('Aktive Dokumentation', 'kb-markdown-importer'); ?>">
                <a class="kb-app-active-nav__title" href="<?php echo esc_url($url_builder::version($active_product_slug, $active_version_slug)); ?>">
                    <?php echo esc_html((string) $active_product['term']->name); ?>
                </a>
                <ul class="kb-app-page-list">
                    <?php foreach ((array) $active_pages as $page) : ?>
                        <?php
                        $linkSlug = (string) ($active_page_link_slugs[$page->ID] ?? get_post_meta($page->ID, '_kb_page_slug', true));
                        $isActivePage = $linkSlug === $active_page_slug || ('' === $active_page_slug && in_array($linkSlug, ['', 'index'], true));
                        $sourceSlug = (string) get_post_meta($page->ID, '_kb_product_slug', true);
                        $part = (array) (($active_product['parts'][$sourceSlug] ?? []) ?: []);
                        ?>
                        <li class="<?php echo $isActivePage ? 'is-active' : ''; ?>">
                            <a href="<?php echo esc_url($url_builder::page($active_product_slug, $active_version_slug, $linkSlug)); ?>">
                                <?php if (! empty($part['label']) && count((array) $active_product['parts']) > 1) : ?>
                                    <span class="kb-app-page-list__part"><?php echo esc_html((string) $part['label']); ?></span>
                                <?php endif; ?>
                                <?php echo esc_html(get_the_title($page)); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </section>
        <?php endif; ?>
        <nav class="kb-app-nav">
            <h2 class="kb-app-nav__title"><?php esc_html_e('Alle Produkte', 'kb-markdown-importer'); ?></h2>
            <?php foreach ($products_by_category as $category => $category_products) : ?>
                <section class="kb-app-category">
                    <h3><?php echo esc_html((string) $category); ?></h3>
                    <?php foreach ((array) $category_products as $item) : ?>
                        <?php
                        $term = $item['term'];
                        $versions = (array) $item['versions'];
                        $latest = $versions[0] ?? null;
                        $isActiveProduct = $term->slug === $active_product_slug;
                        ?>
                        <section class="kb-app-product <?php echo $isActiveProduct ? 'is-active' : ''; ?>">
                            <a class="kb-app-product__link" href="<?php echo esc_url($latest ? $url_builder::version($term->slug, $latest->slug) : $url_builder::product($term->slug)); ?>">
                                <?php echo esc_html($term->name); ?>
                            </a>
                        </section>
                    <?php endforeach; ?>
                </section>
            <?php endforeach; ?>
        </nav>
    </aside>
    <section class="kb-app-main">
        <?php echo $content; ?>
    </section>
</div>
