<?php
/**
 * Single Wiki Article
 */
get_header();
bluesky_navbar('wiki');
$header_bg = bluesky_get_image('bluesky_header_bg_url', 'images/header-background.jpg');

the_post();
$cats = get_the_terms(get_the_ID(), 'wiki_category');
$breadcrumb = [];
if ($cats && !is_wp_error($cats)) {
    $cat = $cats[0];
    if ($cat->parent) {
        $parent = get_term($cat->parent, 'wiki_category');
        if ($parent && !is_wp_error($parent))
            $breadcrumb[] = ['label' => $parent->name, 'url' => get_term_link($parent)];
    }
    $breadcrumb[] = ['label' => $cat->name, 'url' => get_term_link($cat)];
}

// Sidebar: alle Kategorien & Artikel
$all_top_cats = get_terms(['taxonomy'=>'wiki_category','hide_empty'=>true,'parent'=>0,'orderby'=>'menu_order']);
$current_cat_ids = wp_list_pluck($cats ?: [], 'term_id');
$current_post_id = get_the_ID();
?>

<!-- PAGE HEADER -->
<section id="page-header" style="background-image: url('<?php echo esc_url($header_bg); ?>')">
    <div class="content">
        <nav class="wiki-breadcrumb">
            <a href="<?php echo home_url('/wiki/'); ?>">Wiki</a>
            <?php foreach ($breadcrumb as $crumb): ?>
            <span class="sep">›</span>
            <a href="<?php echo esc_url($crumb['url']); ?>"><?php echo esc_html($crumb['label']); ?></a>
            <?php endforeach; ?>
            <span class="sep">›</span>
            <span class="current"><?php the_title(); ?></span>
        </nav>
        <h1 class="title"><span><?php the_title(); ?></span></h1>
    </div>
</section>

<!-- ARTIKEL -->
<section id="wiki-article">
    <div class="content" style="padding-top:50px;padding-bottom:80px">
        <div class="wiki-article-layout">

            <!-- SIDEBAR -->
            <aside class="wiki-sidebar">
                <div class="wiki-sidebar-inner">
                    <h4 class="wiki-sidebar-title">📚 Navigation</h4>
                    <?php if (!empty($all_top_cats) && !is_wp_error($all_top_cats)):
                        foreach ($all_top_cats as $top_cat):
                            $sub_cats = get_terms(['taxonomy'=>'wiki_category','hide_empty'=>true,'parent'=>$top_cat->term_id]);
                            $top_articles = new WP_Query([
                                'post_type'=>'bluesky_wiki','posts_per_page'=>-1,'post_status'=>'publish',
                                'orderby'=>'title','order'=>'ASC',
                                'tax_query'=>[['taxonomy'=>'wiki_category','field'=>'term_id','terms'=>$top_cat->term_id,'include_children'=>false]],
                            ]);
                            $is_open = in_array($top_cat->term_id, $current_cat_ids);
                            // Auch öffnen wenn ein Kind-Element aktiv ist
                            if (!$is_open && !empty($sub_cats) && !is_wp_error($sub_cats)) {
                                foreach ($sub_cats as $sub) {
                                    if (in_array($sub->term_id, $current_cat_ids)) { $is_open = true; break; }
                                }
                            }
                    ?>
                    <div class="wiki-nav-cat <?php echo $is_open ? 'open' : ''; ?>">
                        <div class="wiki-nav-cat-header">
                            <span><?php echo esc_html($top_cat->name); ?></span>
                            <span class="wiki-nav-toggle">▾</span>
                        </div>
                        <div class="wiki-nav-cat-body">
                            <?php if ($top_articles->have_posts()):
                                while ($top_articles->have_posts()): $top_articles->the_post(); ?>
                            <a href="<?php the_permalink(); ?>"
                               class="wiki-nav-link <?php echo get_the_ID() === $current_post_id ? 'active' : ''; ?>">
                                <?php the_title(); ?>
                            </a>
                            <?php endwhile; wp_reset_postdata(); endif; ?>

                            <?php if (!empty($sub_cats) && !is_wp_error($sub_cats)):
                                foreach ($sub_cats as $sub):
                                    $sub_articles = new WP_Query([
                                        'post_type'=>'bluesky_wiki','posts_per_page'=>-1,'post_status'=>'publish',
                                        'orderby'=>'title','order'=>'ASC',
                                        'tax_query'=>[['taxonomy'=>'wiki_category','field'=>'term_id','terms'=>$sub->term_id,'include_children'=>false]],
                                    ]);
                                    if (!$sub_articles->have_posts()) { wp_reset_postdata(); continue; }
                            ?>
                            <div class="wiki-nav-subcat">
                                <span class="wiki-nav-subcat-label"><?php echo esc_html($sub->name); ?></span>
                                <?php while ($sub_articles->have_posts()): $sub_articles->the_post(); ?>
                                <a href="<?php the_permalink(); ?>"
                                   class="wiki-nav-link wiki-nav-link-sub <?php echo get_the_ID() === $current_post_id ? 'active' : ''; ?>">
                                    <?php the_title(); ?>
                                </a>
                                <?php endwhile; wp_reset_postdata(); ?>
                            </div>
                            <?php endforeach; endif; ?>
                        </div>
                    </div>
                    <?php endforeach; endif; ?>
                </div>
            </aside>

            <!-- ARTIKEL CONTENT -->
            <article class="wiki-article-content">

                <!-- Header -->
                <div class="wiki-article-header">
                    <nav class="wiki-breadcrumb" style="margin-bottom:8px">
                        <a href="<?php echo home_url('/wiki/'); ?>">Wiki</a>
                        <?php foreach ($breadcrumb as $crumb): ?>
                        <span class="sep">›</span>
                        <a href="<?php echo esc_url($crumb['url']); ?>"><?php echo esc_html($crumb['label']); ?></a>
                        <?php endforeach; ?>
                    </nav>
                    <h1><?php the_title(); ?></h1>
                </div>

                <?php if (has_post_thumbnail()): ?>
                <div class="wiki-article-hero">
                    <?php the_post_thumbnail('large', ['class'=>'wiki-hero-img']); ?>
                </div>
                <?php endif; ?>

                <!-- Body -->
                <section class="wiki-answer-accordion" data-wiki-answer>
                    <button
                        type="button"
                        class="wiki-answer-toggle"
                        aria-expanded="false"
                        aria-controls="wiki-answer-content-<?php echo esc_attr($current_post_id); ?>"
                    >
                        <span class="wiki-answer-toggle-label">Antwort</span>
                        <span class="wiki-answer-toggle-hint">Zum Anzeigen aufklappen</span>
                        <span class="wiki-answer-toggle-icon" aria-hidden="true"></span>
                    </button>
                    <div class="wiki-answer-panel" id="wiki-answer-content-<?php echo esc_attr($current_post_id); ?>">
                        <div class="wiki-article-body">
                            <?php the_content(); ?>
                        </div>
                    </div>
                </section>

                <!-- Vor/Zurück -->
                <?php
                $prev = get_previous_post(true, '', 'wiki_category');
                $next = get_next_post(true, '', 'wiki_category');
                if ($prev || $next):
                ?>
                <div class="wiki-article-nav">
                    <?php if ($prev): ?>
                    <a href="<?php echo get_permalink($prev); ?>" class="wiki-nav-prev">
                        <span class="label">← Vorheriger Artikel</span>
                        <span class="title"><?php echo esc_html(get_the_title($prev)); ?></span>
                    </a>
                    <?php else: ?><div></div><?php endif; ?>
                    <?php if ($next): ?>
                    <a href="<?php echo get_permalink($next); ?>" class="wiki-nav-next">
                        <span class="label">Nächster Artikel →</span>
                        <span class="title"><?php echo esc_html(get_the_title($next)); ?></span>
                    </a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>

            </article>
        </div>
    </div>
</section>

<?php bluesky_footer(); ?>
<?php get_footer(); ?>
