<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it
 * do happen. When this occurs, the version of the template file will be bumped
 * and the readme will list any important changes.
 *
 * @see     https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 9.4.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

if ( ! is_a( $product, WC_Product::class ) || ! $product->is_visible() ) {
	return;
}

$post        = get_post();
$product_id  = $product->get_id();

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

$badge_text  = '';
$badge_class = '';

if ( ! $is_in_stock ) {
	$badge_text  = __( 'Sold Out', 'pawwell' );
	$badge_class = 'sold-out';
} elseif ( $is_on_sale ) {
	$badge_text  = pawwell_get_product_sale_percentage( $product );
	$badge_class = 'sale-badge';
} elseif ( function_exists( 'pawwell_is_product_new' ) && pawwell_is_product_new( $product_id ) ) {
	$badge_text  = __( 'New', 'pawwell' );
	$badge_class = 'new';
}

$brand_name         = pawwell_get_product_brand( $product_id );
$product_categories = wc_get_product_category_list( $product_id, ', ' );

// Determine add-to-cart state.
$is_in_cart = false;
if ( $is_in_stock && function_exists( 'WC' ) && WC()->cart ) {
	foreach ( WC()->cart->get_cart() as $cart_item ) {
		if ( $cart_item['product_id'] === $product_id ) {
			$is_in_cart = true;
			break;
		}
	}
}

// Round rating for star rendering (0-5).
$rating_round = (int) round( $average_rating );
$rating_round = max( 0, min( 5, $rating_round ) );
?>

<div <?php wc_product_class( 'product-card', $product ); ?> data-product-id="<?php echo esc_attr( $product_id ); ?>">

	<div class="product-card-img">
		<a href="<?php the_permalink(); ?>" class="product-card-img-link">
			<?php
			if ( function_exists( 'pawwell_template_loop_product_thumbnail' ) ) {
				pawwell_template_loop_product_thumbnail();
			} else {
				woocommerce_template_loop_product_thumbnail();
			}
			?>
			<?php do_action( 'pawwell_product_second_image', $product_id ); ?>
		</a>

		<?php if ( ! empty( $badge_text ) ) : ?>
			<span class="product-badge <?php echo esc_attr( $badge_class ); ?>"><?php echo esc_html( $badge_text ); ?></span>
		<?php endif; ?>

		<div class="product-card-actions">
			<?php do_action( 'pawwell_before_compare_button', $product_id ); ?>
			<?php do_action( 'pawwell_after_wishlist_button', $product_id ); ?>
			<?php do_action( 'pawwell_after_compare_button', $product_id ); ?>
		</div>

		<?php if ( $is_in_stock ) : ?>
			<?php if ( $is_in_cart ) : ?>
				<button class="p-quick-add p-quick-added" disabled aria-label="<?php esc_attr_e( 'Already in cart', 'pawwell' ); ?>">
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 6 9 17l-5-5"/></svg>
					<?php esc_html_e( 'Added to cart', 'pawwell' ); ?>
				</button>
			<?php else : ?>
				<button class="p-quick-add" data-quick-add-to-cart="<?php echo esc_attr( $product_id ); ?>" aria-label="<?php esc_attr_e( 'Add to cart', 'pawwell' ); ?>">
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M13 2 4 14h7l-1 8 9-12h-7l1-8z"/></svg>
					<?php esc_html_e( 'Quick Add to Cart', 'pawwell' ); ?>
				</button>
			<?php endif; ?>
		<?php endif; ?>
	</div>

	<div class="product-card-info">
		<?php if ( ! empty( $product_categories ) ) : ?>
			<div class="product-card-category"><?php echo wp_kses_post( $product_categories ); ?></div>
		<?php elseif ( ! empty( $brand_name ) ) : ?>
			<div class="product-card-category"><?php echo esc_html( $brand_name ); ?></div>
		<?php endif; ?>

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

		<?php if ( $rating_round > 0 ) : ?>
			<div class="product-card-rating">
				<span class="p-stars" aria-label="<?php echo esc_attr( sprintf( __( 'Rated %s out of 5', 'pawwell' ), esc_html( $average_rating ) ) ); ?>">
					<?php for ( $s = 1; $s <= 5; $s++ ) : ?>
						<svg viewBox="0 0 24 24" fill="<?php echo $s <= $rating_round ? 'currentColor' : 'none'; ?>" stroke="currentColor" stroke-width="1.5"><path d="M12 2.5l2.9 5.9 6.5.9-4.7 4.6 1.1 6.5L12 17.8 6.2 20.9l1.1-6.5L2.6 9.8l6.5-.9L12 2.5z"/></svg>
					<?php endfor; ?>
				</span>
				<?php if ( $review_count > 0 ) : ?>
					<span class="p-rating-count">(<?php echo esc_html( $review_count ); ?>)</span>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<div class="product-card-price">
			<?php woocommerce_template_loop_price(); ?>
		</div>

		<?php
		do_action( 'woocommerce_after_shop_loop_item_title' );

		pawwell_archive_swatches();
		?>

		<?php pawwell_countdown_loop(); ?>
	</div>

</div>
