<?php

if (!defined('ABSPATH')) {
    return;
}
if (!class_exists('AIEO_Core_Brand_Vitals') || !class_exists('AIEO_DMM_Brand_Extras_Data')) {
    return;
}

$show_counts   = !empty($attributes['showCounts']);
$show_wishlist = !empty($attributes['showWishlist']);
$show_rating   = !empty($attributes['showRating']);

$qo = get_queried_object();
if (!($qo instanceof \WP_Term) || $qo->taxonomy !== 'product_brand') {
    return '';
}

$bv     = AIEO_Core_Brand_Vitals::instance();
$vitals = $bv->get_by_id((int) $qo->term_id);
$extras = AIEO_DMM_Brand_Extras_Data::get('product_brand', (int) $qo->term_id);

$brand_name = $vitals['name'] ?? $qo->name;
$hero_url   = $vitals['hero_url'] ?? '';
$logo_url   = $vitals['logo_url'] ?? '';
$headline   = isset($extras['headline']) ? (string) $extras['headline'] : '';
$count_total = (int) ($vitals['product_count']  ?? 0);
$count_stock = (int) ($vitals['in_stock_count'] ?? 0);
$rating_avg  = isset($vitals['rating_avg']) ? $vitals['rating_avg'] : null;
$rating_n    = (int) ($vitals['rating_count'] ?? 0);

$scope_category = null;
$scope_count    = null;
if ( ! empty( $_GET['in_cat'] ) ) { 
    $aieo_in_cat = absint( wp_unslash( $_GET['in_cat'] ) ); 
    if ( $aieo_in_cat > 0 ) {
        $aieo_tq = array(
            'relation' => 'AND',
            array( 'taxonomy' => $qo->taxonomy, 'field' => 'term_id', 'terms' => (int) $qo->term_id ),
            array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => array( $aieo_in_cat ), 'include_children' => true ),
        );
        if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
            $aieo_tq[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'outofstock', 'operator' => 'NOT IN' );
        }
        $aieo_scoped = new WP_Query( array(
            'post_type'      => 'product',
            'post_status'    => 'publish',
            'posts_per_page' => 1,
            'fields'         => 'ids',
            'no_found_rows'  => false,
            'tax_query'      => $aieo_tq, 
        ) );
        $scope_count   = (int) $aieo_scoped->found_posts;
        $aieo_cat_term = get_term( $aieo_in_cat, 'product_cat' );
        if ( $aieo_cat_term && ! is_wp_error( $aieo_cat_term ) ) {
            $aieo_cat_link  = get_term_link( $aieo_cat_term );
            $scope_category = array(
                'name' => (string) $aieo_cat_term->name,
                'url'  => is_string( $aieo_cat_link ) ? $aieo_cat_link : '',
            );
        }
    }
}

$wrapper_attrs = function_exists('get_block_wrapper_attributes')
    ? get_block_wrapper_attributes(array('class' => 'aieo-brand-hero' . ($hero_url ? ' has-hero-image' : '')))
    : 'class="aieo-brand-hero' . ($hero_url ? ' has-hero-image' : '') . '"';

ob_start();
?>
<section <?php echo $wrapper_attrs; ?>
         data-brand-id="<?php echo esc_attr((string) $qo->term_id); ?>"
         data-brand-slug="<?php echo esc_attr((string) $qo->slug); ?>">

    <div class="aieo-brand-hero__media">
        <?php if ($hero_url) : ?>
            <img class="aieo-brand-hero__bg"
                 src="<?php echo esc_url($hero_url); ?>"
                 alt=""
                 loading="eager" />
        <?php endif; ?>
        <div class="aieo-brand-hero__scrim" aria-hidden="true"></div>
    </div>

    <div class="aieo-brand-hero__body">
        <?php if ($logo_url) : ?>
            <div class="aieo-brand-hero__logo">
                <img src="<?php echo esc_url($logo_url); ?>"
                     alt="<?php echo esc_attr($brand_name); ?>" />
            </div>
        <?php endif; ?>

        <h1 class="aieo-brand-hero__title"><?php echo esc_html($brand_name); ?></h1>

        <?php if ( ! empty( $scope_category ) ) : ?>
            <div class="aieo-brand-hero__scope" style="margin-top:6px;font-size:0.95rem;opacity:0.9;">
                <?php
                $aieo_sc_html = $scope_category['url'] !== ''
                    ? '<a href="' . esc_url( $scope_category['url'] ) . '">' . esc_html( $scope_category['name'] ) . '</a>'
                    : esc_html( $scope_category['name'] );
                
                printf( esc_html__( 'στην κατηγορία: %s', 'ai-eshop-optimizer' ), $aieo_sc_html ); 
                ?>
            </div>
        <?php endif; ?>

        <?php if ($headline !== '') : ?>
            <?php
            
            
            
            ?>
            <p class="aieo-brand-hero__headline"><?php echo wp_kses_post($headline); ?></p>
        <?php endif; ?>

        <div class="aieo-brand-hero__meta">
            <?php if ($show_counts && $scope_count !== null) : ?>
                <span class="aieo-brand-hero__count">
                    <?php
                    
                    printf( esc_html( _n('%d προϊόν', '%d προϊόντα', $scope_count, 'ai-eshop-optimizer') ), (int) $scope_count );
                    ?>
                </span>
            <?php elseif ($show_counts && $count_total > 0) : ?>
                <span class="aieo-brand-hero__count">
                    <?php
                    printf(
                        
                        esc_html__('%1$d προϊόντα · %2$d ποικιλίες σε απόθεμα', 'ai-eshop-optimizer'),
                        $count_total, 
                        $count_stock 
                    );
                    ?>
                </span>
            <?php endif; ?>

            <?php if ($show_rating && $rating_avg !== null && $rating_n > 0) : ?>
                <span class="aieo-brand-hero__rating" aria-label="<?php
                    
                    echo esc_attr(sprintf(__('%1$.1f out of 5, %2$d reviews', 'ai-eshop-optimizer'), (float) $rating_avg, $rating_n));
                ?>">
                    <span class="aieo-brand-hero__rating-stars" data-value="<?php echo esc_attr((string) $rating_avg); ?>" aria-hidden="true">★★★★★</span>
                    <span class="aieo-brand-hero__rating-meta">
                        <?php echo esc_html(number_format_i18n((float) $rating_avg, 1)); ?>
                        <span class="aieo-brand-hero__rating-count">(<?php echo esc_html(number_format_i18n($rating_n)); ?>)</span>
                    </span>
                </span>
            <?php endif; ?>
        </div>

        <?php if ($show_wishlist && ( ! class_exists('AIEO_Wishlist') || AIEO_Wishlist::is_type_enabled('brand') ) ) :
            
            
            
            
            
            
            $is_in_list = false;
            if (class_exists('AIEO_Wishlist')) {
                $is_in_list = AIEO_Wishlist::instance()->is_wishlisted((int) $qo->term_id, 'brand');
            }
            $btn_classes   = 'aieo-brand-follow' . ($is_in_list ? ' is-in-list' : '');
            $label_add     = __('Follow brand', 'ai-eshop-optimizer');
            $label_in      = __('Following', 'ai-eshop-optimizer');
            $current_label = $is_in_list ? $label_in : $label_add;
            $nonce         = wp_create_nonce('aieo_wishlist');
            $ajax_url      = admin_url('admin-ajax.php');
        ?>
            <div class="aieo-brand-hero__actions">
                <button type="button"
                        class="<?php echo esc_attr($btn_classes); ?>"
                        data-brand-id="<?php echo esc_attr((string) $qo->term_id); ?>"
                        data-ajax-url="<?php echo esc_url($ajax_url); ?>"
                        data-nonce="<?php echo esc_attr($nonce); ?>"
                        data-label-add="<?php echo esc_attr($label_add); ?>"
                        data-label-in="<?php echo esc_attr($label_in); ?>"
                        aria-pressed="<?php echo $is_in_list ? 'true' : 'false'; ?>"
                        aria-label="<?php echo esc_attr($current_label); ?>">
                    <span class="aieo-brand-follow__icon" aria-hidden="true">
                        <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                            <path class="aieo-brand-follow__icon-outline" d="M12 21s-7.5-4.7-9.5-9.4C1.1 8.3 3 5 6.2 5c1.9 0 3.4 1 4.5 2.7C11.7 6 13.2 5 15.1 5c3.2 0 5.1 3.3 3.7 6.6C18.5 16.3 12 21 12 21z" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/>
                            <path class="aieo-brand-follow__icon-fill"    d="M12 21s-7.5-4.7-9.5-9.4C1.1 8.3 3 5 6.2 5c1.9 0 3.4 1 4.5 2.7C11.7 6 13.2 5 15.1 5c3.2 0 5.1 3.3 3.7 6.6C18.5 16.3 12 21 12 21z" fill="currentColor"/>
                        </svg>
                    </span>
                    <span class="aieo-brand-follow__label"><?php echo esc_html($current_label); ?></span>
                </button>
            </div>
        <?php endif; ?>
    </div>
</section>
<?php
echo ob_get_clean(); 
