<?php

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

$qo = get_queried_object();
if ( ! ( $qo instanceof \WP_Term ) ) {
    return;
}

$taxonomy = (string) $qo->taxonomy;
$term_id  = (int) $qo->term_id;

if ( $taxonomy === 'product_brand' ) {
    echo do_blocks( '<!-- wp:aieo/brand-archive-hero {"showCounts":true,"showWishlist":true,"showRating":true} /-->' ); 
    return;
}

if ( $taxonomy === 'product_cat' ) {
    echo do_blocks( '<!-- wp:aieo/category-hero {"align":"full"} /-->' ); 
    return;
}

$show_counts   = ! isset( $attributes['showCounts'] )   || ! empty( $attributes['showCounts'] );
$show_headline = ! isset( $attributes['showHeadline'] ) || ! empty( $attributes['showHeadline'] );

$data = array(
    'name'          => (string) $qo->name,
    'slug'          => (string) $qo->slug,
    'description'   => (string) ( $qo->description ?? '' ),
    'product_count' => (int) ( $qo->count ?? 0 ),
    'hero_url'      => '',
    'icon_url'      => '',
    'headline'      => '',
    'footer_text'   => '',
);

if ( $taxonomy === 'pa_need' && ! 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' => $taxonomy,      'field' => 'term_id', 'terms' => $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, 
        ) );
        $data['product_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 );
            $data['scope_category'] = array(
                'name' => (string) $aieo_cat_term->name,
                'url'  => is_string( $aieo_cat_link ) ? $aieo_cat_link : '',
            );
        }
    }
}

if ( $taxonomy === 'pa_need' && class_exists( 'AIEO_DMM_Need_Extras_Data' ) ) {
    $extras = AIEO_DMM_Need_Extras_Data::get( $taxonomy, $term_id );
    if ( ! empty( $extras['hero_image_id'] ) ) {
        $url = wp_get_attachment_image_url( (int) $extras['hero_image_id'], 'large' );
        if ( $url ) { $data['hero_url'] = $url; }
    }
    if ( ! empty( $extras['icon_image_id'] ) ) {
        $url = wp_get_attachment_image_url( (int) $extras['icon_image_id'], 'thumbnail' );
        if ( $url ) { $data['icon_url'] = $url; }
    }
    $data['headline']    = (string) ( $extras['headline']    ?? '' );
    $data['footer_text'] = (string) ( $extras['footer_text'] ?? '' );
} elseif ( $taxonomy === 'product_tag' ) {
    
    $thumbnail_id = get_term_meta( $term_id, 'thumbnail_id', true );
    if ( $thumbnail_id ) {
        $url = wp_get_attachment_image_url( (int) $thumbnail_id, 'large' );
        if ( $url ) { $data['hero_url'] = $url; }
    }
    
    $data['headline'] = $data['description'];
}

$data = apply_filters( 'aieo_taxonomy_archive_hero_data', $data, $taxonomy, $term_id, $qo );

$has_hero = ! empty( $data['hero_url'] );
$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'              => 'aieo-tax-hero aieo-tax-hero--' . sanitize_html_class( $taxonomy ) . ( $has_hero ? ' has-hero-image' : '' ),
        'data-taxonomy'      => $taxonomy,
        'data-term-id'       => (string) $term_id,
        'data-term-slug'     => (string) $data['slug'],
    ) )
    : 'class="aieo-tax-hero aieo-tax-hero--' . esc_attr( sanitize_html_class( $taxonomy ) ) . ( $has_hero ? ' has-hero-image' : '' ) . '"';

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

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

    <div class="aieo-tax-hero__body">
        <?php if ( ! empty( $data['icon_url'] ) ) : ?>
            <div class="aieo-tax-hero__icon">
                <img src="<?php echo esc_url( $data['icon_url'] ); ?>" alt="<?php echo esc_attr( $data['name'] ); ?>" />
            </div>
        <?php endif; ?>

        <h1 class="aieo-tax-hero__title"><?php echo esc_html( $data['name'] ); ?></h1>

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

        <?php if ( $show_headline && $data['headline'] !== '' ) : ?>
            <div class="aieo-tax-hero__headline"><?php echo wp_kses_post( $data['headline'] ); ?></div>
        <?php endif; ?>

        <?php if ( $show_counts && $data['product_count'] > 0 ) : ?>
            <div class="aieo-tax-hero__meta">
                <span class="aieo-tax-hero__count">
                    <?php
                    printf(
                        
                        esc_html( _n( '%d product', '%d products', (int) $data['product_count'], 'ai-eshop-optimizer' ) ),
                        (int) $data['product_count']
                    );
                    ?>
                </span>
            </div>
        <?php endif; ?>
    </div>

</section>
<?php
echo ob_get_clean(); 
