<?php
/**
 * Product image URL and DOM-context filtering for partner imports.
 *
 * @package Niwa_AI
 */

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

class Niwa_AI_Admin_Partner_Product_Image_Context {
	public function is_product_image_url( $url ) {
		$path = strtolower( (string) wp_parse_url( $url, PHP_URL_PATH ) );
		if ( ! preg_match( '/\.(jpe?g|png|webp)(?:$|\?)/i', $path ) ) {
			return false;
		}
		foreach ( $this->blocked_image_terms() as $bad ) {
			if ( false !== strpos( $path, $bad ) ) {
				return false;
			}
		}
		return true;
	}

	public function canonical_image_key( $url ) {
		$path = (string) wp_parse_url( $url, PHP_URL_PATH );
		$path = preg_replace( '/-\d+x\d+(?=\.(?:jpe?g|png|webp)$)/i', '', $path );
		return strtolower( $path );
	}

	public function srcset_best_url( $srcset ) {
		$best_url   = '';
		$best_width = 0;
		foreach ( explode( ',', (string) $srcset ) as $srcset_part ) {
			$piece = trim( $srcset_part );
			if ( '' === $piece ) {
				continue;
			}
			$bits  = preg_split( '/\s+/', $piece );
			$url   = isset( $bits[0] ) ? trim( (string) $bits[0] ) : '';
			$width = 0;
			if ( isset( $bits[1] ) && preg_match( '/^(\d+)w$/', (string) $bits[1], $m ) ) {
				$width = intval( $m[1] );
			}
			if ( '' !== $url && ( $width >= $best_width || '' === $best_url ) ) {
				$best_url   = $url;
				$best_width = $width;
			}
		}
		return $best_url;
	}

	public function is_product_gallery_image_node( $img ) {
		if ( ! $img || ! method_exists( $img, 'getAttribute' ) ) {
			return false;
		}

		$attr_haystack = strtolower(
			implode(
				' ',
				array(
					(string) $img->getAttribute( 'class' ),
					(string) $img->getAttribute( 'id' ),
					(string) $img->getAttribute( 'alt' ),
					(string) $img->getAttribute( 'data-large_image' ),
					(string) $img->getAttribute( 'data-zoom-image' ),
				)
			)
		);

		if ( $this->contains_non_product_context( $attr_haystack ) ) {
			return false;
		}
		$has_direct_gallery_signal = false !== strpos( $attr_haystack, 'wp-post-image' )
			|| false !== strpos( $attr_haystack, 'zoom' )
			|| false !== strpos( $attr_haystack, 'woocommerce-product-gallery__image' )
			|| '' !== trim( (string) $img->getAttribute( 'data-large_image' ) )
			|| '' !== trim( (string) $img->getAttribute( 'data-zoom-image' ) );

		$node  = $img->parentNode;
		$depth = 0;
		while ( $node && $depth < 10 ) {
			if ( method_exists( $node, 'getAttribute' ) ) {
				$context = strtolower(
					implode(
						' ',
						array(
							(string) $node->getAttribute( 'class' ),
							(string) $node->getAttribute( 'id' ),
							(string) $node->getAttribute( 'role' ),
						)
					)
				);
				if ( $this->contains_non_product_context( $context ) ) {
					return false;
				}
				if ( $this->contains_product_context( $context ) ) {
					return true;
				}
			}
			$node = $node->parentNode;
			$depth++;
		}

		return $has_direct_gallery_signal;
	}

	public function is_product_gallery_link_node( $link ) {
		if ( ! $link || ! method_exists( $link, 'getAttribute' ) ) {
			return false;
		}

		$href = trim( (string) $link->getAttribute( 'href' ) );
		if ( '' === $href || ! $this->is_product_image_url( $href ) ) {
			return false;
		}

		$attr_haystack = strtolower(
			implode(
				' ',
				array(
					(string) $link->getAttribute( 'class' ),
					(string) $link->getAttribute( 'id' ),
					(string) $link->getAttribute( 'data-caption' ),
					$href,
				)
			)
		);
		$attr_haystack = str_replace( 'lightbox-icon', 'lightbox', $attr_haystack );
		if ( $this->contains_non_product_context( $attr_haystack ) ) {
			return false;
		}

		$has_direct_gallery_signal = false !== strpos( $attr_haystack, 'lightbox' )
			|| false !== strpos( $attr_haystack, 'zoom' )
			|| false !== strpos( $attr_haystack, 'woocommerce-product-gallery__image' );

		$node  = $link->parentNode;
		$depth = 0;
		while ( $node && $depth < 10 ) {
			if ( method_exists( $node, 'getAttribute' ) ) {
				$context = strtolower(
					implode(
						' ',
						array(
							(string) $node->getAttribute( 'class' ),
							(string) $node->getAttribute( 'id' ),
							(string) $node->getAttribute( 'role' ),
						)
					)
				);
				if ( $this->contains_non_product_context( $context ) ) {
					return false;
				}
				if ( $this->contains_product_context( $context ) ) {
					return true;
				}
			}
			$node = $node->parentNode;
			$depth++;
		}

		return $has_direct_gallery_signal;
	}

	public function contains_product_context( $context ) {
		foreach ( array( 'woocommerce-product-gallery', 'product-gallery', 'product_gallery', 'product-images', 'product_images', 'product-thumbnails', 'product_thumbnail', 'product-main-image', 'product-image-main', 'single-product-main-image', 'flex-viewport', 'zoom-gallery', 'photoswipe', 'swiper-product', 'product-slider', 'product-carousel' ) as $good ) {
			if ( false !== strpos( (string) $context, $good ) ) {
				return true;
			}
		}
		return false;
	}

	public function contains_non_product_context( $context ) {
		foreach ( array( 'related', 'recommend', 'upsell', 'cross-sell', 'also-bought', 'recently-viewed', 'trust', 'badge', 'payment', 'secure', 'logo', 'brand-logo', 'header', 'footer', 'newsletter', 'social', 'share', 'widget', 'menu', 'breadcrumb', 'review', 'avatar', 'icon', 'banner', 'promo' ) as $bad ) {
			if ( false !== strpos( (string) $context, $bad ) ) {
				return true;
			}
		}
		return false;
	}

	private function blocked_image_terms() {
		return array( 'logo', 'icon', 'sprite', 'placeholder', 'spinner', 'loader', 'avatar', 'payment', 'banner', 'badge', 'trust', 'seal', 'secure', 'paypal', 'visa', 'mastercard', 'shipping', 'delivery', 'newsletter', 'social', 'facebook', 'instagram', 'recommended', 'related', 'upsell', 'cross-sell' );
	}
}
