<?php
/**
 * Register and handle the section template shortcode
 * Handles both [section_template] and [sifency_template] shortcodes
 */
function sifency_addons_section_template_shortcode( $atts ) {
    // Parse shortcode attributes
    $atts = shortcode_atts( 
        array(
            'id' => 0,
        ),
        $atts,
        'section_template'
    );

    $template_id = (int) $atts['id'];

    if ( ! $template_id ) {
        return '<!-- Sifency: No template ID provided -->';
    }

    $post = get_post( $template_id );

    // Verify post exists and is the correct type
    if ( ! $post || 'section_templates' !== $post->post_type ) {
        return '<!-- Sifency: Template not found or incorrect post type -->';
    }

    // Only show published templates on the frontend
    // We allow 'private' or 'draft' if the user is logged in and can edit, 
    // otherwise the preview won't work while you are building the template.
    if ( ! is_admin() && 'publish' !== $post->post_status && ! current_user_can( 'edit_posts' ) ) {
        return '<!-- Sifency: Template is not published -->';
    }

    // Prevent infinite loops with a temporary flag
    static $rendering = array();
    if ( isset( $rendering[ $template_id ] ) ) {
        return '<!-- Sifency: Recursive template detected -->';
    }
    $rendering[ $template_id ] = true;

    $output = '';

    try {
        // Check if Elementor is active
        if ( class_exists( '\Elementor\Plugin' ) ) {
            $elementor_instance = \Elementor\Plugin::instance();

            // Check if the template was actually built with Elementor
            $is_elementor_template = false;
            if ( method_exists( $elementor_instance->db, 'is_built_with_elementor' ) ) {
                $is_elementor_template = $elementor_instance->db->is_built_with_elementor( $template_id );
            } else {
                // Fallback check - look for Elementor metadata
                $is_elementor_template = ! empty( get_post_meta( $template_id, '_elementor_data', true ) );
            }

            if ( $is_elementor_template ) {
                // Render the Elementor content
                if ( method_exists( $elementor_instance->frontend, 'get_builder_content_for_display' ) ) {
                    $output = $elementor_instance->frontend->get_builder_content_for_display( $template_id, true );
                } else {
                    // Alternative method if get_builder_content_for_display is not available
                    $output = $post->post_content;
                }
            } else {
                // Template not built with Elementor, return raw content
                $output = do_shortcode( wpautop( $post->post_content ) );
            }
        } else {
            // Elementor not active, return raw content
            $output = do_shortcode( wpautop( $post->post_content ) );
        }
    } catch ( Exception $e ) {
        // In case of any error, fallback to raw content
        error_log( 'Sifency Template Shortcode Error: ' . $e->getMessage() );
        $output = do_shortcode( wpautop( $post->post_content ) );
    }

    // Clean up the recursion flag
    unset( $rendering[ $template_id ] );

    return $output;
}

// Register both shortcode names for compatibility
add_shortcode( 'section_template', 'sifency_addons_section_template_shortcode' );
add_shortcode( 'sifency_template', 'sifency_addons_section_template_shortcode' );