<?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', array( $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;
		}

		if ( ! is_product() ) {
			return;
		}

		$sifency_addons_template_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'product' );

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

		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;
				}

				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() {
		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 );

		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 );

		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 );

		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 -->';
		$shop_template_id = get_query_var( 'sifency_shop_template_id' );
		$header_id        = Sifency_Addons_Frontend_Helper::find_matching_template( 'header' );

		if ( $header_id ) {
			get_header();
			echo '<header id="sifency-custom-header" class="sifency-header">';
             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
			echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $header_id, true );
			echo '</header>';
		} else {
			get_header();
		}

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

		get_footer();

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

		exit;
	}

	/**
	 * 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();
