<?php

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

$attrs       = ( isset( $attributes ) && is_array( $attributes ) ) ? $attributes : array();
$title       = isset( $attrs['title'] ) ? (string) $attrs['title'] : '';
$limit       = isset( $attrs['limit'] ) ? max( 1, min( 40, (int) $attrs['limit'] ) ) : 12;
$orderby     = isset( $attrs['orderby'] ) ? (string) $attrs['orderby'] : 'date';
$show_rating = ! empty( $attrs['showRating'] );

$split = static function ( $val ) {
	$out = array();
	foreach ( explode( ',', (string) $val ) as $p ) {
		$p = trim( $p );
		if ( '' !== $p ) { $out[] = $p; }
	}
	return $out;
};

$to_slugs = static function ( array $vals, $taxonomy ) {
	$slugs = array();
	foreach ( $vals as $v ) {
		if ( is_numeric( $v ) ) {
			$t = get_term( (int) $v, $taxonomy );
			if ( $t && ! is_wp_error( $t ) ) { $slugs[] = $t->slug; }
		} else {
			$slugs[] = sanitize_title( $v );
		}
	}
	return array_values( array_unique( array_filter( $slugs ) ) );
};

$pids      = array_values( array_filter( array_map( 'intval', $split( isset( $attrs['productIds'] ) ? $attrs['productIds'] : '' ) ) ) );
$cat_slugs = $to_slugs( $split( isset( $attrs['categories'] ) ? $attrs['categories'] : '' ), 'product_cat' );
$tag_slugs = $to_slugs( $split( isset( $attrs['tags'] ) ? $attrs['tags'] : '' ), 'product_tag' );

$query = array(
	'status'       => 'publish',
	'limit'        => $limit,
	'return'       => 'objects',
	'visibility'   => 'visible',
	'orderby'      => 'date',
	'order'        => 'DESC',
);
switch ( $orderby ) {
	case 'popularity': $query['orderby'] = 'popularity'; break;
	case 'rating':     $query['orderby'] = 'rating'; break;
	case 'price':      $query['orderby'] = 'price'; $query['order'] = 'ASC'; break;
	case 'price-desc': $query['orderby'] = 'price'; $query['order'] = 'DESC'; break;
	case 'rand':       $query['orderby'] = 'rand'; break;
	case 'menu_order': $query['orderby'] = 'menu_order'; $query['order'] = 'ASC'; break;
}

if ( ! empty( $pids ) ) {
	
	$query['include'] = $pids;
	$query['orderby'] = 'include';
} else {
	if ( ! empty( $cat_slugs ) ) { $query['category'] = $cat_slugs; }
	if ( ! empty( $tag_slugs ) ) { $query['tag'] = $tag_slugs; }
}

$products = wc_get_products( $query );
if ( empty( $products ) ) { return; }

$wrapper = function_exists( 'get_block_wrapper_attributes' )
	? get_block_wrapper_attributes( array( 'class' => 'aieo-recommendations aieo-product-slider' ) )
	: 'class="aieo-recommendations aieo-product-slider"';

ob_start();
echo '<section ' . $wrapper . '>'; 
if ( '' !== $title ) {
	echo '<h2 class="aieo-recommendations-title">' . esc_html( $title ) . '</h2>';
}
echo '<div class="aieo-recommendations-grid">';
foreach ( $products as $product ) {
	if ( ! is_a( $product, 'WC_Product' ) ) { continue; }
	$id      = $product->get_id();
	$classes = 'aieo-card';
	if ( $product->is_on_sale() )   { $classes .= ' is-on-sale'; }
	if ( ! $product->is_in_stock() ) { $classes .= ' is-out-of-stock'; }

	echo '<a class="' . esc_attr( $classes ) . '" href="' . esc_url( get_permalink( $id ) ) . '"'
		. ' data-product-id="' . esc_attr( $id ) . '" data-product-type="' . esc_attr( $product->get_type() ) . '">';
	echo '<div class="aieo-card-img-wrap">'
		. $product->get_image( 'woocommerce_thumbnail', array( 'loading' => 'lazy' ) ) 
		. '</div>';
	echo '<div class="aieo-card-body">';
	echo '<h3 class="aieo-card-name">' . esc_html( $product->get_name() ) . '</h3>';
	echo '<div class="aieo-card-price">' . $product->get_price_html() . '</div>'; 
	if ( $show_rating && function_exists( 'wc_get_rating_html' ) ) {
		$rating = wc_get_rating_html( (float) $product->get_average_rating(), (int) $product->get_rating_count() );
		if ( $rating ) {
			echo '<div class="aieo-card-rating">' . $rating . '</div>'; 
		}
	}
	echo '</div></a>';
}
echo '</div>';
echo '</section>';
echo ob_get_clean(); 
