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

$settings = (array) ($settings ?? []);
$updates = (array) ($updates ?? []);
$updates_by_month = [];
$products_by_category = [];

foreach ($updates as $update) {
    $month = (string) ($update['month_label'] ?? '');
    $updates_by_month[$month ?: ''][] = $update;
}

foreach ((array) ($products ?? []) as $item) {
    $category = trim((string) ($item['category'] ?? ''));
    $products_by_category[$category ?: __('Weitere Produkte', 'kb-markdown-importer')][] = $item;
}
?>
<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) : ?>
                <?php foreach ($updates_by_month as $month => $month_updates) : ?>
                    <section class="kb-product-updates__month">
                        <?php if ('' !== $month) : ?><h3><?php echo esc_html($month); ?></h3><?php endif; ?>
                        <ul class="kb-product-updates__list">
                            <?php foreach ($month_updates as $update) : ?>
                                <li>
                                    <div class="kb-product-updates__meta">
                                        <?php if (! empty($update['link'])) : ?>
                                            <strong><a href="<?php echo esc_url((string) $update['link']); ?>" target="_blank" rel="noopener"><?php echo esc_html((string) ($update['product'] ?: __('Produkt', 'kb-markdown-importer'))); ?></a></strong>
                                        <?php else : ?>
                                            <strong><?php echo esc_html((string) ($update['product'] ?: __('Produkt', 'kb-markdown-importer'))); ?></strong>
                                        <?php endif; ?>
                                        <?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_lines']) && is_array($update['changelog_lines'])) : ?>
                                        <ul class="kb-product-updates__changes">
                                            <?php foreach ($update['changelog_lines'] as $line) : ?>
                                                <li><?php echo esc_html((string) $line); ?></li>
                                            <?php endforeach; ?>
                                        </ul>
                                    <?php elseif (! empty($update['changelog'])) : ?>
                                        <p><?php echo esc_html((string) $update['changelog']); ?></p>
                                    <?php endif; ?>
                                    <?php if (! empty($update['starface_min']) || ! empty($update['starface_max']) || ! empty($update['link']) || ! empty($update['download_link'])) : ?>
                                        <dl class="kb-product-updates__details">
                                            <?php if (! empty($update['starface_min']) || ! empty($update['starface_max'])) : ?>
                                                <div>
                                                    <dt><?php esc_html_e('STARFACE', 'kb-markdown-importer'); ?></dt>
                                                    <dd><?php echo esc_html(trim((string) ($update['starface_min'] ?? '') . ' - ' . (string) ($update['starface_max'] ?? ''), ' -')); ?></dd>
                                                </div>
                                            <?php endif; ?>
                                            <?php if (! empty($update['link'])) : ?>
                                                <div>
                                                    <dt><?php esc_html_e('Dokumentation', 'kb-markdown-importer'); ?></dt>
                                                    <dd><a href="<?php echo esc_url((string) $update['link']); ?>" target="_blank" rel="noopener"><?php echo esc_html((string) $update['link']); ?></a></dd>
                                                </div>
                                            <?php endif; ?>
                                            <?php if (! empty($update['download_link'])) : ?>
                                                <div>
                                                    <dt><?php esc_html_e('Download', 'kb-markdown-importer'); ?></dt>
                                                    <dd><a href="<?php echo esc_url((string) $update['download_link']); ?>" target="_blank" rel="noopener">get.o-byte.com</a></dd>
                                                </div>
                                            <?php endif; ?>
                                        </dl>
                                    <?php endif; ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </section>
                    <?php endforeach; ?>
            <?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-groups">
        <?php foreach ($products_by_category as $category => $category_products) : ?>
            <section class="kb-product-category">
                <h2><?php echo esc_html((string) $category); ?></h2>
                <div class="kb-product-list">
                    <?php foreach ((array) $category_products as $item) : ?>
                        <?php $term = $item['term']; ?>
                        <?php $latest = $item['versions'][0] ?? null; ?>
                        <section class="kb-product-card">
                            <h3><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></h3>
                            <?php if (! empty($item['parts']) && count((array) $item['parts']) > 1) : ?>
                                <p class="kb-product-card__parts"><?php echo esc_html(implode(', ', array_map(static fn (array $part): string => (string) $part['label'], (array) $item['parts']))); ?></p>
                            <?php endif; ?>
                            <?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>
        <?php endforeach; ?>
    </div>
</section>
