<?php
/**
 * Template View: Product Card
 *
 * Displays a single product with image, price and buy button.
 *
 * @package    BoostBot
 * @subpackage Templates/Views
 * @since      1.6.0
 *
 * @var int    $id            Product ID.
 * @var string $name          Product name.
 * @var string $image         Product image URL.
 * @var float  $price         Product price.
 * @var float  $regular_price Original price (if on sale).
 * @var string $description   Short description.
 * @var string $url           Product page URL.
 * @var bool   $in_stock      Whether product is in stock.
 */

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

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

$is_on_sale = ! empty( $regular_price ) && $regular_price > $price;
$is_out_of_stock = isset( $in_stock ) && ! $in_stock;
?>

<div class="bb-product-card<?php echo $is_out_of_stock ? ' bb-product-out-of-stock' : ''; ?>">
    <?php if ( $is_on_sale ) : ?>
        <div class="bb-product-sale-badge"><?php esc_html_e( 'מבצע', 'boostbot' ); ?></div>
    <?php endif; ?>

    <?php if ( ! empty( $image ) ) : ?>
        <div class="bb-product-image">
            <img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $name ); ?>" loading="lazy" />
        </div>
    <?php else : ?>
        <div class="bb-product-image bb-product-no-image">
            <span>📦</span>
        </div>
    <?php endif; ?>

    <div class="bb-product-content">
        <h4 class="bb-product-name"><?php echo esc_html( $name ); ?></h4>

        <?php if ( ! empty( $description ) ) : ?>
            <p class="bb-product-description"><?php echo esc_html( $description ); ?></p>
        <?php endif; ?>

        <div class="bb-product-price-row">
            <?php if ( $is_on_sale ) : ?>
                <span class="bb-product-regular-price">₪<?php echo esc_html( number_format( $regular_price, 0 ) ); ?></span>
            <?php endif; ?>
            <span class="bb-product-price<?php echo $is_on_sale ? ' bb-product-sale-price' : ''; ?>">
                ₪<?php echo esc_html( number_format( $price, 0 ) ); ?>
            </span>
        </div>

        <?php if ( $is_out_of_stock ) : ?>
            <div class="bb-product-stock-status bb-product-stock-out">
                <?php esc_html_e( 'אזל מהמלאי', 'boostbot' ); ?>
            </div>
        <?php endif; ?>

        <div class="bb-product-actions">
            <?php if ( ! empty( $url ) ) : ?>
                <a href="<?php echo esc_url( $url ); ?>" class="bb-product-button bb-product-button-view">
                    <?php esc_html_e( 'צפה במוצר', 'boostbot' ); ?>
                </a>
            <?php endif; ?>
            
            <?php if ( ! $is_out_of_stock && ! empty( $id ) ) : ?>
                <button type="button" 
                        class="bb-product-button bb-product-button-cart" 
                        data-product-id="<?php echo esc_attr( $id ); ?>">
                    <?php esc_html_e( 'הוסף לסל', 'boostbot' ); ?>
                </button>
            <?php endif; ?>
        </div>
    </div>
</div>

