<?php

if ( ! defined( 'ABSPATH' ) ) { return; }
if ( ! class_exists( 'AIEO_DMM_Term_Relations' ) ) { 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' ) { return; }

$limit   = isset( $attributes['limit'] ) ? max( 1, min( 8, (int) $attributes['limit'] ) ) : 3;
$layout  = ( isset( $attributes['layout'] ) && $attributes['layout'] === 'horizontal' ) ? 'horizontal' : 'vertical';
$heading = isset( $attributes['heading'] ) ? trim( (string) $attributes['heading'] ) : '';
if ( $heading === '' ) {
    $heading = __( 'From the Blog', 'ai-eshop-optimizer' );
}

$rels = AIEO_DMM_Term_Relations::for_term( $taxonomy, $term_id, 'post', 'post' );
if ( empty( $rels ) ) { return; }

$articles = array();
$i = 0;
foreach ( $rels as $r ) {
    if ( $i >= $limit ) { break; }
    $post_id = (int) ( $r['id'] ?? 0 );
    if ( $post_id <= 0 ) { continue; }
    $post = get_post( $post_id );
    if ( ! $post || $post->post_status !== 'publish' ) { continue; }

    $articles[] = array(
        'id'        => $post_id,
        'title'     => get_the_title( $post_id ),
        'permalink' => (string) get_permalink( $post_id ),
        'thumb'     => (string) get_the_post_thumbnail_url( $post_id, 'medium' ),
        'excerpt'   => wp_strip_all_tags( (string) get_the_excerpt( $post_id ) ),
    );
    $i++;
}

if ( empty( $articles ) ) { return; }

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'         => 'aieo-brand-related aieo-brand-related--' . esc_attr( $layout )
                         . ' aieo-brand-related--for-tax-' . sanitize_html_class( $taxonomy ),
        'data-taxonomy' => $taxonomy,
        'data-term-id'  => (string) $term_id,
    ) )
    : 'class="aieo-brand-related aieo-brand-related--' . esc_attr( $layout )
        . ' aieo-brand-related--for-tax-' . esc_attr( sanitize_html_class( $taxonomy ) ) . '"';

ob_start();
?>
<section <?php echo $wrapper_attrs; ?>>
    <div class="aieo-brand-related__row aieo-brand-related__row--articles">
        <h3 class="aieo-brand-related__heading"><?php echo esc_html( $heading ); ?></h3>
        <ul class="aieo-brand-related__articles" role="list">
            <?php foreach ( $articles as $a ) :
                $bg_style = ! empty( $a['thumb'] )
                    ? 'background-image:url(' . esc_url( $a['thumb'] ) . ')'
                    : '';
            ?>
                <li class="aieo-article-card<?php echo empty( $a['thumb'] ) ? ' has-no-thumb' : ''; ?>">
                    <a class="aieo-article-card__link"
                       href="<?php echo esc_url( $a['permalink'] ); ?>"
                       style="<?php echo esc_attr( $bg_style ); ?>">
                        <span class="aieo-article-card__overlay">
                            <span class="aieo-article-card__title"><?php echo esc_html( $a['title'] ); ?></span>
                            <?php if ( ! empty( $a['excerpt'] ) ) : ?>
                                <span class="aieo-article-card__excerpt"><?php echo esc_html( wp_trim_words( $a['excerpt'], 14 ) ); ?></span>
                            <?php endif; ?>
                        </span>
                    </a>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
</section>
<?php
echo ob_get_clean(); 
