<?php
/**
 * Template View: Bundle Discovery
 *
 * Shows available bundles containing a specific product.
 * Requires Pro + Bundles license.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var int    $product_id   The product ID user is viewing.
 * @var string $product_name The product name.
 * @var array  $bundles      Array of bundle objects.
 */

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

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

// Validate required data
if ( empty( $bundles ) || ! is_array( $bundles ) ) {
    return;
}
?>

<div class="bb-bundle-discovery">
    <div class="bb-discovery-header">
        <span class="bb-discovery-icon">💡</span>
        <div class="bb-discovery-text">
            <div class="bb-discovery-title">
                <?php 
                if ( ! empty( $product_name ) ) {
                    /* translators: %s: product name */
                    echo esc_html( sprintf( __( 'חסוך כסף עם חבילות הכוללות את %s!', 'boostbot' ), $product_name ) );
                } else {
                    esc_html_e( 'חבילות מומלצות עבורך!', 'boostbot' );
                }
                ?>
            </div>
            <div class="bb-discovery-subtitle">
                <?php /* translators: %d: number of bundles */
                echo esc_html( sprintf( _n( 'מצאנו %d חבילה', 'מצאנו %d חבילות', count( $bundles ), 'boostbot' ), count( $bundles ) ) ); ?>
            </div>
        </div>
    </div>
    
    <div class="bb-discovery-bundles">
        <?php foreach ( $bundles as $bundle ) : 
            $savings_percent = 0;
            if ( ! empty( $bundle['total_price'] ) && ! empty( $bundle['bundle_price'] ) && $bundle['total_price'] > $bundle['bundle_price'] ) {
                $savings_percent = round( ( ( $bundle['total_price'] - $bundle['bundle_price'] ) / $bundle['total_price'] ) * 100 );
            }
        ?>
            <div class="bb-discovery-bundle-item">
                <div class="bb-discovery-bundle-info">
                    <div class="bb-discovery-bundle-name"><?php echo esc_html( $bundle['name'] ?? '' ); ?></div>
                    <div class="bb-discovery-bundle-products">
                        <?php /* translators: %d: product count */
                        echo esc_html( sprintf( __( '%d מוצרים', 'boostbot' ), $bundle['product_count'] ?? 0 ) ); ?>
                    </div>
                </div>
                <div class="bb-discovery-bundle-pricing">
                    <?php if ( $savings_percent > 0 ) : ?>
                        <span class="bb-discovery-savings"><?php /* translators: %d: savings percentage */
                        echo esc_html( sprintf( __( '-%d%%', 'boostbot' ), $savings_percent ) ); ?></span>
                    <?php endif; ?>
                    <span class="bb-discovery-price">₪<?php echo esc_html( number_format( $bundle['bundle_price'] ?? 0, 0 ) ); ?></span>
                </div>
                <?php if ( ! empty( $bundle['url'] ) ) : ?>
                    <a href="<?php echo esc_url( $bundle['url'] ); ?>" class="bb-discovery-link" aria-label="<?php echo esc_attr( $bundle['name'] ?? '' ); ?>">→</a>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>

