<?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 class="product-bed-two d-flex flex-column h-100 position-relative">
    <a href="#" class="image mx-auto d-block">
        <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="178" height="94"
            class="block mx-auto">
    </a>
    <h2 class="title-product text-center">
        <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="category d-block"> <?php echo esc_html($first_category['title']); ?> </a>
    <?php endif; ?>

    <div
        class="satisfaction-sales d-flex align-items-center justify-content-between gap-2 position-relative overflow-hidden">
        <div class="satisfaction-sales-item d-flex align-items-center">
            <div class="icon d-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0">
                <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="none" viewBox="0 0 14 14">
                    <path fill="#FFDF39" stroke="#000" stroke-linejoin="round" stroke-opacity=".91" stroke-width="1.3"
                        d="M.96 3.731c.017-.16.217-.2.31-.067l1.543 2.232a1 1 0 0 0 1.707-.101L6.79 1.5a.237.237 0 0 1 .42 0l2.27 4.294a1 1 0 0 0 1.707.101l1.543-2.232c.093-.133.293-.094.31.067.114 1.064.21 2.156.21 3.269s-.096 2.205-.21 3.268a3.132 3.132 0 0 1-2.753 2.76c-1.07.12-2.167.222-3.287.222s-2.218-.102-3.287-.222a3.132 3.132 0 0 1-2.753-2.76C.846 9.205.75 8.114.75 7s.096-2.205.21-3.269Z" />
                </svg>
            </div>
            <div>
                <div class="title"> <?php esc_html_e('Satisfaction:', 'kavehTheme'); ?> </div>
                <div class="val"> <?= $product->get_average_rating(); ?> </div>
            </div>
        </div>
        <div class="satisfaction-sales-item d-flex align-items-center">
            <div class="icon d-flex align-items-center justify-content-center flex-fill flex-grow-0 flex-shrink-0">
                <svg xmlns="http://www.w3.org/2000/svg" width="15" height="17" fill="none" viewBox="0 0 15 17">
                    <path fill="#FFDF39" stroke="#020202" stroke-width="1.3"
                        d="M1 12.176c0-1.83.648-3.676 1.295-5.097.5-1.1 1.634-1.748 2.842-1.748H9.93c1.207 0 2.341.649 2.842 1.748.646 1.42 1.295 3.267 1.295 5.097 0 1.71-1.06 2.875-2.723 3.148-.967.16-2.177.274-3.81.274-1.634 0-2.844-.114-3.812-.274C2.06 15.051 1 13.887 1 12.176Z" />
                    <path stroke="#020202" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.3"
                        d="M6.6 9.998h1.867" />
                    <path stroke="#020202" stroke-width="1.3" d="M4.733 5.331v-.933a2.8 2.8 0 1 1 5.6 0v.933" />
                </svg>
            </div>
            <div>
                <div class="title"> <?php esc_html_e('Sales:', 'kavehTheme'); ?> </div>
                <div class="val"> <?= $product->get_total_sales(); ?> </div>
            </div>
        </div>
    </div>
    <div class="price d-flex align-items-center justify-content-between">
    <?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>
    <div class="btns d-flex align-items-center justify-content-between">
        <a href="<?= get_permalink($product->get_id()); ?>"
            class="btn btn-primary-3 btn-view w-100 flex-grow-1 d-flex align-items-center justify-content-between gap-2">
            <?php esc_html_e('View', 'kavehTheme'); ?>
            <i class="isax isax-arrow-left4 lh-1"></i>
        </a>
        <?php
$product_id = $product->get_id();
$product_type = $product->get_type();
$is_purchasable = $product->is_purchasable() && $product->is_in_stock();

// Base styling classes (must keep these exact classes)
$base_classes = [
    'btn',
    'btn-orange',
    'btn-add-to-cart',
    'p-0',
    'd-flex',
    'align-items-center',
    'justify-content-center',
    'flex-fill',
    'flex-grow-0',
    'flex-shrink-0'
];

// WooCommerce functional classes
$wc_classes = [
    'product_type_' . $product_type,
    $is_purchasable ? 'add_to_cart_button' : '',
    ($product_type === 'simple' && $product->supports('ajax_add_to_cart') && $is_purchasable) ? 'ajax_add_to_cart' : ''
];

// Combine all classes and filter out empty ones
$all_classes = array_merge($base_classes, array_filter($wc_classes));
?>

<a href="<?php echo esc_url($is_purchasable ? ($product_type === 'variable' ? $product->get_permalink() : $product->add_to_cart_url()) : $product->get_permalink()); ?>"
   <?php if ($is_purchasable) : ?>
       data-product_id="<?php echo esc_attr($product_id); ?>"
       data-product_sku="<?php echo esc_attr($product->get_sku()); ?>"
       <?php if ($product_type === 'simple') : ?>data-quantity="1"<?php endif; ?>
   <?php endif; ?>
   aria-label="<?php echo esc_attr($product->add_to_cart_description()); ?>"
   class="<?php echo esc_attr(implode(' ', $all_classes)); ?>">
    <i class="isax isax-bag"></i>
</a>
    </div>
</div>