<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/**
 * Sifency Single Product Page Renderer
 * ------------------------------------
 * Replaces WooCommerce single product layout with an Elementor template.
 */
final class Sifency_Single_Product_Render {
	private static $instance = null;
	public function __construct() {
		if ( is_admin() ) return;
		add_action( 'template_redirect', [ $this, 'setup_single_product_override' ], 5 );
	}

	/**
	 * Safely detect and override WooCommerce single product pages
	 */
	public function setup_single_product_override() {

		// Prevent conflicts inside admin, AJAX, cron, REST, etc.
		if ( is_admin() || wp_doing_ajax() || wp_doing_cron() || defined( 'REST_REQUEST' ) ) {
			return;
		}

		if ( ! function_exists( 'is_product' ) ) {
			return; // WooCommerce disabled
		}

		// Only run on actual product pages
		if ( ! is_product() ) {
			return;
		}

		// Fetch Elementor template for single product
		$sifency_addons_template_id = Sifency_Frontend_Helper::find_matching_template( 'product' );

		if ( empty( $sifency_addons_template_id ) || get_post_status( $sifency_addons_template_id ) !== 'publish' ) {
			return;
		}

		// Remove all standard WooCommerce single product hooks
		if ( method_exists( $this, 'remove_all_single_product_actions' ) ) {
			$this->remove_all_single_product_actions();
		}

		/**
		 * Render Elementor template safely inside the body,
		 * after theme and WooCommerce wrappers are loaded.
		 */
		add_action( 'wp_body_open', function() use ( $sifency_addons_template_id ) {

			if ( ! did_action( 'elementor/loaded' ) ) {
				return; // Prevent fatal errors if Elementor is disabled
			}

			echo '<div class="sifency-single-product-template">';

			if ( method_exists( $this, 'render_elementor_single_product' ) ) {
				$this->render_elementor_single_product( $sifency_addons_template_id );
			}

			echo '</div>';

		}, 25 );
	}


	/**
	 * Remove WooCommerce's single product structure
	 */
	public function remove_all_single_product_actions() {
		// Breadcrumb and notices
		remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
		remove_action( 'woocommerce_before_main_content', 'woocommerce_output_all_notices', 10 );
		remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
		remove_action( 'woocommerce_before_single_product', 'wc_print_notices', 10 );

		// Product summary (title, price, etc.)
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );

		// Tabs, related, upsells, sidebar
		remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 20 );
		remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
		remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );

		// Default wrappers
		remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
		remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
	}

	/**
	 * Render Elementor Single Product Template outside theme content
	 */
	public function render_elementor_single_product( $sifency_addons_template_id ) {
		if ( ! class_exists( '\Elementor\Plugin' ) ) return;

		echo '<!-- Sifency Elementor Single Product Start -->';

		get_header();

		echo '<div class="sifency-elementor-single-product-wrapper">';
		Sifency_Frontend_Helper::render_elementor_content( $sifency_addons_template_id, true );
		echo '</div>';

		get_footer();

		echo '<!-- Sifency Elementor Single Product End -->';

		exit; // Prevent default WooCommerce rendering
	}

	/**
	 * Singleton Instance
	 */
	public static function get_instance() {
		if ( self::$instance === null ) {
			self::$instance = new self();
		}
		return self::$instance;
	}
}

// Initialize
Sifency_Single_Product_Render::get_instance();
