<?php

if (!defined('ABSPATH')) {
    return;
}

if (!class_exists('AIEO_Core_Brand_Vitals')) {
    return;
}

$show_counts     = !empty($attributes['showCounts']);
$show_logos      = !empty($attributes['showLogos']);
$sticky_bar      = !empty($attributes['stickyBar']);
$heading_level   = isset($attributes['headingLevel']) ? (string) $attributes['headingLevel'] : 'h2';
$show_hash       = !empty($attributes['showHashBucket']);
if (!in_array($heading_level, array('h2', 'h3', 'h4'), true)) { $heading_level = 'h2'; }

$bv      = AIEO_Core_Brand_Vitals::instance();
$grouped = $bv->get_all_grouped();

$letters = range('A', 'Z');
if ($show_hash) { $letters[] = '#'; }

$wrapper_attrs = function_exists('get_block_wrapper_attributes')
    ? get_block_wrapper_attributes(array(
        'class' => 'aieo-brand-azindex' . ($sticky_bar ? ' has-sticky-bar' : ''),
    ))
    : 'class="aieo-brand-azindex' . ($sticky_bar ? ' has-sticky-bar' : '') . '"';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>

    <nav class="aieo-azbar<?php echo $sticky_bar ? ' aieo-azbar--sticky' : ''; ?>"
         aria-label="<?php esc_attr_e('Brand A to Z navigation', 'ai-eshop-optimizer'); ?>"
         data-sticky="<?php echo $sticky_bar ? '1' : '0'; ?>">
        <ul class="aieo-azbar__list" role="list">
            <?php foreach ($letters as $L) :
                $bucket = isset($grouped[$L]) ? $grouped[$L] : array('count' => 0, 'brands' => array());
                $count  = (int) $bucket['count'];
                $empty  = $count === 0;
            ?>
                <li class="aieo-azbar__item">
                    <a class="aieo-azbar__chip<?php echo $empty ? ' is-empty' : ''; ?>"
                       href="#letter-<?php echo esc_attr($L); ?>"
                       data-letter="<?php echo esc_attr($L); ?>"
                       data-empty="<?php echo $empty ? '1' : '0'; ?>"
                       data-count="<?php echo esc_attr((string) $count); ?>"
                       <?php if ($empty) : ?>aria-disabled="true" tabindex="-1"<?php endif; ?>>
                        <?php echo esc_html($L); ?>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
        <span class="aieo-azbar__active-meta" aria-live="polite"></span>
    </nav>

    <div class="aieo-azindex-sections">
        <?php foreach ($letters as $L) :
            $bucket = isset($grouped[$L]) ? $grouped[$L] : array('count' => 0, 'brands' => array());
            $count  = (int) $bucket['count'];
            $brands = is_array($bucket['brands']) ? $bucket['brands'] : array();
        ?>
            <section
                id="letter-<?php echo esc_attr($L); ?>"
                class="aieo-letter<?php echo $count === 0 ? ' is-empty' : ''; ?>"
                data-letter="<?php echo esc_attr($L); ?>"
                data-empty="<?php echo $count === 0 ? '1' : '0'; ?>"
                data-count="<?php echo esc_attr((string) $count); ?>">
                <?php if ($count > 0) : ?>
                    <<?php echo esc_html($heading_level); ?> class="aieo-letter__heading">
                        <span class="aieo-letter__glyph"><?php echo esc_html($L); ?></span>
                        <?php if ($show_counts) : ?>
                            <span class="aieo-letter__count">
                                <?php
                                
                                echo esc_html(sprintf(_n('(%d brand)', '(%d brands)', $count, 'ai-eshop-optimizer'), $count));
                                ?>
                            </span>
                        <?php endif; ?>
                    </<?php echo esc_html($heading_level); ?>>
                    <ul class="aieo-letter__grid" role="list">
                        <?php foreach ($brands as $b) : ?>
                            <li class="aieo-brand-card<?php echo !empty($b['promo']) ? ' is-promo' : ''; ?>">
                                <a class="aieo-brand-card__link" href="<?php echo esc_url((string) $b['permalink']); ?>">
                                    <?php if ($show_logos && !empty($b['logo_url'])) : ?>
                                        <span class="aieo-brand-card__logo">
                                            <img loading="lazy"
                                                 src="<?php echo esc_url((string) $b['logo_url']); ?>"
                                                 alt="<?php echo esc_attr((string) $b['name']); ?>" />
                                        </span>
                                    <?php endif; ?>
                                    <span class="aieo-brand-card__body">
                                        <span class="aieo-brand-card__name"><?php echo esc_html((string) $b['name']); ?></span>
                                        <?php if ($show_counts) : ?>
                                            <span class="aieo-brand-card__meta">
                                                <?php
                                                printf(
                                                    
                                                    esc_html__('%1$d προϊόντα · %2$d ποικιλίες σε απόθεμα', 'ai-eshop-optimizer'),
                                                    (int) $b['product_count'],
                                                    (int) $b['in_stock_count']
                                                );
                                                ?>
                                            </span>
                                        <?php endif; ?>
                                    </span>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </section>
        <?php endforeach; ?>
    </div>
</div>
<?php
echo ob_get_clean(); 
