<?php
/**
 * Template View: Bundle Card
 *
 * Displays a complete bundle with products.
 * Requires Pro + Bundles license.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var int    $id             Bundle ID.
 * @var string $name           Bundle name.
 * @var string $description    Bundle description.
 * @var string $image          Bundle image URL.
 * @var array  $products       Array of product objects.
 * @var float  $total_price    Total price (sum of individual).
 * @var float  $bundle_price   Discounted bundle price.
 * @var string $url            URL to bundle page.
 */

// 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( $name ) || empty( $products ) || ! isset( $bundle_price ) ) {
    return;
}

$savings = 0;
$savings_percent = 0;
if ( ! empty( $total_price ) && $total_price > $bundle_price ) {
    $savings = $total_price - $bundle_price;
    $savings_percent = round( ( $savings / $total_price ) * 100 );
}
?>

<div class="bb-bundle-card">
    <?php if ( $savings_percent > 0 ) : ?>
        <div class="bb-bundle-savings-ribbon">
            <?php /* translators: %d: savings percentage */
            echo esc_html( sprintf( __( 'חסוך %d%%', 'boostbot' ), $savings_percent ) ); ?>
        </div>
    <?php endif; ?>
    
    <div class="bb-bundle-header">
        <?php if ( ! empty( $image ) ) : ?>
            <div class="bb-bundle-image">
                <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $name ); ?>">
            </div>
        <?php endif; ?>
        <div class="bb-bundle-info">
            <h3 class="bb-bundle-name"><?php echo esc_html( $name ); ?></h3>
            <?php if ( ! empty( $description ) ) : ?>
                <p class="bb-bundle-description"><?php echo esc_html( $description ); ?></p>
            <?php endif; ?>
        </div>
    </div>
    
    <div class="bb-bundle-products">
        <div class="bb-bundle-products-header">
            <span class="bb-products-icon">📦</span>
            <span><?php /* translators: %d: number of products */
            echo esc_html( sprintf( __( 'כולל %d מוצרים', 'boostbot' ), count( $products ) ) ); ?></span>
        </div>
        <div class="bb-bundle-products-list">
            <?php foreach ( $products as $product ) : ?>
                <div class="bb-bundle-product-item">
                    <?php if ( ! empty( $product['image'] ) ) : ?>
                        <img src="<?php echo esc_url( $product['image'] ); ?>" alt="<?php echo esc_attr( $product['name'] ?? '' ); ?>">
                    <?php else : ?>
                        <span class="bb-product-placeholder">📦</span>
                    <?php endif; ?>
                    <div class="bb-bundle-product-details">
                        <span class="bb-product-name"><?php echo esc_html( $product['name'] ?? '' ); ?></span>
                        <?php if ( ! empty( $product['price'] ) ) : ?>
                            <span class="bb-product-price">₪<?php echo esc_html( number_format( $product['price'], 0 ) ); ?></span>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
    
    <div class="bb-bundle-footer">
        <div class="bb-bundle-pricing">
            <?php if ( ! empty( $total_price ) && $total_price > $bundle_price ) : ?>
                <span class="bb-bundle-original">₪<?php echo esc_html( number_format( $total_price, 0 ) ); ?></span>
            <?php endif; ?>
            <span class="bb-bundle-price">₪<?php echo esc_html( number_format( $bundle_price, 0 ) ); ?></span>
        </div>
        <?php if ( ! empty( $url ) ) : ?>
            <a href="<?php echo esc_url( $url ); ?>" class="bb-bundle-cta">
                <?php esc_html_e( 'צפה בחבילה', 'boostbot' ); ?>
            </a>
        <?php endif; ?>
    </div>
</div>

