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

$settings = (array) ($settings ?? []);
$updates = (array) ($updates ?? []);
?>
<section class="kb-docs-home">
    <h1><?php esc_html_e('Dokumentation', 'kb-markdown-importer'); ?></h1>
    <div class="kb-docs-home-grid">
        <section class="kb-home-card kb-home-intro">
            <h2><?php echo esc_html((string) ($settings['docs_home_intro_title'] ?? __('So nutzt du die Dokumentation', 'kb-markdown-importer'))); ?></h2>
            <div class="kb-home-intro__content">
                <?php echo wp_kses_post(wpautop((string) ($settings['docs_home_intro_content'] ?? ''))); ?>
            </div>
        </section>
        <section class="kb-home-card kb-product-updates">
            <h2><?php esc_html_e('Neueste Produktupdates', 'kb-markdown-importer'); ?></h2>
            <?php if ($updates) : ?>
                <ul class="kb-product-updates__list">
                    <?php foreach ($updates as $update) : ?>
                        <li>
                            <div class="kb-product-updates__meta">
                                <strong><?php echo esc_html((string) ($update['product'] ?: __('Produkt', 'kb-markdown-importer'))); ?></strong>
                                <?php if (! empty($update['version'])) : ?><span><?php echo esc_html((string) $update['version']); ?></span><?php endif; ?>
                                <?php if (! empty($update['date'])) : ?><time><?php echo esc_html((string) $update['date']); ?></time><?php endif; ?>
                            </div>
                            <?php if (! empty($update['changelog'])) : ?>
                                <p><?php echo esc_html((string) $update['changelog']); ?></p>
                            <?php endif; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php else : ?>
                <p class="kb-empty-state"><?php esc_html_e('Es wurden noch keine Produktupdates gefunden.', 'kb-markdown-importer'); ?></p>
            <?php endif; ?>
        </section>
    </div>
    <div class="kb-product-list">
        <?php foreach ((array) $products as $item) : ?>
            <?php $term = $item['term']; ?>
            <?php $latest = $item['versions'][0] ?? null; ?>
            <section class="kb-product-card">
                <h2><a 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></h2>
                <?php if (! empty($item['versions'])) : ?>
                    <ul>
                        <?php foreach ($item['versions'] as $version) : ?>
                            <li><a href="<?php echo esc_url($url_builder::version($term->slug, $version->slug)); ?>"><?php echo esc_html($version->name); ?></a></li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </section>
        <?php endforeach; ?>
    </div>
</section>
