<?php
defined('ABSPATH') || exit;

global $product;

// Ensure $product is valid
if (!$product || !$product->is_visible()) {
    return;
}

// Get category information
$category_ids = $product->get_category_ids();
$first_category = null;

if (!empty($category_ids)) {
    $first_category_id = $category_ids[0];
    $category = get_term($first_category_id, 'product_cat');

    if (!is_wp_error($category)) {
        $first_category = [
            'title' => $category->name,
            'link' => get_term_link($first_category_id, 'product_cat')
        ];
    }
}
?>
<div <?php wc_product_class('box-product-one d-flex flex-column h-100 position-relative overflow-hidden', $product); ?>>
    <a href="<?= get_permalink( $product->get_id() ); ?>" class="image block mx-auto">
        <img src="<?php if (get_the_post_thumbnail_url()): ?><?php the_post_thumbnail_url('full'); ?><?php else: ?><?php echo wc_placeholder_img_src(); ?><?php endif; ?>"
            alt="<?php
$product_name = $product->get_name();
$show_full_titles = kaveh_get_option('kaveh_product_title_display', false);
if ($show_full_titles) {
    echo esc_html($product_name);
} else {
    if ( wp_is_mobile() ) {
        echo wp_trim_words( $product_name, 5, '...' );
    } else {
        echo wp_trim_words( $product_name, 8, '...' );
    }
}
?>" width="79" height="79" class="block mx-auto">
    </a>
    <h2 class="title-product text-center overflow-hidden">
        <a href="<?= get_permalink( $product->get_id() ); ?>"><?php
$product_name = $product->get_name();
if ( wp_is_mobile() ) {
    echo wp_trim_words( $product_name, 5, '...' );
} else {
    echo wp_trim_words( $product_name, 8, '...' );
}
?></a>
    </h2>
    <?php if ($first_category): ?>
        <a href="<?php echo esc_url($first_category['link']); ?>" class="brand-product text-center mt-auto">
            <?php echo esc_html($first_category['title']); ?> </a>
    <?php endif; ?>

    <div class="price d-flex align-items-center justify-content-end">
    <div class="d-flex flex-column">
        <?php
        // Use WooCommerce's built-in price HTML output
        $price_html = $product->get_price_html();
        // If we have price HTML (not empty)
        if ($price_html) {
            echo $price_html;
        }
        ?>
    </div>
    <style>
        .box-product-one .price span.woocommerce-Price-amount.amount{
            font-size: 12px !important;
            line-height: 10px!important;
            letter-spacing: 0 !important;
        }
        .box-product-one .price span.woocommerce-Price-currencySymbol{
            font-size: 10px !important;
            line-height: 10px!important; 
            letter-spacing: 0 !important;
            margin-right: 5px !important;

        }
    </style>

</div>
<?php
$product_id = $product->get_id();
$product_type = $product->get_type();

$class = [
    'btn-add-to-cart',
    'position-absolute',
    'start-0',
    'bottom-0',
    'd-flex',
    'align-items-center',
    'justify-content-center',
    'product_type_' . $product_type,
];

if ($product->is_purchasable() && $product->is_in_stock()) {
    if ($product_type === 'simple') {
        $class[] = 'add_to_cart_button';
        if ($product->supports('ajax_add_to_cart')) {
            $class[] = 'ajax_add_to_cart';
        }
    }
}

$link = $product_type === 'variable' ? $product->get_permalink() : $product->add_to_cart_url();
?>

<a href="<?php echo esc_url($link); ?>" 
   data-product_id="<?php echo esc_attr($product_id); ?>"
   data-product_sku="<?php echo esc_attr($product->get_sku()); ?>"
   aria-label="<?php echo esc_attr($product->add_to_cart_description()); ?>"
   class="<?php echo esc_attr(implode(' ', $class)); ?>"
   <?php echo ($product_type === 'simple' && $product->supports('ajax_add_to_cart')) ? 'data-quantity="1"' : ''; ?>>
    <i class="isax isax-bag lh-1"></i>
</a>
</div>