<?php

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

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

$mode           = isset($attributes['mode'])           ? (string) $attributes['mode'] : 'top';
$limit          = isset($attributes['limit'])          ? (int)    $attributes['limit'] : 10;
$slides_per_view = isset($attributes['slidesPerView']) ? (int)    $attributes['slidesPerView'] : 5;
$title          = isset($attributes['title'])          ? (string) $attributes['title'] : '';
$subtitle       = isset($attributes['subtitle'])       ? (string) $attributes['subtitle'] : '';
$heading_feature = !empty($attributes['headingFeature']);

$title_cls      = $heading_feature ? 'aieo-recommendations-title' : 'aieo-brand-slider__title';
$cta_label      = isset($attributes['ctaLabel'])       ? (string) $attributes['ctaLabel'] : '';
$cta_url        = isset($attributes['ctaUrl'])         ? (string) $attributes['ctaUrl'] : '';
$show_counts    = !empty($attributes['showCounts']);
$card_style     = isset($attributes['cardStyle'])      ? (string) $attributes['cardStyle'] : 'hero';
$manual_ids     = isset($attributes['manualBrandIds']) && is_array($attributes['manualBrandIds'])
    ? array_map('intval', $attributes['manualBrandIds'])
    : array();

$valid_modes = array('top', 'new_brand', 'new_product', 'favourites', 'manual');
if (!in_array($mode, $valid_modes, true)) { $mode = 'top'; }
if (!in_array($card_style, array('hero', 'logo'), true)) { $card_style = 'hero'; }
$limit = max(1, min(50, $limit));
$slides_per_view = max(1, min(8, $slides_per_view));

$bv     = AIEO_Core_Brand_Vitals::instance();
$brands = array();

switch ($mode) {
    case 'new_brand':   $brands = $bv->get_newest_brand($limit); break;
    case 'new_product': $brands = $bv->get_newest_product($limit); break;
    case 'favourites':
        $uid = get_current_user_id();
        if ($uid > 0) { $brands = $bv->get_for_user($uid, $limit); }
        
        
        if (empty($brands)) { return ''; }
        break;
    case 'manual':
        if (!empty($manual_ids)) { $brands = $bv->get_by_ids($manual_ids); }
        if (count($brands) > $limit) { $brands = array_slice($brands, 0, $limit); }
        break;
    case 'top':
    default:
        $brands = $bv->get_top($limit);
}

if (empty($brands)) { return ''; }

$wrapper_cls = 'aieo-brand-slider aieo-brand-slider--' . esc_attr($mode) . ' is-style-' . esc_attr($card_style)
    . ($heading_feature ? ' aieo-brand-slider--heading-feature' : '');
$wrapper_attrs = function_exists('get_block_wrapper_attributes')
    ? get_block_wrapper_attributes(array('class' => $wrapper_cls))
    : 'class="' . $wrapper_cls . '"';

$nonce = wp_create_nonce('aieo_brand_slider');

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>
     data-mode="<?php echo esc_attr($mode); ?>"
     data-limit="<?php echo esc_attr((string) $limit); ?>"
     data-slides-per-view="<?php echo esc_attr((string) $slides_per_view); ?>"
     data-nonce="<?php echo esc_attr($nonce); ?>"
     data-card-style="<?php echo esc_attr($card_style); ?>">

    <?php if ($title !== '' || $subtitle !== '') : ?>
        <header class="aieo-brand-slider__header">
            <?php if ($title !== '') : ?>
                <h2 class="<?php echo esc_attr($title_cls); ?>"><?php echo esc_html($title); ?></h2>
            <?php endif; ?>
            <?php if ($subtitle !== '') : ?>
                <p class="aieo-brand-slider__subtitle"><?php echo esc_html($subtitle); ?></p>
            <?php endif; ?>
        </header>
    <?php endif; ?>

    <div class="aieo-brand-slider__viewport"
         style="--aieo-spv: <?php echo esc_attr((string) $slides_per_view); ?>;">
        <button type="button"
                class="aieo-brand-slider__nav aieo-brand-slider__nav--prev"
                aria-label="<?php esc_attr_e('Previous brands', 'ai-eshop-optimizer'); ?>"
                hidden>‹</button>

        <ul class="aieo-brand-slider__track" role="list">
            <?php foreach ($brands as $b) : ?>
                <?php
                
                
                
                $img_kind = '';
                $img_url  = '';
                if ($card_style === 'hero' && !empty($b['hero_url'])) {
                    $img_kind = 'hero';
                    $img_url  = (string) $b['hero_url'];
                } elseif (!empty($b['logo_url'])) {
                    $img_kind = 'logo';
                    $img_url  = (string) $b['logo_url'];
                }
                ?>
                <li class="aieo-brand-slider__slide">
                    <a class="aieo-brand-slide aieo-brand-slide--<?php echo esc_attr($card_style); ?> has-img-<?php echo esc_attr($img_kind ?: 'none'); ?>"
                       href="<?php echo esc_url((string) $b['permalink']); ?>">
                        <?php if ($img_kind === 'hero') : ?>
                            <span class="aieo-brand-slide__hero">
                                <img loading="lazy"
                                     src="<?php echo esc_url($img_url); ?>"
                                     alt="<?php echo esc_attr((string) $b['name']); ?>" />
                            </span>
                        <?php elseif ($img_kind === 'logo') : ?>
                            <span class="aieo-brand-slide__logo">
                                <img loading="lazy"
                                     src="<?php echo esc_url($img_url); ?>"
                                     alt="<?php echo esc_attr((string) $b['name']); ?>" />
                            </span>
                        <?php else : ?>
                            <span class="aieo-brand-slide__placeholder" aria-hidden="true">
                                <span class="aieo-brand-slide__placeholder-glyph"><?php echo esc_html(mb_substr((string) $b['name'], 0, 1, 'UTF-8')); ?></span>
                            </span>
                        <?php endif; ?>
                        <span class="aieo-brand-slide__name"><?php echo esc_html((string) $b['name']); ?></span>
                        <?php if ($show_counts) : ?>
                            <span class="aieo-brand-slide__meta"><?php
                                printf(
                                    
                                    esc_html__('%d products', 'ai-eshop-optimizer'),
                                    (int) $b['product_count']
                                );
                            ?></span>
                        <?php endif; ?>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>

        <button type="button"
                class="aieo-brand-slider__nav aieo-brand-slider__nav--next"
                aria-label="<?php esc_attr_e('Next brands', 'ai-eshop-optimizer'); ?>">›</button>
    </div>

    <?php if ($cta_label !== '' && $cta_url !== '') : ?>
        <div class="aieo-brand-slider__footer">
            <a class="aieo-brand-slider__cta" href="<?php echo esc_url($cta_url); ?>">
                <?php echo esc_html($cta_label); ?>
            </a>
        </div>
    <?php endif; ?>
</div>
<?php
echo ob_get_clean(); 
