<?php
/**
 * Reader favorites — Woo query with hardcoded fallback.
 *
 * @package Foliora
 */

$skus     = array( 'FOL-EMB-001', 'FOL-SLV-VIN', 'FOL-LGT-001', 'FOL-ANN-KIT' );
$catalog  = foliora_catalog_fallback();
$products = array();

if ( foliora_wc_ready() ) {
	foreach ( $skus as $sku ) {
		$id = wc_get_product_id_by_sku( $sku );
		if ( $id ) {
			$products[ $sku ] = wc_get_product( $id );
		}
	}
}
?>
<section class="section bestsellers" id="bestsellers" aria-labelledby="best-title">
	<div class="wrap">
		<header class="section-head" data-reveal>
			<p class="eyebrow">Reader favorites</p>
			<h2 id="best-title">The little things our readers keep reaching for.</h2>
		</header>
		<ul class="product-row">
			<?php foreach ( $skus as $sku ) : ?>
				<?php
				$item    = $catalog[ $sku ];
				$product = isset( $products[ $sku ] ) ? $products[ $sku ] : null;
				$name    = $product ? $product->get_name() : $item['name'];
				$url     = $product ? $product->get_permalink() : foliora_product_url( $sku );
				$img     = $product ? foliora_product_card_image( $product ) : foliora_image_uri( $item['image'] );
				$hover   = $product ? foliora_hover_image_url( $product ) : foliora_image_uri( $item['hover'] );
				$badge   = $item['badge'];
				$price   = $product ? $product->get_price_html() : '<span class="price"><span class="woocommerce-Price-amount">' . esc_html( foliora_money( $item['regular'] ) ) . '</span></span>';
				$pid     = $product ? $product->get_id() : 0;
				$swatches = ( 'FOL-SLV-VIN' === $sku ) ? foliora_sleeve_swatches() : array();
				?>
				<li class="p-card" data-reveal>
					<a class="p-card__media" href="<?php echo esc_url( $url ); ?>">
						<img class="p-card__img" src="<?php echo esc_url( $img ); ?>" alt="" width="800" height="1000">
						<?php if ( $hover ) : ?>
							<img class="p-card__img p-card__img--hover" src="<?php echo esc_url( $hover ); ?>" alt="" width="800" height="1000">
						<?php endif; ?>
						<?php if ( $badge ) : ?>
							<span class="p-card__badge"><?php echo esc_html( $badge ); ?></span>
						<?php endif; ?>
					</a>
					<div class="p-card__body">
						<h3 class="p-card__title"><a href="<?php echo esc_url( $url ); ?>"><?php echo esc_html( $name ); ?></a></h3>
						<p class="p-card__stars"><span class="stars" aria-hidden="true">★★★★★</span> <?php echo esc_html( $item['rating_display'] ); ?></p>
						<p class="p-card__price"><?php echo wp_kses_post( $price ); ?></p>
						<?php if ( $swatches ) : ?>
							<p class="swatches" aria-label="Colors">
								<?php foreach ( $swatches as $color => $file ) : ?>
									<a class="swatch swatch--<?php echo esc_attr( sanitize_title( $color ) ); ?>" href="<?php echo esc_url( $url ); ?>" title="<?php echo esc_attr( $color ); ?>"><span class="visually-hidden"><?php echo esc_html( $color ); ?></span></a>
								<?php endforeach; ?>
							</p>
						<?php endif; ?>
						<?php if ( $product && $product->is_purchasable() && $product->is_in_stock() ) : ?>
							<?php if ( $product->is_type( 'simple' ) ) : ?>
								<a
									class="button button--small ajax_add_to_cart add_to_cart_button"
									href="<?php echo esc_url( $product->add_to_cart_url() ); ?>"
									data-product_id="<?php echo esc_attr( (string) $pid ); ?>"
									data-product_sku="<?php echo esc_attr( $sku ); ?>"
									data-quantity="1"
									aria-label="<?php echo esc_attr( 'Quick add ' . $name ); ?>"
								>Quick Add</a>
							<?php else : ?>
								<?php
								$kids = $product->get_children();
								$vid  = $kids ? $kids[0] : 0;
								?>
								<a
									class="button button--small ajax_add_to_cart add_to_cart_button"
									href="<?php echo esc_url( $url ); ?>"
									data-product_id="<?php echo esc_attr( (string) $pid ); ?>"
									data-variation_id="<?php echo esc_attr( (string) $vid ); ?>"
									data-product_sku="<?php echo esc_attr( $sku ); ?>"
									data-quantity="1"
								>Quick Add</a>
							<?php endif; ?>
						<?php else : ?>
							<a class="button button--small" href="<?php echo esc_url( $url ); ?>">View</a>
						<?php endif; ?>
					</div>
				</li>
			<?php endforeach; ?>
		</ul>
	</div>
</section>
