<?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('product-book bg-white d-flex flex-column h-100', $product); ?>>
    <div class="image position-relative d-flex align-items-center justify-content-center">
        <a href="<?= get_permalink( $product->get_id() ); ?>">
            <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="213" height="213"
                class="d-block w-auto">
        </a>
    </div>
    <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 text-center mx-auto"> <?php echo esc_html($first_category['title']); ?> </a>
    <?php endif; ?>

    <div class="btn-price d-flex align-items-center justify-content-between gap-2">
    <?php
$product_id = $product->get_id();
$product_type = $product->get_type();
$is_purchasable = $product->is_purchasable() && $product->is_in_stock();

// Base classes (always include these)
$add_to_cart_classes = [
    'btn-add-to-cart',
    'd-flex',
    'align-items-center',
    'justify-content-center',
    'gap-2',
    'flex-md-grow-1',
    'product_type_' . $product_type,
];

// Additional classes for purchasable products
if ($is_purchasable) {
    if ($product_type === 'simple' && $product->supports('ajax_add_to_cart')) {
        $add_to_cart_classes[] = 'add_to_cart_button';
        $add_to_cart_classes[] = 'ajax_add_to_cart';
    }
}

// Determine the appropriate URL
$button_url = $is_purchasable 
    ? ($product_type === 'variable' ? $product->get_permalink() : $product->add_to_cart_url())
    : $product->get_permalink();
?>

<a href="<?php echo esc_url($button_url); ?>"
   <?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(' ', $add_to_cart_classes)); ?>">
    <i class="isax isax-bag lh-1"></i>
    <span class="d-none d-md-block"><?php esc_html_e('Buy', 'kavehTheme'); ?></span>
</a>
        <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>
    </div>
</div>
<style>
    .product-book .woocommerce-Price-amount.amount {
        line-height: 2px !important;
    }
</style>