<?php
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the 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 does
 * 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 8.6.0
 */

defined( 'ABSPATH' ) || exit;

remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );

get_header( 'shop' );
?>

<div class="shop-archive-wrapper">

	<?php get_template_part( 'template-parts/shop/hero' ); ?>

	<div class="container">
		<?php
		/**
		 * Shop by Category quick-links strip.
		 * Uses the free helper function pawwell_get_shop_categories().
		 * Only render on the main shop page (not on a single category archive).
		 */
		if ( is_shop() && get_theme_mod( 'pawwell_woo_archive_category_enable', true ) ) :
			$pet_categories = function_exists( 'pawwell_get_shop_categories' ) ? pawwell_get_shop_categories( 8 ) : array();
			if ( ! empty( $pet_categories ) && ! is_wp_error( $pet_categories ) ) :
		?>
		<section class="shop-pet-section">
			<div class="shop-pet-head">
				<div>
					<div class="shop-pet-eyebrow"><?php esc_html_e( 'Quick Links', 'pawwell' ); ?></div>
					<h2 class="shop-pet-heading"><?php esc_html_e( 'Shop by Category', 'pawwell' ); ?></h2>
				</div>
			</div>
			<div class="shop-pet-grid">
				<?php foreach ( $pet_categories as $pet_cat ) :
					$pet_link = get_term_link( $pet_cat );
					if ( is_wp_error( $pet_link ) ) {
						$pet_link = '#';
					}
					?>
					<a href="<?php echo esc_url( $pet_link ); ?>" class="shop-pet-card">
						<span class="shop-pet-avatar">
							<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9-9-9z"/><circle cx="7.5" cy="7.5" r="1.4"/></svg>
						</span>
						<span class="shop-pet-name"><?php echo esc_html( $pet_cat->name ); ?></span>
						<span class="shop-pet-count"><?php
							printf(
								/* translators: %d: number of items */
								esc_html__( '%d items', 'pawwell' ),
								esc_html( (int) $pet_cat->count )
							);
						?></span>
					</a>
				<?php endforeach; ?>
			</div>
		</section>
		<?php
			endif;
		endif;
		?>

		<div class="shop-toolbar" id="shopFilterBar">
			<div class="shop-toolbar-left">
				<div class="shop-toolbar-count">
					<?php
					$toolbar_total = wc_get_loop_prop( 'total', 0 );
					printf(
						/* translators: %d: total products */
						esc_html__( 'Showing %d products', 'pawwell' ),
						esc_html( (int) $toolbar_total )
					);
					?>
				</div>
			</div>

			<div class="shop-toolbar-right">
				<div class="shop-filter-group" id="categoryFilters">
					<?php
					$categories  = get_terms( array(
						'taxonomy'   => 'product_cat',
						'hide_empty' => true,
						'number'     => 6,
						'orderby'    => 'count',
						'order'      => 'DESC',
					) );

					$current_cat    = is_product_category() ? get_queried_object()->slug : '';
					$is_all_active  = empty( $current_cat ) && ! isset( $_GET['product_cat'] );
					$shop_url       = get_permalink( wc_get_page_id( 'shop' ) );
					?>
					<a href="<?php echo esc_url( $shop_url ); ?>" class="filter-pill<?php echo $is_all_active ? ' active-filter' : ''; ?>"><?php esc_html_e( 'All', 'pawwell' ); ?></a>

					<?php if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) : ?>
						<?php foreach ( $categories as $category ) :
							$is_active = ( $current_cat === $category->slug );
							$cat_link  = get_term_link( $category );
							$cat_url   = ! is_wp_error( $cat_link ) ? $cat_link : add_query_arg( 'product_cat', $category->slug, $shop_url );
							?>
							<a href="<?php echo esc_url( $cat_url ); ?>" class="filter-pill<?php echo $is_active ? ' active-filter' : ''; ?>"><?php echo esc_html( $category->name ); ?></a>
						<?php endforeach; ?>
					<?php endif; ?>

					<span class="filter-pill filter-divider" aria-hidden="true">|</span>

					<a href="<?php echo esc_url( add_query_arg( 'filter_new', '1', $shop_url ) ); ?>" class="filter-pill"><?php esc_html_e( 'New', 'pawwell' ); ?></a>
					<a href="<?php echo esc_url( add_query_arg( 'on_sale', '1', $shop_url ) ); ?>" class="filter-pill"><?php esc_html_e( 'Sale', 'pawwell' ); ?></a>
				</div>

				<div class="shop-sort-wrapper">
					<span class="sort-label"><?php esc_html_e( 'Sort by', 'pawwell' ); ?></span>
					<?php
					if ( function_exists( 'woocommerce_catalog_ordering' ) ) {
						woocommerce_catalog_ordering();
					}
					?>
				</div>
			</div>
		</div>

		<main class="shop-main">
			<?php if ( woocommerce_product_loop() ) : ?>

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

				<?php woocommerce_product_loop_start(); ?>

				<?php if ( wc_get_loop_prop( 'total' ) ) : ?>
					<?php while ( have_posts() ) : the_post(); ?>
						<?php do_action( 'woocommerce_shop_loop' ); ?>
						<?php wc_get_template_part( 'content', 'product' ); ?>
					<?php endwhile; ?>
				<?php endif; ?>

				<?php woocommerce_product_loop_end(); ?>

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

			<?php else : ?>

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

			<?php endif; ?>
		</main>
	</div>
</div>

<?php
do_action( 'woocommerce_after_main_content' );

get_footer( 'shop' );
