<?php
/**
 * The template for displaying product content within loops
 *
 * This template overrides the default WooCommerce product card 
 * to use the beautiful premium design from the Paws & Care theme's frontpage.
 *
 * @package Paws_And_Care
 */

defined( 'ABSPATH' ) || exit;

global $product;

// Ensure visibility.
if ( empty( $product ) || ! $product->is_visible() ) {
    return;
}

$average_rating = $product->get_average_rating();
$review_count   = $product->get_review_count();

// Ensure classes are passed correctly
$classes = array( 'product-card-item' );
?>
<li <?php wc_product_class( $classes, $product ); ?>>
    <div class="paws-product-card">
        <!-- Product Image -->
        <div class="paws-product-image">
            <a href="<?php the_permalink(); ?>" class="paws-product-image-link">
                <?php if ( has_post_thumbnail() ) : ?>
                    <?php the_post_thumbnail( 'woocommerce_thumbnail', array( 'class' => 'product-thumb' ) ); ?>
                <?php else : ?>
                    <img src="<?php echo esc_url( wc_placeholder_img_src( 'woocommerce_thumbnail' ) ); ?>" alt="<?php the_title_attribute(); ?>" class="product-thumb">
                <?php endif; ?>
            </a>

            <!-- Badges -->
            <div class="paws-product-badges">
                <?php if ( $product->is_on_sale() ) : ?>
                    <span class="paws-badge paws-badge-sale">
                        <?php
                        if ( $product->is_type( 'simple' ) ) {
                            $regular  = (float) $product->get_regular_price();
                            $sale     = (float) $product->get_sale_price();
                            if ( $regular > 0 ) {
                                $percent = round( 100 - ( $sale / $regular * 100 ) );
                                /* translators: %d: sale percentage */
                                printf( esc_html__( '-%d%%', 'paws-and-care' ), $percent );
                            } else {
                                esc_html_e( 'Sale', 'paws-and-care' );
                            }
                        } else {
                            esc_html_e( 'Sale', 'paws-and-care' );
                        }
                        ?>
                    </span>
                <?php endif; ?>
                <?php if ( $product->is_featured() ) : ?>
                    <span class="paws-badge paws-badge-featured">
                        <i class="fa-solid fa-star"></i>
                    </span>
                <?php endif; ?>
            </div>

            <!-- Quick Actions Overlay -->
            <div class="paws-product-actions">
                <a href="<?php the_permalink(); ?>" class="paws-action-btn" title="<?php esc_attr_e( 'View Details', 'paws-and-care' ); ?>">
                    <i class="fa-solid fa-eye"></i>
                </a>
                <?php if ( $product->is_purchasable() && $product->is_in_stock() && $product->is_type( 'simple' ) ) : ?>
                    <a href="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="paws-action-btn ajax_add_to_cart add_to_cart_button" data-product_id="<?php echo esc_attr( $product->get_id() ); ?>" data-quantity="1" title="<?php esc_attr_e( 'Add to Cart', 'paws-and-care' ); ?>">
                        <i class="fa-solid fa-cart-plus"></i>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <!-- Product Info -->
        <div class="paws-product-info">
            <?php
            $cats = get_the_terms( $product->get_id(), 'product_cat' );
            if ( $cats && ! is_wp_error( $cats ) ) :
                $first_cat = $cats[0];
            ?>
                <a href="<?php echo esc_url( get_term_link( $first_cat ) ); ?>" class="paws-product-cat">
                    <?php echo esc_html( $first_cat->name ); ?>
                </a>
            <?php endif; ?>

            <h3 class="paws-product-title">
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h3>

            <?php if ( $average_rating > 0 ) : ?>
                <div class="paws-product-rating">
                    <div class="paws-stars">
                        <?php
                        for ( $i = 1; $i <= 5; $i++ ) {
                            if ( $i <= floor( $average_rating ) ) {
                                echo '<i class="fa-solid fa-star"></i>';
                            } elseif ( $i - 0.5 <= $average_rating ) {
                                echo '<i class="fa-solid fa-star-half-stroke"></i>';
                            } else {
                                echo '<i class="fa-regular fa-star"></i>';
                            }
                        }
                        ?>
                    </div>
                    <span class="paws-review-count">
                        <?php
                        /* translators: %d: review count */
                        printf( esc_html( _n( '(%d)', '(%d)', $review_count, 'paws-and-care' ) ), $review_count );
                        ?>
                    </span>
                </div>
            <?php endif; ?>

            <div class="paws-product-footer">
                <div class="paws-product-price">
                    <?php echo wp_kses_post( $product->get_price_html() ); ?>
                </div>
                <?php if ( $product->is_purchasable() && $product->is_in_stock() ) : ?>
                    <a href="<?php echo esc_url( $product->is_type( 'simple' ) ? $product->add_to_cart_url() : get_permalink() ); ?>"
                       class="paws-add-to-cart-btn <?php echo $product->is_type( 'simple' ) ? 'ajax_add_to_cart add_to_cart_button' : ''; ?>"
                       <?php if ( $product->is_type( 'simple' ) ) : ?>
                           data-product_id="<?php echo esc_attr( $product->get_id() ); ?>"
                           data-quantity="1"
                       <?php endif; ?>
                       title="<?php echo esc_attr( $product->add_to_cart_text() ); ?>">
                        <i class="fa-solid fa-bag-shopping"></i>
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</li>
