<?php

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

if (!class_exists('AIEO_Core_Brand_Vitals') || !function_exists('render_block')) {
    return;
}

$show_featured   = !empty($attributes['showFeatured']);
$show_new        = !empty($attributes['showNew']);
$show_favourites = !empty($attributes['showFavourites']);
$show_top        = !empty($attributes['showTop']);
$new_mode        = isset($attributes['newMode']) && $attributes['newMode'] === 'new_brand' ? 'new_brand' : 'new_product';

$default_limit  = isset($attributes['limitPerRail'])  ? (int) $attributes['limitPerRail']  : 10;
$default_slides = isset($attributes['slidesPerView']) ? (int) $attributes['slidesPerView'] : 5;
$default_limit  = max(3, min(30, $default_limit));
$default_slides = max(2, min(8, $default_slides));

$rail_limit = function ($key) use ($attributes, $default_limit) {
    $v = isset($attributes[$key]) ? (int) $attributes[$key] : 0;
    return $v > 0 ? max(3, min(50, $v)) : $default_limit;
};
$rail_slides = function ($key) use ($attributes, $default_slides) {
    $v = isset($attributes[$key]) ? (int) $attributes[$key] : 0;
    return $v > 0 ? max(2, min(8, $v)) : $default_slides;
};

$rail = function ($mode, $title, $subtitle, $card_style, $limit, $slides) {
    return render_block(array(
        'blockName' => 'aieo/brand-slider',
        'attrs'     => array(
            'mode'          => $mode,
            'limit'         => (int) $limit,
            'slidesPerView' => (int) $slides,
            'title'         => $title,
            'subtitle'      => $subtitle,
            'cardStyle'     => $card_style,
        ),
        'innerBlocks'  => array(),
        'innerHTML'    => '',
        'innerContent' => array(),
    ));
};

$wrapper_attrs = function_exists('get_block_wrapper_attributes')
    ? get_block_wrapper_attributes(array('class' => 'aieo-brand-rails'))
    : 'class="aieo-brand-rails"';

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

    <?php if ($show_top) :
        echo $rail( 
            'top',
            __('Top Brands', 'ai-eshop-optimizer'), 
            __('The brands our customers love most.', 'ai-eshop-optimizer'), 
            'logo',
            $rail_limit('topLimit'), 
            $rail_slides('topSlidesPerView') 
        );
    endif; ?>

    <?php if ($show_featured) :
        echo $rail( 
            'top',
            __('Featured Brands', 'ai-eshop-optimizer'), 
            __('Curated picks from the catalogue.', 'ai-eshop-optimizer'), 
            'hero',
            $rail_limit('featuredLimit'), 
            $rail_slides('featuredSlidesPerView') 
        );
    endif; ?>

    <?php if ($show_new) :
        echo $rail( 
            $new_mode,
            __('ΝΕΑ BRANDS', 'ai-eshop-optimizer'), 
            __('New arrivals and freshly landed brands.', 'ai-eshop-optimizer'), 
            'hero',
            $rail_limit('newLimit'), 
            $rail_slides('newSlidesPerView') 
        );
    endif; ?>

    <?php if ($show_favourites && is_user_logged_in()) :
        echo $rail( 
            'favourites',
            __('Your Favourite Brands', 'ai-eshop-optimizer'), 
            __('Based on your shopping history.', 'ai-eshop-optimizer'), 
            'hero',
            $rail_limit('favouritesLimit'), 
            $rail_slides('favouritesSlidesPerView') 
        );
    endif; ?>
</div>
<?php
echo ob_get_clean(); 
