<?php
/**
 * Template View: Bundle Summary
 *
 * Shows bundle totals and savings.
 * Requires Pro + Bundles license.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var float  $total_price  Original total price.
 * @var float  $bundle_price Bundle discounted price.
 * @var float  $savings      Amount saved.
 * @var int    $item_count   Number of items.
 */

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

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

// Calculate savings if not provided
if ( ! isset( $savings ) && isset( $total_price ) && isset( $bundle_price ) ) {
    $savings = $total_price - $bundle_price;
}

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

// Direction and currency
$is_rtl = is_rtl();
$dir = $is_rtl ? 'rtl' : 'ltr';
$currency = function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '$';
?>

<div class="bb-bundle-summary" dir="<?php echo esc_attr( $dir ); ?>">
    <div class="bb-summary-row bb-summary-items">
        <span class="bb-summary-label">
            <?php /* translators: %d: product count */
            echo esc_html( sprintf( _n( '%d product', '%d products', $item_count ?? 0, 'boostbot' ), $item_count ?? 0 ) ); ?>
        </span>
        <?php if ( ! empty( $total_price ) ) : ?>
            <span class="bb-summary-value bb-original-total"><?php echo esc_html( $currency . number_format( $total_price, 2 ) ); ?></span>
        <?php endif; ?>
    </div>
    
    <?php if ( ! empty( $savings ) && $savings > 0 ) : ?>
        <div class="bb-summary-row bb-summary-savings">
            <span class="bb-summary-label">
                <span class="bb-savings-icon">🎉</span>
                <?php esc_html_e( 'Bundle savings', 'boostbot' ); ?>
            </span>
            <span class="bb-summary-value bb-savings-value">-<?php echo esc_html( $currency . number_format( $savings, 2 ) ); ?></span>
        </div>
    <?php endif; ?>
    
    <div class="bb-summary-divider"></div>
    
    <div class="bb-summary-row bb-summary-total">
        <span class="bb-summary-label"><?php esc_html_e( 'Total', 'boostbot' ); ?></span>
        <div class="bb-summary-final">
            <span class="bb-summary-value bb-final-price"><?php echo esc_html( $currency . number_format( $bundle_price ?? 0, 2 ) ); ?></span>
            <?php if ( $savings_percent > 0 ) : ?>
                <span class="bb-savings-badge"><?php /* translators: %d: savings percentage */
                echo esc_html( sprintf( __( 'Save %d%%', 'boostbot' ), $savings_percent ) ); ?></span>
            <?php endif; ?>
        </div>
    </div>
</div>
