<?php
/**
 * Products Grid Template
 *
 * Displays multiple products in a grid layout.
 *
 * @package BoostBot
 * @since 1.1.0
 *
 * Available variables:
 * @var string $title    Optional grid title
 * @var array  $products Array of products (required)
 * @var int    $columns  Number of columns (2-4, default: 2)
 */

// phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template-scope variables set by the includer, not true globals.

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

$columns = max( 2, min( 4, intval( $columns ?? 2 ) ) );
$column_class = 'bb-grid-cols-' . $columns;
?>
<div class="bb-template bb-products-grid <?php echo esc_attr( $column_class ); ?>" dir="rtl">
    <?php if ( ! empty( $title ) ) : ?>
        <h4 class="bb-grid-title"><?php echo esc_html( $title ); ?></h4>
    <?php endif; ?>
    
    <div class="bb-grid-items">
        <?php if ( ! empty( $products ) && is_array( $products ) ) : ?>
            <?php foreach ( $products as $product ) : ?>
                <?php if ( ! is_array( $product ) ) { continue; } // Skip malformed AI-supplied items (avoids a fatal TypeError in the sub-template). ?>
                <?php
                // Render each product using the product-mini template
                $renderer = new BoostBot_Template_Renderer();
                // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is escaped in renderer
                echo $renderer->render( 'product-mini', $product );
                ?>
            <?php endforeach; ?>
        <?php else : ?>
            <p class="bb-grid-empty"><?php esc_html_e( 'לא נמצאו מוצרים', 'boostbot' ); ?></p>
        <?php endif; ?>
    </div>
</div>
