<?php
/**
 * WooCommerce Product V2 integration contributor.
 *
 * Emits Product/ProductGroup only for real WooCommerce product objects and
 * tags them as integration sources. Never gated by the legacy global Product
 * catalogue checkbox.
 *
 * @package SEOvaultSEO\WooCommerce
 */

namespace SEOvaultSEO\WooCommerce;

use SEOvaultSEO\Schema\SchemaIntegrationContributor;
use SEOvaultSEO\Schema\SchemaIntegrationRegistry;

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

/**
 * Objective WooCommerce Product schema contribution.
 */
class ProductSchemaContributor implements SchemaIntegrationContributor {

	const INTEGRATION_ID = 'woocommerce';

	/**
	 * Whether the contributor has been registered for this request.
	 *
	 * @var bool
	 */
	private static $registered = false;

	/**
	 * Register with the V2 integration registry (idempotent).
	 *
	 * @return void
	 */
	public static function register() {
		if ( self::$registered ) {
			return;
		}
		self::$registered = true;

		add_filter(
			'seovaultseo_schema_integration_contributors',
			static function ( $contributors ) {
				if ( ! is_array( $contributors ) ) {
					$contributors = array();
				}
				$contributors[] = new self();
				return $contributors;
			}
		);

		// Clear cached registry so the filter is observed in the same request.
		SchemaIntegrationRegistry::reset();
	}

	/**
	 * Reset registration flag (tests).
	 *
	 * @return void
	 */
	public static function reset_registration() {
		self::$registered = false;
		SchemaIntegrationRegistry::reset();
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_integration_id() {
		return self::INTEGRATION_ID;
	}

	/**
	 * {@inheritdoc}
	 */
	public function collect_tagged_schemas( $object ) {
		$post_id = $this->resolve_product_post_id( $object );
		if ( $post_id <= 0 ) {
			return array();
		}

		$woo = WooCommerce::get_instance();
		$product = $woo->get_product_by_id( $post_id );
		if ( ! is_object( $product ) ) {
			return array();
		}

		$schema = $woo->build_product_schema( $post_id );
		if ( ! is_array( $schema ) || empty( $schema ) ) {
			return array();
		}

		return array(
			array(
				'source'     => SchemaIntegrationRegistry::SOURCE,
				'source_ref' => SchemaIntegrationRegistry::SOURCE . ':' . self::INTEGRATION_ID . ':product:' . $post_id,
				'schema'     => $schema,
			),
		);
	}

	/**
	 * {@inheritdoc}
	 */
	public function owned_types( $object ) {
		$post_id = $this->resolve_product_post_id( $object );
		if ( $post_id <= 0 ) {
			return array();
		}

		$product = WooCommerce::get_instance()->get_product_by_id( $post_id );
		if ( ! is_object( $product ) ) {
			return array();
		}

		return array( 'Product', 'ProductGroup' );
	}

	/**
	 * Woo Product still passes through Product readiness after sanitize.
	 * Trusted bypass remains available for other documented integrations.
	 *
	 * {@inheritdoc}
	 */
	public function is_trusted_type( $type, array $node, array $context = array() ) {
		unset( $type, $node, $context );
		return false;
	}

	/**
	 * Resolve a WooCommerce product post ID from an explicit object or query.
	 *
	 * @param \WP_Post|\WP_Term|\WP_User|null $object Object.
	 * @return int
	 */
	private function resolve_product_post_id( $object ) {
		if ( $object instanceof \WP_Post ) {
			if ( 'product' !== $object->post_type ) {
				return 0;
			}
			return (int) $object->ID;
		}

		if ( null !== $object ) {
			return 0;
		}

		if ( ! function_exists( 'is_singular' ) || ! is_singular( 'product' ) ) {
			return 0;
		}

		return (int) get_queried_object_id();
	}
}
