<?php

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

if ( ! is_singular( 'post' ) ) { return; }

$post_id = (int) get_the_ID();
if ( $post_id <= 0 ) { return; }

$rels = AIEO_DMM_Term_Relations::for_post( $post_id, 'post' );
if ( empty( $rels ) ) { return; }

$image_size  = isset( $attributes['imageSize'] ) ? (string) $attributes['imageSize'] : 'medium';
$auto_rotate = ! isset( $attributes['autoRotate'] ) || (bool) $attributes['autoRotate'];
$rotate_ms   = isset( $attributes['rotateMs'] ) ? max( 1500, (int) $attributes['rotateMs'] ) : 4000;

$brands = array();
foreach ( $rels as $r ) {
    if ( ( $r['taxonomy'] ?? '' ) !== 'product_brand' ) { continue; }
    $term_id = (int) ( $r['term_id'] ?? 0 );
    if ( $term_id <= 0 ) { continue; }
    $term = get_term( $term_id, 'product_brand' );
    if ( ! $term || is_wp_error( $term ) ) { continue; }

    
    
    $logo_id = 0;
    if ( class_exists( 'AIEO_DMM_Brand_Extras_Data' ) ) {
        $extras = AIEO_DMM_Brand_Extras_Data::get( 'product_brand', $term_id );
        if ( ! empty( $extras['logo_image_id'] ) ) {
            $logo_id = (int) $extras['logo_image_id'];
        }
    }
    if ( ! $logo_id ) {
        $logo_id = (int) get_term_meta( $term_id, 'thumbnail_id', true );
    }
    if ( ! $logo_id ) { continue; }

    $logo_url = wp_get_attachment_image_url( $logo_id, $image_size );
    if ( ! $logo_url ) {
        $logo_url = wp_get_attachment_image_url( $logo_id, 'full' );
    }
    if ( ! $logo_url ) { continue; }

    $brands[] = array(
        'name'    => $term->name,
        'slug'    => $term->slug,
        'url'     => (string) get_term_link( $term ),
        'logoUrl' => (string) $logo_url,
    );
}

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

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'             => 'aieo-post-related-brands',
        'data-auto-rotate'  => $auto_rotate ? '1' : '0',
        'data-rotate-ms'    => (string) $rotate_ms,
    ) )
    : 'class="aieo-post-related-brands" data-auto-rotate="' . ( $auto_rotate ? '1' : '0' ) . '" data-rotate-ms="' . (int) $rotate_ms . '"';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>
    <div class="aieo-post-related-brands__track">
        <?php foreach ( $brands as $b ) : ?>
            <div class="aieo-post-related-brands__item">
                <a href="<?php echo esc_url( $b['url'] ); ?>"
                   title="<?php echo esc_attr( sprintf(  __( 'Δείτε τα προϊόντα του brand: %s', 'ai-eshop-optimizer' ), $b['name'] ) ); ?>"
                   aria-label="<?php echo esc_attr( sprintf(  __( 'Δείτε τα προϊόντα του brand: %s', 'ai-eshop-optimizer' ), $b['name'] ) ); ?>">
                    <img class="aieo-post-related-brands__logo"
                         src="<?php echo esc_url( $b['logoUrl'] ); ?>"
                         alt="<?php echo esc_attr( sprintf(  __( 'Logo of %s', 'ai-eshop-optimizer' ), $b['name'] ) ); ?>"
                         loading="lazy" decoding="async" />
                </a>
            </div>
        <?php endforeach; ?>
    </div>
    <?php if ( count( $brands ) > 1 ) : ?>
        <div class="aieo-post-related-brands__bullets" aria-hidden="true"></div>
    <?php endif; ?>
</div>
<?php
echo ob_get_clean(); 
