<?php
/**
 * Template View: Pricing Table
 *
 * Displays 2-4 pricing packages side by side in a responsive grid.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var array  $packages Array of package objects with name, price, features, etc.
 * @var string $title    Optional title for the table.
 */

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

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

// Validate packages
if ( empty( $packages ) || ! is_array( $packages ) ) {
    return;
}

$package_count = count( $packages );
$grid_class    = 'bb-pricing-grid bb-pricing-grid-' . min( $package_count, 4 );
?>

<div class="bb-pricing-table">
    <?php if ( ! empty( $title ) ) : ?>
        <h3 class="bb-pricing-title"><?php echo esc_html( $title ); ?></h3>
    <?php endif; ?>

    <div class="<?php echo esc_attr( $grid_class ); ?>">
        <?php foreach ( $packages as $index => $package ) : 
            $is_featured = ! empty( $package['featured'] );
            $card_class  = 'bb-pricing-card' . ( $is_featured ? ' bb-pricing-featured' : '' );
        ?>
            <div class="<?php echo esc_attr( $card_class ); ?>">
                <?php if ( $is_featured ) : ?>
                    <div class="bb-pricing-badge"><?php esc_html_e( 'מומלץ', 'boostbot' ); ?></div>
                <?php endif; ?>

                <div class="bb-pricing-header">
                    <h4 class="bb-pricing-name"><?php echo esc_html( $package['name'] ?? '' ); ?></h4>
                    
                    <?php if ( isset( $package['price'] ) ) : ?>
                        <div class="bb-pricing-price">
                            <span class="bb-price-currency">₪</span>
                            <span class="bb-price-amount"><?php echo esc_html( number_format( $package['price'], 0 ) ); ?></span>
                            <?php if ( ! empty( $package['price_suffix'] ) ) : ?>
                                <span class="bb-price-suffix"><?php echo esc_html( $package['price_suffix'] ); ?></span>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
                </div>

                <?php if ( ! empty( $package['description'] ) ) : ?>
                    <p class="bb-pricing-description"><?php echo esc_html( $package['description'] ); ?></p>
                <?php endif; ?>

                <?php if ( ! empty( $package['features'] ) && is_array( $package['features'] ) ) : ?>
                    <ul class="bb-pricing-features">
                        <?php foreach ( $package['features'] as $feature ) : 
                            // Support both string features and object features with included flag
                            $feature_text    = is_array( $feature ) ? ( $feature['text'] ?? '' ) : $feature;
                            $feature_included = is_array( $feature ) ? ( $feature['included'] ?? true ) : true;
                            $feature_class   = $feature_included ? 'bb-feature-included' : 'bb-feature-excluded';
                        ?>
                            <li class="<?php echo esc_attr( $feature_class ); ?>">
                                <span class="bb-feature-icon"><?php echo $feature_included ? '✓' : '✗'; ?></span>
                                <span class="bb-feature-text"><?php echo esc_html( $feature_text ); ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>

                <?php 
                // Only show CTA button if there's an actual URL provided
                if ( ! empty( $package['cta_url'] ) && $package['cta_url'] !== '#' ) : 
                ?>
                    <div class="bb-pricing-cta">
                        <a href="<?php echo esc_url( $package['cta_url'] ); ?>" 
                           class="bb-pricing-button<?php echo $is_featured ? ' bb-pricing-button-primary' : ''; ?>">
                            <?php echo esc_html( $package['cta_text'] ?? __( 'פרטים נוספים', 'boostbot' ) ); ?>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>
