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

/**
 * Base Class for Archive Template Rendering
 *
 * This abstract class provides a foundation for replacing default archive templates
 * with custom Elementor-powered templates. Child classes only need to define configuration properties.
 *
 * @package Sifency_Addons
 * @subpackage Theme_Builder
 * @abstract
 */
abstract class Sifency_Addons_Archive_Post_Render_Base {

	/**
	 * Post type to filter (e.g., 'service', 'portfolio')
	 *
	 * @var string
	 */
	protected $post_type;

	/**
	 * Path to the canvas template file relative to SIFENCY_TB_PATH
	 *
	 * @var string
	 */
	protected $canvas_path_template;

	/**
	 * Query variable name to store template ID
	 *
	 * @var string
	 */
	protected $query_var_name;

	/**
	 * Post type to lookup in Sifency_Addons_Frontend_Helper::find_matching_template()
	 * Usually post_type + '-archive' (e.g., 'service-archive')
	 *
	 * @var string
	 */
	protected $template_lookup_key;

	/**
	 * Hook priority for template_include filter
	 *
	 * @var int
	 */
	protected $hook_priority = 9999;

	/**
	 * Initialize archive template override
	 */
	public function __construct() {
		if ( is_admin() ) {
			return;
		}
		add_filter( 'template_include', array( $this, 'override_archive_template' ), $this->hook_priority );
	}

	/**
	 * Override archive template with custom Elementor template
	 *
	 * @param string $template Current template file path
	 * @return string Modified or original template path
	 */
	public function override_archive_template( $template ) {
		if ( ! $this->is_archive_page() ) {
			return $template;
		}

		$this->remove_conflicting_filters();

		$canvas_path = $this->get_canvas_path();
		if ( ! file_exists( $canvas_path ) ) {
			return $template;
		}

		$template_id = $this->get_template_id();
		set_query_var( $this->query_var_name, $template_id );

		return $canvas_path;
	}

	/**
	 * Check if current page is the target archive page
	 * Override this method for custom archive checks
	 *
	 * @return bool
	 */
	protected function is_archive_page() {
		return is_post_type_archive( $this->post_type );
	}

	/**
	 * Get the canvas path for the template
	 *
	 * @return string Full path to canvas file
	 */
	protected function get_canvas_path() {
		if ( defined( 'SIFENCY_TB_PATH' ) ) {
			return trailingslashit( SIFENCY_TB_PATH ) . $this->canvas_path_template;
		}

		return plugin_dir_path( __FILE__ ) . 'templates/' . basename( $this->canvas_path_template );
	}

	/**
	 * Get the custom Elementor template ID
	 *
	 * @return int|0 Template ID or 0 if not found
	 */
	protected function get_template_id() {
		if ( ! class_exists( 'Sifency_Addons_Frontend_Helper' )
			|| ! method_exists( 'Sifency_Addons_Frontend_Helper', 'find_matching_template' ) ) {
			return 0;
		}

		return Sifency_Addons_Frontend_Helper::find_matching_template( $this->template_lookup_key );
	}

	/**
	 * Remove conflicting filters from theme builder master
	 */
	protected function remove_conflicting_filters() {
		if ( ! isset( Sifency_Addons_Theme_Builder_Master::$sifency_addons_frontend_render ) ) {
			return;
		}

		remove_filter(
			'template_include',
			array( Sifency_Addons_Theme_Builder_Master::$sifency_addons_frontend_render, 'load_canvas_template' ),
			999
		);
	}
}
