<?php
/**
 * The Template for displaying all single products
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product.php.
 *
 * @see         https://woocommerce.com/document/template-structure/
 * @package     WooCommerce\Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

get_header();

$product = wc_get_product( get_the_ID() );

if ( ! $product ) {
	return;
}

$product_id         = $product->get_id();
$product_title      = $product->get_title();
$product_short_desc = $product->get_short_description();
$sku                = $product->get_sku();

$categories    = get_the_terms( $product_id, 'product_cat' );
$main_category = ! empty( $categories ) && ! is_wp_error( $categories ) ? $categories[0] : null;

$attachment_ids  = $product->get_gallery_image_ids();
$main_image_id   = $product->get_image_id();
$all_images      = array();

if ( $main_image_id ) {
	$all_images[] = $main_image_id;
}
if ( ! empty( $attachment_ids ) ) {
	$all_images = array_merge( $all_images, $attachment_ids );
}

$attributes     = $product->get_attributes();
$size_attribute = null;

foreach ( $attributes as $attribute ) {
	$attr_name = $attribute->get_name();
	if ( false !== strpos( $attr_name, 'size' ) || false !== strpos( $attr_name, 'pa_size' ) ) {
		$size_attribute = $attribute;
	}
}

$related_products = wc_get_related_products( $product_id, 4 );

$comments = get_comments( array(
	'post_id' => $product_id,
	'status'  => 'approve',
	'type'    => 'review',
) );

$rating_distribution = array( 5 => 0, 4 => 0, 3 => 0, 2 => 0, 1 => 0 );
$review_total       = 0;
$review_rating_sum  = 0;
foreach ( $comments as $comment ) {
	$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
	if ( $rating >= 1 && $rating <= 5 ) {
		$rating_distribution[ $rating ]++;
		$review_total++;
		$review_rating_sum += $rating;
	}
}

$rating_count = $review_total;
$average      = $review_total > 0 ? round( $review_rating_sum / $review_total, 2 ) : 0;

$price_html     = $product->get_price_html();
$current_price  = '';
$original_price = '';

if ( $product->is_type( 'variable' ) ) {
	$current_price = $price_html;
} elseif ( $product->is_on_sale() ) {
	if ( preg_match( '/<ins[^>]*>(.*?)<\/ins>/s', $price_html, $matches ) ) {
		$current_price = $matches[1];
	}
	if ( preg_match( '/<del[^>]*>(.*?)<\/del>/s', $price_html, $matches ) ) {
		$original_price = $matches[1];
	}
} else {
	$current_price = $price_html;
}

$badge_text = '';
$tag_text   = '';

if ( $product->is_on_sale() ) {
	$sale_pct   = ! $product->is_type( 'variable' ) ? pawwell_get_sale_percentage( $product ) : '';
	$badge_text = $sale_pct ? $sale_pct : esc_html__( 'Sale', 'pawwell' );
} elseif ( function_exists( 'pawwell_is_product_new' ) && pawwell_is_product_new( $product_id ) ) {
	$badge_text = esc_html__( 'New', 'pawwell' );
	$tag_text   = esc_html__( 'New Arrival', 'pawwell' );
} elseif ( $product->is_featured() ) {
	$tag_text = esc_html__( 'Bestseller', 'pawwell' );
}

$in_stock  = $product->is_in_stock();
$stock_qty = $product->managing_stock() ? $product->get_stock_quantity() : null;
$is_low    = $in_stock && null !== $stock_qty && $stock_qty <= 5 && $stock_qty > 0;

$save_text = '';
if ( $product->is_on_sale() && ! $product->is_type( 'variable' ) ) {
	$save_pct = function_exists( 'pawwell_get_sale_percentage' ) ? pawwell_get_sale_percentage( $product ) : '';
	if ( $save_pct ) {
		$save_text = $save_pct;
	}
}

$breadcrumb_items   = array();
$breadcrumb_items[] = array( 'label' => __( 'Home', 'pawwell' ), 'url' => home_url( '/' ) );

if ( $main_category ) {
	$breadcrumb_items[] = array( 'label' => $main_category->name, 'url' => get_term_link( $main_category ) );
}

$breadcrumb_items[] = array( 'label' => $product_title, 'url' => '' );
if ( pawwell_breadcrumb_is_enabled() ) :
?>

<div class="pawwell-breadcrumb">
	<div class="pawwell-container">
		<nav class="pawwell-breadcrumb-nav">
			<?php foreach ( $breadcrumb_items as $i => $item ) : ?>
				<?php if ( ! empty( $item['url'] ) ) : ?>
					<a href="<?php echo esc_url( $item['url'] ); ?>" class="pawwell-breadcrumb-link"><?php echo esc_html( $item['label'] ); ?></a>
					<span class="pawwell-breadcrumb-sep">/</span>
				<?php else : ?>
					<span class="pawwell-breadcrumb-current"><?php echo esc_html( $item['label'] ); ?></span>
				<?php endif; ?>
			<?php endforeach; ?>
		</nav>
	</div>
</div>

<?php
endif;
?>
<main class="pawwell-pdp-main">
	<div class="pawwell-container pawwell-pdp-container">
		<div class="pawwell-pdp-grid">

			<div class="pawwell-gallery">
				<div class="pawwell-gallery-main">
					<img id="mainImg" src="<?php echo ! empty( $all_images ) ? esc_url( wp_get_attachment_image_url( $all_images[0], 'large' ) ) : wc_placeholder_img_src(); ?>" alt="<?php echo esc_attr( $product_title ); ?>">
					<?php if ( $badge_text ) : ?>
						<div class="pawwell-pdp-badge <?php echo $product->is_on_sale() ? 'pawwell-badge-sale' : ( $product->is_featured() ? 'pawwell-badge-bestseller' : 'pawwell-badge-new' ); ?>"><?php echo esc_html( $badge_text ); ?></div>
					<?php endif; ?>
				</div>
				<?php if ( count( $all_images ) > 1 ) : ?>
				<div class="pawwell-gallery-thumbs">
					<?php foreach ( $all_images as $index => $image_id ) : ?>
						<button data-change-image="<?php echo intval( $index ); ?>" class="pawwell-thumb-btn <?php echo $index === 0 ? 'active' : ''; ?>" data-index="<?php echo intval( $index ); ?>" data-full="<?php echo esc_url( wp_get_attachment_image_url( $image_id, 'large' ) ); ?>">
							<img src="<?php echo esc_url( wp_get_attachment_image_url( $image_id, 'thumbnail' ) ); ?>" alt="<?php echo esc_attr( $product_title ) . ' ' . ( $index + 1 ); ?>">
						</button>
					<?php endforeach; ?>
				</div>
				<?php endif; ?>
			</div>

			<div class="pawwell-pdp-info">
				<?php pawwell_countdown_single(); ?>

				<?php if ( $main_category ) : ?>
					<div class="pawwell-pdp-category"><?php echo esc_html( $main_category->name ); ?></div>
				<?php endif; ?>

				<?php if ( ! empty( $tag_text ) ) : ?>
					<span class="pawwell-pdp-tag"><?php echo esc_html( $tag_text ); ?></span>
				<?php endif; ?>

				<h1 class="pawwell-pdp-title"><?php echo esc_html( $product_title ); ?></h1>

				<div class="pawwell-pdp-meta-row">
					<?php if ( $rating_count > 0 ) : ?>
					<div class="pawwell-pdp-rating">
						<div class="pawwell-rating-stars">
							<?php for ( $i = 1; $i <= 5; $i++ ) : ?>
								<span class="pawwell-star-icon <?php echo $i <= round( $average ) ? 'pawwell-star-filled' : 'pawwell-star-empty'; ?>"><?php echo pawwell_svg_icon( 'star', 14 ); ?></span>
							<?php endfor; ?>
						</div>
						<strong class="pawwell-pdp-rating-num"><?php echo esc_html( number_format( $average, 1 ) ); ?></strong>
						<a href="#reviews" class="pawwell-rating-link"><?php echo esc_html( $rating_count ); ?> <?php echo esc_html( _n( 'review', 'reviews', $rating_count, 'pawwell' ) ); ?></a>
					</div>
					<?php endif; ?>

					<?php if ( $sku ) : ?>
					<div class="pawwell-pdp-sku"><?php esc_html_e( 'SKU:', 'pawwell' ); ?> <strong><?php echo esc_html( $sku ); ?></strong></div>
					<?php endif; ?>
				</div>

				<?php if ( $in_stock ) : ?>
					<div class="pawwell-pdp-stock<?php echo $is_low ? ' low' : ''; ?>"><span class="pawwell-pdp-stock-dot"></span> <?php echo $is_low ? esc_html__( 'Low stock — order soon', 'pawwell' ) : esc_html__( 'In stock — ready to ship', 'pawwell' ); ?></div>
				<?php else : ?>
					<div class="pawwell-pdp-stock out"><span class="pawwell-pdp-stock-dot"></span> <?php esc_html_e( 'Out of stock', 'pawwell' ); ?></div>
				<?php endif; ?>

				<?php if ( $product_short_desc ) : ?>
					<p class="pawwell-pdp-desc"><?php echo wp_kses_post( $product_short_desc ); ?></p>
				<?php endif; ?>

				<div class="pawwell-pdp-price-block" id="productPriceBlock">
					<?php if ( ! empty( $current_price ) ) : ?>
						<span class="pawwell-price-current"><?php echo wp_kses_post( $current_price ); ?></span>
					<?php endif; ?>
					<?php if ( ! empty( $original_price ) ) : ?>
						<span class="pawwell-price-original"><?php echo wp_kses_post( $original_price ); ?></span>
					<?php endif; ?>
					<?php if ( ! empty( $save_text ) ) : ?>
						<span class="pawwell-price-save"><?php echo esc_html( $save_text ); ?></span>
					<?php endif; ?>
					<span class="pawwell-price-tax"><?php esc_html_e( 'Tax included. Shipping calculated at checkout.', 'pawwell' ); ?></span>
				</div>

				<?php if ( $product->is_type( 'variable' ) ) : ?>
					<div class="variations_form" data-product_id="<?php echo esc_attr( $product_id ); ?>">
						<script type="application/json" id="pawwell-variations-data"><?php echo wp_json_encode( $product->get_available_variations() ); ?></script>
						<?php
						$variations         = $product->get_available_variations();
						$var_attributes     = $product->get_variation_attributes();
						$default_attributes = $product->get_default_attributes();

						pawwell_single_swatches();

					// Reorder attributes: selectable values (bags/weight/flavour/size)
					// render before color, matching the product design.
					$ordered_attributes = array();
					$color_attributes   = array();
					foreach ( $var_attributes as $attr_name => $attr_options ) {
						$label_lower = strtolower( wc_attribute_label( $attr_name ) );
						if ( false !== strpos( $label_lower, 'color' ) || false !== strpos( $label_lower, 'colour' ) ) {
							$color_attributes[ $attr_name ] = $attr_options;
						} else {
							$ordered_attributes[ $attr_name ] = $attr_options;
						}
					}
					$var_attributes = $ordered_attributes + $color_attributes;

					foreach ( $var_attributes as $attribute_name => $options ) :
						$attribute_label = wc_attribute_label( $attribute_name );
						$selected_value  = isset( $default_attributes[ $attribute_name ] ) ? $default_attributes[ $attribute_name ] : '';
						$label_lower     = strtolower( $attribute_label );
						$is_color_attr   = ( false !== strpos( $label_lower, 'color' ) || false !== strpos( $label_lower, 'colour' ) );
						?>
						<div class="pawwell-option-group" data-attribute-name="<?php echo esc_attr( $attribute_name ); ?>">
							<div class="pawwell-option-label">
								<span class="pawwell-option-name"><?php echo esc_html( $attribute_label ); ?></span>
								<?php if ( ! $is_color_attr ) : ?>
									<span class="pawwell-option-selected"><?php echo $selected_value ? esc_html( $selected_value ) : ''; ?></span>
								<?php endif; ?>
							</div>

							<?php if ( $is_color_attr ) : ?>
								<div class="pawwell-size-options">
									<?php
									foreach ( $options as $option ) :
										$option_slug = $option;
										if ( taxonomy_exists( $attribute_name ) ) {
											$term        = get_term_by( 'name', $option, $attribute_name );
											$option_slug = $term ? $term->slug : sanitize_title( $option );
										}

										$is_selected = ( $selected_value === $option || $selected_value === $option_slug );
										$is_oos      = false;

										foreach ( $variations as $variation ) {
											$attr_key = 'attribute_' . sanitize_title( $attribute_name );
											if ( isset( $variation['attributes'][ $attr_key ] ) && ( $variation['attributes'][ $attr_key ] === $option_slug || $variation['attributes'][ $attr_key ] === $option ) ) {
												$is_oos = ! $variation['is_in_stock'];
												break;
											}
										}
										?>
										<button class="pawwell-size-btn <?php echo $is_selected ? 'active' : ''; ?> <?php echo $is_oos ? 'oos' : ''; ?>"
											data-attribute="<?php echo esc_attr( 'attribute_' . sanitize_title( $attribute_name ) ); ?>"
											data-value="<?php echo esc_attr( $option_slug ); ?>"
											data-display-name="<?php echo esc_attr( $option ); ?>"
											data-tooltip="<?php echo esc_attr( $option ); ?>"
											aria-label="<?php echo esc_attr( $option ); ?>"
											title="<?php echo esc_attr( $option ); ?>">
											<?php echo esc_html( $option ); ?>
										</button>
									<?php endforeach; ?>
								</div>
							<?php else : ?>
								<div class="pawwell-option-chips">
									<?php
									foreach ( $options as $option ) :
										$option_slug = $option;
										if ( taxonomy_exists( $attribute_name ) ) {
											$term        = get_term_by( 'name', $option, $attribute_name );
											$option_slug = $term ? $term->slug : sanitize_title( $option );
										}

										$is_selected = ( $selected_value === $option || $selected_value === $option_slug );
										$is_oos      = false;
										$chip_price  = '';

										foreach ( $variations as $variation ) {
											$attr_key = 'attribute_' . sanitize_title( $attribute_name );
											if ( isset( $variation['attributes'][ $attr_key ] ) && ( $variation['attributes'][ $attr_key ] === $option_slug || $variation['attributes'][ $attr_key ] === $option ) ) {
												$is_oos = ! $variation['is_in_stock'];
												if ( isset( $variation['display_price'] ) && (float) $variation['display_price'] > 0 ) {
													$chip_price = wp_kses_post( wc_price( $variation['display_price'] ) );
												}
												break;
											}
										}
										?>
										<button class="pawwell-chip-opt <?php echo $is_selected ? 'active' : ''; ?> <?php echo $is_oos ? 'oos' : ''; ?>"
											data-attribute="<?php echo esc_attr( 'attribute_' . sanitize_title( $attribute_name ) ); ?>"
											data-value="<?php echo esc_attr( $option_slug ); ?>"
											data-display-name="<?php echo esc_attr( $option ); ?>"
											aria-label="<?php echo esc_attr( $option ); ?>"
											title="<?php echo esc_attr( $option ); ?>">
											<span class="pawwell-chip-name"><?php echo esc_html( $option ); ?></span>
											<?php if ( $chip_price ) : ?>
												<small class="pawwell-chip-price"><?php echo $chip_price; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></small>
											<?php endif; ?>
										</button>
									<?php endforeach; ?>
								</div>
							<?php endif; ?>
						</div>
					<?php endforeach; ?>

						<div class="pawwell-variation-price" id="variationPriceDisplay" style="display:none;">
							<span class="pawwell-variation-price-current" id="variationPriceCurrent"></span>
							<span class="pawwell-variation-price-original" id="variationPriceOriginal"></span>
						</div>

						<form class="cart" method="post" enctype="multipart/form-data">
							<input type="hidden" name="product_id" value="<?php echo esc_attr( $product_id ); ?>">
							<input type="hidden" name="variation_id" value="" id="variation_id">
							<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
							<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product_id ); ?>" class="single_add_to_cart_button alt" style="display:none!important"></button>
							<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
						</form>
					</div>
				<?php else : ?>
					<?php if ( $size_attribute && $size_attribute->get_visible() ) : ?>
					<div class="pawwell-option-group">
						<div class="pawwell-option-label"><?php esc_html_e( 'Size', 'pawwell' ); ?></div>
						<div class="pawwell-size-options">
							<?php
							$terms = wc_get_product_terms( $product_id, $size_attribute->get_name(), array( 'fields' => 'all' ) );
							foreach ( $terms as $term ) :
								?>
								<button class="pawwell-size-btn"
									data-size-name="<?php echo esc_attr( $term->name ); ?>"
									data-tooltip="<?php echo esc_attr( $term->name ); ?>"
									aria-label="<?php echo esc_attr( $term->name ); ?>"
									title="<?php echo esc_attr( $term->name ); ?>">
									<?php echo esc_html( $term->name ); ?>
								</button>
							<?php endforeach; ?>
						</div>
					</div>
					<?php endif; ?>

					<form class="cart" method="post" enctype="multipart/form-data" style="display:none">
						<input type="hidden" name="product_id" value="<?php echo esc_attr( $product_id ); ?>">
						<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product_id ); ?>" class="single_add_to_cart_button alt"></button>
					</form>
				<?php endif; ?>

				<div class="pawwell-pdp-purchase">
					<div class="pawwell-qty-selector">
						<button class="pawwell-qty-btn" data-qty-change="-1">
							<?php echo pawwell_svg_icon( 'minus', 16 ); ?>
						</button>
						<input type="number" id="qtyValue" class="pawwell-qty-input" value="1" min="1">
						<button class="pawwell-qty-btn" data-qty-change="1">
							<?php echo pawwell_svg_icon( 'plus', 16 ); ?>
						</button>
					</div>
					<button class="pawwell-add-to-cart">
						<?php echo pawwell_svg_icon( 'plus', 18 ); ?>
						<span id="cartBtnText"><?php esc_html_e( 'Add to Cart', 'pawwell' ); ?></span>
					</button>
				<?php pawwell_wishlist_button(); ?>
			</div>
		</div>
	</div>
</main>

<?php pawwell_product_trust_strip(); ?>

<?php
$product_long_desc = $product->get_description();
$first_tab         = ! empty( $product_long_desc ) ? 'description' : 'reviews';
?>

<section class="pawwell-tabs-section">
	<div class="pawwell-container">

		<!-- Tabs Nav -->
		<div class="pawwell-tabs-nav" role="tablist">
			<?php if ( ! empty( $product_long_desc ) ) : ?>
				<button class="pawwell-tab-btn<?php echo 'description' === $first_tab ? ' active' : ''; ?>" data-tab="pawwell-tab-description" role="tab" aria-selected="<?php echo 'description' === $first_tab ? 'true' : 'false'; ?>"><?php esc_html_e( 'Description', 'pawwell' ); ?></button>
			<?php endif; ?>

			<?php if ( $product->has_attributes() ) : ?>
				<button class="pawwell-tab-btn" data-tab="pawwell-tab-additional-info" role="tab" aria-selected="false"><?php esc_html_e( 'Additional Information', 'pawwell' ); ?></button>
			<?php endif; ?>

            <?php do_action( 'pawwell_product_tabs_nav_items_before_reviews' ); ?>

            <?php do_action( 'pawwell_product_tabs_nav_items' ); ?>

            <button class="pawwell-tab-btn<?php echo 'reviews' === $first_tab ? ' active' : ''; ?>" data-tab="pawwell-tab-reviews" role="tab" aria-selected="<?php echo 'reviews' === $first_tab ? 'true' : 'false'; ?>"><?php esc_html_e( 'Reviews', 'pawwell' ); ?> <span class="pawwell-tab-count"><?php echo esc_html( $rating_count ); ?></span></button>
		</div>

		<!-- Description Panel -->
		<?php if ( ! empty( $product_long_desc ) ) : ?>
		<div class="pawwell-tab-panel<?php echo 'description' === $first_tab ? ' active' : ''; ?>" id="pawwell-tab-description" role="tabpanel">
			<div class="pawwell-tab-content">
				<?php echo apply_filters( 'pawwell_product_description_content', wp_kses_post( wpautop( $product_long_desc ) ) ); ?>
			</div>
		</div>
		<?php endif; ?>

		<!-- Additional Information Panel -->
		<?php if ( $product->has_attributes() ) : ?>
		<div class="pawwell-tab-panel" id="pawwell-tab-additional-info" role="tabpanel">
			<div class="pawwell-tab-content">
				<?php wc_display_product_attributes( $product ); ?>
			</div>
		</div>
		<?php endif; ?>

		<!-- Pro hooks for additional panels -->
		<?php do_action( 'pawwell_product_tabs_panels' ); ?>

		<!-- Reviews Panel -->
		<div class="pawwell-tab-panel<?php echo 'reviews' === $first_tab ? ' active' : ''; ?>" id="pawwell-tab-reviews" role="tabpanel">
			<div id="reviews">

				<!-- Summary box -->
				<div class="pawwell-reviews-summary-box">
					<div class="pawwell-review-score-box">
						<div class="pawwell-review-score-num"><?php echo esc_html( number_format( $average, 1 ) ); ?></div>
						<div class="pawwell-review-score-stars">
							<?php for ( $i = 1; $i <= 5; $i++ ) : ?>
								<span class="pawwell-star-icon <?php echo $i <= round( $average ) ? 'pawwell-star-filled' : 'pawwell-star-empty'; ?>"><?php echo pawwell_svg_icon( 'star', 20 ); ?></span>
							<?php endfor; ?>
						</div>
						<div class="pawwell-review-score-count"><?php printf( esc_html__( 'Based on %d reviews', 'pawwell' ), $rating_count ); ?></div>
					</div>
					<div class="pawwell-rating-bars">
						<?php for ( $i = 5; $i >= 1; $i-- ) :
							$percentage = $rating_count > 0 ? round( ( $rating_distribution[ $i ] / $rating_count ) * 100 ) : 0;
						?>
							<div class="pawwell-bar-row">
								<span class="pawwell-bar-label"><?php echo esc_html( $i ); ?><?php echo pawwell_svg_icon( 'star', 13 ); ?></span>
								<div class="pawwell-bar-track">
									<div class="pawwell-bar-fill" style="width:<?php echo esc_attr( $percentage ); ?>%"></div>
								</div>
								<span class="pawwell-bar-count"><?php echo esc_html( $percentage ); ?>%</span>
							</div>
						<?php endfor; ?>
					</div>
				</div>

				<button class="pawwell-write-review-btn" id="writeReviewBtn"><?php esc_html_e( 'Write a Review', 'pawwell' ); ?></button>

				<!-- Review cards grid -->
				<div class="pawwell-review-grid">
					<?php if ( ! empty( $comments ) ) : ?>
						<?php
						$review_index = 0;
						foreach ( $comments as $comment ) :
							$comment_rating  = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
							$comment_author  = $comment->comment_author;
							$comment_content = $comment->comment_content;

							$review_title = '';
							$review_text  = $comment_content;
							if ( preg_match( '/<strong>(.*?)<\/strong>\s*\n\s*\n(.*)/s', $comment_content, $matches ) ) {
								$review_title = $matches[1];
								$review_text  = $matches[2];
							}

							$comment_time  = human_time_diff( get_comment_time( 'U' ), time() );
							$author_initial = mb_substr( $comment_author, 0, 1 );
							$is_hidden      = $review_index >= 3;
						?>
							<article class="pawwell-review-card<?php echo $is_hidden ? ' pawwell-review-hidden' : ''; ?>"<?php echo $is_hidden ? ' style="display:none"' : ''; ?>>
								<div class="pawwell-review-head">
									<span class="pawwell-review-stars">
										<?php for ( $s = 1; $s <= 5; $s++ ) : ?>
											<span class="pawwell-star-icon <?php echo $s <= $comment_rating ? 'pawwell-star-filled' : 'pawwell-star-empty'; ?>"><?php echo pawwell_svg_icon( 'star', 15 ); ?></span>
										<?php endfor; ?>
									</span>
									<span class="pawwell-review-date"><?php printf( esc_html__( '%s ago', 'pawwell' ), esc_html( $comment_time ) ); ?></span>
								</div>
								<?php if ( ! empty( $review_title ) ) : ?>
									<div class="pawwell-review-title"><?php echo esc_html( $review_title ); ?></div>
								<?php endif; ?>
								<p class="pawwell-review-text"><?php echo wp_kses_post( $review_text ); ?></p>
								<div class="pawwell-review-author">
									<div class="pawwell-r-avatar"><?php echo esc_html( strtoupper( $author_initial ) ); ?></div>
									<div>
										<div class="pawwell-r-name"><?php echo esc_html( $comment_author ); ?></div>
										<div class="pawwell-r-verified"><?php echo pawwell_svg_icon( 'check', 13 ); ?> <?php esc_html_e( 'Verified Buyer', 'pawwell' ); ?></div>
									</div>
								</div>
							</article>
						<?php
							$review_index++;
						endforeach;
						?>
					<?php else : ?>
						<p class="pawwell-no-reviews"><?php esc_html_e( 'No reviews yet. Be the first to review this product!', 'pawwell' ); ?></p>
					<?php endif; ?>
				</div>

			</div>

			<?php if ( count( $comments ) > 3 ) : ?>
				<button class="pawwell-load-more-reviews" id="loadMoreReviews" data-per-page="3"><?php esc_html_e( 'Load More Reviews', 'pawwell' ); ?></button>
			<?php endif; ?>
        </div>

    </div>
</section>

<div class="pawwell-review-modal" id="reviewModal">
	<div class="pawwell-review-modal-overlay" id="reviewModalOverlay"></div>
	<div class="pawwell-review-modal-content">
		<button class="pawwell-review-modal-close" id="reviewModalClose">
			<?php echo pawwell_svg_icon( 'x', 20 ); ?>
		</button>
		<div class="pawwell-review-modal-header">
			<h3><?php esc_html_e( 'Write a Review', 'pawwell' ); ?></h3>
			<p><?php esc_html_e( 'Share your experience with this product', 'pawwell' ); ?></p>
		</div>
		<form class="pawwell-review-form" id="reviewForm" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
			<input type="hidden" name="action" value="pawwell_submit_review">
			<input type="hidden" name="product_id" value="<?php echo esc_attr( $product_id ); ?>">
			<?php wp_nonce_field( 'pawwell_submit_review', 'pawwell_review_nonce' ); ?>

			<div class="pawwell-form-group">
				<label><?php esc_html_e( 'Your Rating', 'pawwell' ); ?></label>
				<div class="pawwell-star-rating-input" id="starRatingInput">
					<span class="star" data-rating="5"><?php echo pawwell_svg_icon( 'star', 24 ); ?></span>
					<span class="star" data-rating="4"><?php echo pawwell_svg_icon( 'star', 24 ); ?></span>
					<span class="star" data-rating="3"><?php echo pawwell_svg_icon( 'star', 24 ); ?></span>
					<span class="star" data-rating="2"><?php echo pawwell_svg_icon( 'star', 24 ); ?></span>
					<span class="star" data-rating="1"><?php echo pawwell_svg_icon( 'star', 24 ); ?></span>
					<input type="hidden" name="rating" id="ratingInput" value="">
				</div>
			</div>

			<div class="pawwell-form-group">
				<label for="reviewTitle"><?php esc_html_e( 'Review Title', 'pawwell' ); ?></label>
				<input type="text" id="reviewTitle" name="review_title" placeholder="<?php esc_attr_e( 'Give your review a title', 'pawwell' ); ?>" required>
			</div>

			<div class="pawwell-form-group">
				<label for="reviewContent"><?php esc_html_e( 'Your Review', 'pawwell' ); ?></label>
				<textarea id="reviewContent" name="review_content" rows="5" placeholder="<?php esc_attr_e( 'What did you like or dislike?', 'pawwell' ); ?>" required></textarea>
			</div>

			<div class="pawwell-form-row">
				<div class="pawwell-form-group">
					<label for="reviewerName"><?php esc_html_e( 'Your Name', 'pawwell' ); ?></label>
					<input type="text" id="reviewerName" name="reviewer_name" placeholder="<?php esc_attr_e( 'Enter your name', 'pawwell' ); ?>" required>
				</div>
				<div class="pawwell-form-group">
					<label for="reviewerEmail"><?php esc_html_e( 'Email', 'pawwell' ); ?></label>
					<input type="email" id="reviewerEmail" name="reviewer_email" placeholder="<?php esc_attr_e( 'Enter your email', 'pawwell' ); ?>" required>
				</div>
			</div>

			<button type="submit" class="pawwell-submit-review"><?php esc_html_e( 'Submit Review', 'pawwell' ); ?></button>
		</form>
	</div>
</div>

<?php if ( ! empty( $related_products ) ) : ?>
<section class="pawwell-related-section">
	<div class="pawwell-container">
		<div class="pawwell-related-head">
			<div class="pawwell-related-head-text">
				<span class="pawwell-related-eyebrow"><?php esc_html_e( 'You May Also Like', 'pawwell' ); ?></span>
				<h2 class="pawwell-related-title"><?php esc_html_e( 'Related Products', 'pawwell' ); ?></h2>
			</div>
			<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ); ?>" class="pawwell-related-viewall">
				<?php esc_html_e( 'View All', 'pawwell' ); ?>
				<?php echo pawwell_svg_icon( 'chevron-right', 16 ); ?>
			</a>
		</div>
		<div class="pawwell-related-grid">
			<?php foreach ( $related_products as $related_product_id ) :
				$related_post = get_post( $related_product_id );
				if ( ! $related_post ) {
					continue;
				}
				$GLOBALS['post']    = $related_post;
				$GLOBALS['product'] = wc_get_product( $related_product_id );
				if ( ! $GLOBALS['product'] ) {
					continue;
				}
				setup_postdata( $related_post );
				wc_get_template_part( 'content', 'product' );
			endforeach;
			wp_reset_postdata();
			?>
		</div>
	</div>
</section>
<?php endif; ?>

<div id="lightbox" class="pawwell-lightbox">
	<button class="pawwell-lightbox-close"><?php echo pawwell_svg_icon( 'x', 24 ); ?></button>
	<img id="lightboxImg" src="" alt="<?php esc_attr_e( 'Zoom', 'pawwell' ); ?>">
</div>

<button id="scrollTop" class="pawwell-scroll-top">
	<?php echo pawwell_svg_icon( 'chevron-up', 20 ); ?>
</button>

<?php get_footer(); ?>
