<?php

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

$count          = isset( $attributes['posts'] ) ? max( 1, min( 12, (int) $attributes['posts'] ) ) : 5;
$image_size     = isset( $attributes['imageSize'] ) ? (string) $attributes['imageSize'] : 'medium';
$exclude_current = ! isset( $attributes['excludeCurrent'] ) || (bool) $attributes['excludeCurrent'];
$card_style     = isset( $attributes['cardStyle'] ) && $attributes['cardStyle'] === 'glass' ? 'glass' : 'overlay';

if ( ! class_exists( 'AIEO_Block_Header' ) && defined( 'AIEO_DIR' ) ) {
    $hp = AIEO_DIR . 'includes/class-aieo-block-header.php';
    if ( is_file( $hp ) ) { require_once $hp; }
}

$args = array(
    'post_type'           => 'post',
    'posts_per_page'      => $count,
    'ignore_sticky_posts' => 1,
    'no_found_rows'       => true,
);
if ( $exclude_current && is_singular( 'post' ) ) {
    $current = (int) get_the_ID();
    if ( $current > 0 ) {
        $args['post__not_in'] = array( $current ); 
    }
}

$q = new WP_Query( $args );
if ( ! $q->have_posts() ) { return; }

$wrapper_cls = 'aieo-latest-posts-overlay aieo-latest-posts-overlay--' . $card_style;
$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array( 'class' => $wrapper_cls ) )
    : 'class="' . esc_attr( $wrapper_cls ) . '"';

$header_html = class_exists( 'AIEO_Block_Header' ) ? AIEO_Block_Header::render( $attributes ) : '';

ob_start();
?>
<div <?php echo $wrapper_attrs; ?>>
    <?php echo $header_html; ?>
    <div class="aieo-latest-posts-overlay__grid">
        <?php while ( $q->have_posts() ) : $q->the_post();
            if ( ! has_post_thumbnail() ) { continue; }
            $thumb_id = get_post_thumbnail_id( get_the_ID() );
            $thumb    = wp_get_attachment_image_src( $thumb_id, $image_size );
            if ( ! $thumb ) { continue; }
            $img_url = $thumb[0];
            $img_w   = (int) ( $thumb[1] ?? 416 );
            $img_h   = (int) ( $thumb[2] ?? 333 );

            $title     = get_the_title();
            $permalink = get_the_permalink();
            $cats      = get_the_category();
            $cat_name  = '';
            $cat_link  = '';
            if ( ! empty( $cats ) ) {
                $first    = $cats[0];
                $cat_name = $first->name;
                $cat_link = (string) get_category_link( $first->term_id );
            }
            ?>
            <div class="aieo-latest-posts-overlay__item">
                <a class="aieo-latest-posts-overlay__link"
                   href="<?php echo esc_url( $permalink ); ?>"
                   aria-label="<?php echo esc_attr( sprintf(  __( 'Read more: %s', 'ai-eshop-optimizer' ), $title ) ); ?>">
                    <span class="aieo-latest-posts-overlay__image-wrap">
                        <img src="<?php echo esc_url( $img_url ); ?>"
                             width="<?php echo esc_attr( $img_w ); ?>"
                             height="<?php echo esc_attr( $img_h ); ?>"
                             alt="<?php echo esc_attr( $title ); ?>"
                             loading="lazy" decoding="async" />
                        <span class="aieo-latest-posts-overlay__veil">
                            <span class="aieo-latest-posts-overlay__text">
                                <?php if ( $cat_name && $cat_link ) : ?>
                                    <span class="aieo-latest-posts-overlay__cat">
                                        <?php echo esc_html( $cat_name ); ?>
                                    </span>
                                <?php endif; ?>
                                <span class="aieo-latest-posts-overlay__title">
                                    <?php echo esc_html( $title ); ?>
                                </span>
                            </span>
                        </span>
                    </span>
                </a>
                <?php if ( $cat_name && $cat_link ) : ?>
                    <a class="aieo-latest-posts-overlay__cat-link"
                       href="<?php echo esc_url( $cat_link ); ?>"
                       aria-label="<?php echo esc_attr( sprintf(  __( 'Category: %s', 'ai-eshop-optimizer' ), $cat_name ) ); ?>"></a>
                <?php endif; ?>
            </div>
        <?php endwhile; wp_reset_postdata(); ?>
    </div>
    <div class="aieo-latest-posts-overlay__bullets" aria-hidden="true"></div>
</div>
<?php
echo ob_get_clean(); 
