<?php
/**
 * Template Name: BlueSky - Wiki
 */
get_header();
bluesky_navbar('wiki');
$server_ip = get_option('bluesky_server_ip', 'mc.example.com');
$header_bg = bluesky_get_image('bluesky_header_bg_url', 'images/header-background.jpg');

// Nur Top-Level Kategorien holen
$top_cats = get_terms([
    'taxonomy'   => 'wiki_category',
    'hide_empty' => false,
    'parent'     => 0,
    'orderby'    => 'menu_order',
    'order'      => 'ASC',
]);
?>
<section id="page-header" style="background-image:url('<?php echo esc_url($header_bg); ?>')">
    <div class="content">
        <p class="minecraft-server-ip"><?php echo esc_html($server_ip); ?></p>
        <h1 class="title"><span>Wiki</span></h1>
        <p class="description">Alle Informationen zum Server auf einen Blick.</p>
    </div>
</section>

<section id="wiki-overview">
    <div class="content">

        <?php if (!empty($top_cats) && !is_wp_error($top_cats)): ?>
        <div class="wiki-plugin-container">
            <div class="wiki-tabs-nav">
                <?php foreach ($top_cats as $index => $cat): ?>
                <button
                    type="button"
                    class="wiki-tab-button <?php echo $index === 0 ? 'active' : ''; ?>"
                    data-tab="wiki-tab-<?php echo esc_attr($cat->term_id); ?>"
                >
                    <?php echo esc_html($cat->name); ?>
                </button>
                <?php endforeach; ?>
            </div>

            <div class="wiki-tabs-content">
                <?php foreach ($top_cats as $index => $cat):
                    $sub_cats = get_terms([
                        'taxonomy'   => 'wiki_category',
                        'hide_empty' => false,
                        'parent'     => $cat->term_id,
                        'orderby'    => 'menu_order',
                        'order'      => 'ASC',
                    ]);

                    $direct_articles = new WP_Query([
                        'post_type'      => 'bluesky_wiki',
                        'posts_per_page' => -1,
                        'post_status'    => 'publish',
                        'orderby'        => 'menu_order title',
                        'order'          => 'ASC',
                        'tax_query'      => [[
                            'taxonomy'         => 'wiki_category',
                            'field'            => 'term_id',
                            'terms'            => $cat->term_id,
                            'include_children' => false,
                        ]],
                    ]);
                ?>
                <div id="wiki-tab-<?php echo esc_attr($cat->term_id); ?>" class="wiki-tab-panel <?php echo $index === 0 ? 'active' : ''; ?>">
                    <div class="wiki-cat-header">
                        <?php if ($cat->description): ?>
                        <div class="wiki-cat-icon"><?php echo wp_kses_post($cat->description); ?></div>
                        <?php endif; ?>
                        <div>
                            <h2 class="wiki-cat-title"><?php echo esc_html($cat->name); ?></h2>
                            <span class="wiki-cat-count"><?php echo esc_html($cat->count); ?> Artikel</span>
                        </div>
                    </div>

                    <?php if ($direct_articles->have_posts()): ?>
                    <div class="wiki-panel-block">
                        <h3 class="wiki-subcat-title"><span class="wiki-subcat-dot"></span>Allgemein</h3>
                        <div class="wiki-articles-grid">
                            <?php while ($direct_articles->have_posts()): $direct_articles->the_post();
                                $thumb = has_post_thumbnail()
                                    ? get_the_post_thumbnail_url(get_the_ID(), 'thumbnail')
                                    : get_template_directory_uri() . '/images/logo.png';
                                $excerpt = get_the_excerpt() ?: wp_trim_words(get_the_content(), 15, '...');
                            ?>
                            <a href="<?php the_permalink(); ?>" class="wiki-article-card">
                                <div class="wiki-card-icon">
                                    <img src="<?php echo esc_url($thumb); ?>" alt="<?php the_title_attribute(); ?>">
                                </div>
                                <div class="wiki-card-body">
                                    <h3><?php the_title(); ?></h3>
                                    <?php if ($excerpt): ?><p><?php echo esc_html($excerpt); ?></p><?php endif; ?>
                                </div>
                                <div class="wiki-card-arrow">→</div>
                            </a>
                            <?php endwhile; wp_reset_postdata(); ?>
                        </div>
                    </div>
                    <?php 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'        => 'menu_order 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-subcat-block">
                        <h3 class="wiki-subcat-title"><span class="wiki-subcat-dot"></span><?php echo esc_html($sub->name); ?></h3>
                        <div class="wiki-articles-grid">
                            <?php while ($sub_articles->have_posts()): $sub_articles->the_post();
                                $thumb = has_post_thumbnail()
                                    ? get_the_post_thumbnail_url(get_the_ID(), 'thumbnail')
                                    : get_template_directory_uri() . '/images/logo.png';
                                $excerpt = get_the_excerpt() ?: wp_trim_words(get_the_content(), 15, '...');
                            ?>
                            <a href="<?php the_permalink(); ?>" class="wiki-article-card">
                                <div class="wiki-card-icon">
                                    <img src="<?php echo esc_url($thumb); ?>" alt="<?php the_title_attribute(); ?>">
                                </div>
                                <div class="wiki-card-body">
                                    <h3><?php the_title(); ?></h3>
                                    <?php if ($excerpt): ?><p><?php echo esc_html($excerpt); ?></p><?php endif; ?>
                                </div>
                                <div class="wiki-card-arrow">→</div>
                            </a>
                            <?php endwhile; wp_reset_postdata(); ?>
                        </div>
                    </div>
                    <?php endforeach; endif; ?>
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <?php else: ?>
        <!-- Noch keine Kategorien — Hinweis -->
        <div class="wiki-empty">
            <div class="wiki-empty-icon">📚</div>
            <h2>Wiki ist noch leer</h2>
            <p>Erstelle im Backend zuerst <strong>Wiki-Kategorien</strong> und dann <strong>Wiki-Artikel</strong>.</p>
            <?php if (current_user_can('manage_options')): ?>
            <div style="display:flex;gap:12px;justify-content:center;margin-top:20px">
                <a href="<?php echo admin_url('edit-tags.php?taxonomy=wiki_category&post_type=bluesky_wiki'); ?>" class="wiki-btn">+ Kategorie erstellen</a>
                <a href="<?php echo admin_url('post-new.php?post_type=bluesky_wiki'); ?>" class="wiki-btn wiki-btn-secondary">+ Artikel erstellen</a>
            </div>
            <?php endif; ?>
        </div>
        <?php endif; ?>

    </div>
</section>

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