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

/**
 * Sifency Service Archive Render
 *
 * Dynamically overrides the default archive template
 * with an Elementor-powered custom layout.
 */
final class Sifency_Service_Archive_Render {

    public function __construct() {
        if ( is_admin() ) {
            return;
        }
        add_filter( 'template_include', [ $this, 'override_service_archive_template' ], 9999 );
    }
    public function override_service_archive_template( $template ) {
        if ( ! is_post_type_archive( 'service' ) ) {
            return $template;
        }
        if ( isset( Sifency_Addons_Theme_Builder_Master::$sifency_addons_frontend_render ) ) {
            remove_filter(
                'template_include',
                [ Sifency_Addons_Theme_Builder_Master::$sifency_addons_frontend_render, 'load_canvas_template' ],
                999
            );
        }
        $canvas_path = SIFENCY_TB_PATH . 'post-type/service/templates/sifency-service-archive-canvas.php';
        if ( ! file_exists( $canvas_path ) ) {
            return $template;
        }
        $sifency_addons_template_id = 0;
        if ( class_exists( 'Sifency_Frontend_Helper' ) && method_exists( 'Sifency_Frontend_Helper', 'find_matching_template' ) ) {
            $sifency_addons_template_id = Sifency_Frontend_Helper::find_matching_template( 'service-archive' );
        }
        set_query_var( 'sifency_service_archive_template_id', $sifency_addons_template_id );
        return $canvas_path;
    }
}
new Sifency_Service_Archive_Render();
