<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Handles Elementor-based shop and product archive rendering.
 * Replaces default WooCommerce output with a custom Elementor template.
 */
final class Sifency_Shop_Page_Render {

	private static $instance = null;

	public function __construct() {
		if ( is_admin() ) {
			return;
		}
		if (
			Sifency_Addons_Theme_Builder_Master::sync_environment_state() &&
			class_exists( 'WooCommerce' )
		) {
			add_action( 'template_redirect', array( $this, 'sifency_addons_override_shop_page' ), 5 );
		}
	}

	/**
	 * Checks if we're on the shop or a product archive and renders Elementor template.
	 */
	public function sifency_addons_override_shop_page() {
		if ( ! is_shop() && ! is_product_taxonomy() ) {
			return;
		}

		$template_id = Sifency_Addons_Frontend_Helper::find_matching_template( 'shop' );
		if ( empty( $template_id ) ) {
			return;
		}

		// Disable WooCommerce default rendering
		$this->remove_woocommerce_hooks();

		// Use our custom Elementor render logic
		add_action(
			'wp_body_open',
			function () use ( $template_id ) {
				$this->render_fullwidth_template( $template_id );
			},
			20
		);
	}

	/**
	 * Removes WooCommerce default shop page actions and markup.
	 */
	private function remove_woocommerce_hooks() {
		// Remove layout and wrapper
		remove_all_actions( 'woocommerce_before_main_content' );
		remove_all_actions( 'woocommerce_after_main_content' );
		remove_all_actions( 'woocommerce_sidebar' );

		// Remove breadcrumb and notices
		remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
		remove_action( 'woocommerce_before_shop_loop', 'wc_print_notices', 10 );

		// Remove archive descriptions and product loops
		remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
		remove_action( 'woocommerce_archive_description', 'woocommerce_product_archive_description', 10 );
		remove_all_actions( 'woocommerce_before_shop_loop' );
		remove_all_actions( 'woocommerce_after_shop_loop' );
	}

	/**
	 * Renders Elementor shop template in full-width mode.
	 */
	private function render_fullwidth_template( $template_id ) {
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return;
		}

		$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();
		}

		// Load Elementor assets
		\Elementor\Plugin::$instance->frontend->enqueue_styles();
		\Elementor\Plugin::$instance->frontend->enqueue_scripts();

		echo '<div class="sifency-elementor-shop-fullwidth">';
		Sifency_Addons_Frontend_Helper::render_elementor_content( $template_id, true );
		echo '</div>';

		get_footer();
		exit;
	}

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

// Initialize the renderer
Sifency_Shop_Page_Render::get_instance();
