<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * The sidebar containing the shop widget area
 *
 * @package Pawfect
 */

if ( ! is_active_sidebar( 'shop-sidebar' ) ) {
	return;
}
?>

<div id="secondary" class="pawfect-shop-sidebar pawfect-space-y-6">

	<!-- Enhanced Search Widget -->
	<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
		<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
			<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-orange-400 pawfect-to-orange-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
				<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
				</svg>
			</div>
			<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
				<?php esc_html_e( 'Search Products', 'pawfect' ); ?>
			</h3>
		</div>
		<div class="pawfect-relative">
			<?php get_product_search_form(); ?>
		</div>
	</div>

	<!-- Enhanced Categories Widget -->
	<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
		<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
			<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-blue-400 pawfect-to-blue-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
				<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
				</svg>
			</div>
			<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
				<?php esc_html_e( 'Shop by Category', 'pawfect' ); ?>
			</h3>
		</div>
		<?php
		$product_categories = get_terms( array(
			'taxonomy' => 'product_cat',
			'hide_empty' => true,
			'parent' => 0,
		) );

		if ( ! empty( $product_categories ) && ! is_wp_error( $product_categories ) ) : ?>
			<div class="pawfect-space-y-2">
				<?php foreach ( $product_categories as $category ) : ?>
					<div class="pawfect-group">
						<a href="<?php echo esc_url( get_term_link( $category ) ); ?>"
						   class="pawfect-flex pawfect-justify-between pawfect-items-center pawfect-p-3 pawfect-rounded-lg pawfect-text-gray-700 hover:pawfect-bg-orange-50 hover:pawfect-text-orange-600 pawfect-transition-all pawfect-duration-200 group-hover:pawfect-shadow-sm">
							<div class="pawfect-flex pawfect-items-center">
								<div class="pawfect-w-2 pawfect-h-2 pawfect-bg-orange-400 pawfect-rounded-full pawfect-mr-3 group-hover:pawfect-bg-orange-500"></div>
								<span class="pawfect-font-medium"><?php echo esc_html( $category->name ); ?></span>
							</div>
							<span class="pawfect-text-xs pawfect-bg-gray-100 pawfect-text-gray-600 pawfect-px-3 pawfect-py-1 pawfect-rounded-full group-hover:pawfect-bg-orange-100 group-hover:pawfect-text-orange-700 pawfect-transition-colors">
								<?php echo esc_html( $category->count ); ?>
							</span>
						</a>
					</div>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>

	<!-- Enhanced Price Filter Widget -->
	<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
		<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
			<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-green-400 pawfect-to-green-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
				<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1"></path>
				</svg>
			</div>
			<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
				<?php esc_html_e( 'Price Range', 'pawfect' ); ?>
			</h3>
		</div>
		<div class="pawfect-price-filter-enhanced">
			<?php if (class_exists('WC_Widget_Price_Filter')) {
				the_widget('WC_Widget_Price_Filter');
			} ?>
		</div>
	</div>

	<!-- Enhanced Product Tags Filter -->
	<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
		<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
			<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-purple-400 pawfect-to-purple-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
				<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
				</svg>
			</div>
			<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
				<?php esc_html_e( 'Product Tags', 'pawfect' ); ?>
			</h3>
		</div>
		<?php
		$product_tags = get_terms( array(
			'taxonomy' => 'product_tag',
			'hide_empty' => true,
			'number' => 10,
		) );

		if ( ! empty( $product_tags ) && ! is_wp_error( $product_tags ) ) : ?>
			<div class="pawfect-flex pawfect-flex-wrap pawfect-gap-2">
				<?php foreach ( $product_tags as $tag ) : ?>
					<a href="<?php echo esc_url( get_term_link( $tag ) ); ?>" class="pawfect-inline-block pawfect-bg-purple-100 pawfect-text-purple-700 hover:pawfect-bg-purple-500 hover:pawfect-text-white pawfect-px-4 pawfect-py-2 pawfect-rounded-full pawfect-text-sm pawfect-font-medium pawfect-transition-all pawfect-duration-200 hover:pawfect-shadow-md">
						<?php echo esc_html( $tag->name ); ?>
					</a>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>
	</div>

	<!-- Product Attributes Filter -->
	<?php
	$attributes = wc_get_attribute_taxonomies();
	if ( ! empty( $attributes ) ) :
		foreach ( $attributes as $attribute ) :
			$taxonomy = wc_attribute_taxonomy_name( $attribute->attribute_name );
			$terms = get_terms( array(
				'taxonomy' => $taxonomy,
				'hide_empty' => true,
			) );

			if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) : ?>
				<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
					<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
						<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-indigo-400 pawfect-to-indigo-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
							<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
								<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4M7.835 4.697a3.42 3.42 0 001.946-.806 3.42 3.42 0 014.438 0 3.42 3.42 0 001.946.806 3.42 3.42 0 013.138 3.138 3.42 3.42 0 00.806 1.946 3.42 3.42 0 010 4.438 3.42 3.42 0 00-.806 1.946 3.42 3.42 0 01-3.138 3.138 3.42 3.42 0 00-1.946.806 3.42 3.42 0 01-4.438 0 3.42 3.42 0 00-1.946-.806 3.42 3.42 0 01-3.138-3.138 3.42 3.42 0 00-.806-1.946 3.42 3.42 0 010-4.438 3.42 3.42 0 00.806-1.946 3.42 3.42 0 013.138-3.138z"></path>
							</svg>
						</div>
						<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
							<?php echo esc_html( $attribute->attribute_label ); ?>
						</h3>
					</div>
					<div class="pawfect-space-y-2">
						<?php foreach ( $terms as $term ) : ?>
							<div class="pawfect-group">
								<a href="<?php echo esc_url( get_term_link( $term ) ); ?>" class="pawfect-flex pawfect-justify-between pawfect-items-center pawfect-p-3 pawfect-rounded-lg pawfect-text-gray-700 hover:pawfect-bg-indigo-50 hover:pawfect-text-indigo-600 pawfect-transition-all pawfect-duration-200 group-hover:pawfect-shadow-sm">
									<div class="pawfect-flex pawfect-items-center">
										<div class="pawfect-w-2 pawfect-h-2 pawfect-bg-indigo-400 pawfect-rounded-full pawfect-mr-3 group-hover:pawfect-bg-indigo-500"></div>
										<span class="pawfect-font-medium"><?php echo esc_html( $term->name ); ?></span>
									</div>
									<span class="pawfect-text-xs pawfect-bg-gray-100 pawfect-text-gray-600 pawfect-px-3 pawfect-py-1 pawfect-rounded-full group-hover:pawfect-bg-indigo-100 group-hover:pawfect-text-indigo-700 pawfect-transition-colors">
										<?php echo esc_html( $term->count ); ?>
									</span>
								</a>
							</div>
						<?php endforeach; ?>
					</div>
				</div>
			<?php endif;
		endforeach;
	endif; ?>

	<!-- Enhanced Sort Options -->
	<div class="pawfect-bg-white pawfect-p-8 pawfect-rounded-xl pawfect-shadow-lg pawfect-border pawfect-border-gray-100">
		<div class="pawfect-flex pawfect-items-center pawfect-mb-6">
			<div class="pawfect-w-10 pawfect-h-10 pawfect-bg-gradient-to-r pawfect-from-pink-400 pawfect-to-pink-500 pawfect-rounded-lg pawfect-flex pawfect-items-center pawfect-justify-center pawfect-mr-3">
				<svg class="pawfect-w-5 pawfect-h-5 pawfect-text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
					<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 4h13M3 8h9m-9 4h9m5-4v12m0 0l-4-4m4 4l4-4"></path>
				</svg>
			</div>
			<h3 class="pawfect-text-xl pawfect-font-bold pawfect-text-gray-900">
				<?php esc_html_e( 'Sort Products', 'pawfect' ); ?>
			</h3>
		</div>
		<form method="get" action="<?php echo esc_url( wc_get_page_permalink( 'shop' ) ); ?>">
			<select name="orderby" class="pawfect-w-full pawfect-p-4 pawfect-border-2 pawfect-border-gray-200 pawfect-rounded-lg pawfect-text-gray-700 pawfect-bg-gray-50 focus:pawfect-outline-none focus:pawfect-ring-2 focus:pawfect-ring-pink-400 focus:pawfect-border-pink-400 focus:pawfect-bg-white pawfect-transition-all" onchange="this.form.submit()">
				<option value="menu_order" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : 'menu_order', 'menu_order' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Default sorting', 'pawfect' ); ?>
				</option>
				<option value="popularity" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '', 'popularity' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Sort by popularity', 'pawfect' ); ?>
				</option>
				<option value="rating" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '', 'rating' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Sort by average rating', 'pawfect' ); ?>
				</option>
				<option value="date" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '', 'date' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Sort by latest', 'pawfect' ); ?>
				</option>
				<option value="price" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '', 'price' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Sort by price: low to high', 'pawfect' ); ?>
				</option>
				<option value="price-desc" <?php selected( isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : '', 'price-desc' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended ?>>
					<?php esc_html_e( 'Sort by price: high to low', 'pawfect' ); ?>
				</option>
			</select>
			<?php
			// Preserve other query parameters (sanitized)
			if ( ! empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			foreach ( $_GET as $raw_key => $raw_value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
					$key   = sanitize_text_field( wp_unslash( $raw_key ) );
					$value = sanitize_text_field( wp_unslash( $raw_value ) );
					if ( 'orderby' !== $key ) {
						echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) . '">';
					}
				}
			}
			?>
		</form>
	</div>

</div><!-- #secondary -->
