<?php
namespace Softech_Addons_for_Elementor\Widgets;

if (!defined('ABSPATH'))
    exit;
    
if ( ! class_exists( '\\Softech_Addons_for_Elementor\\Includes\\Softech_Post_Query_Manager' ) ) {
    require_once SITS_ELEMENTOR_ADDONS_PLUGIN_PATH . 'includes/class-post-query-manager.php';
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Utils;
use Softech_Addons_for_Elementor\Includes\Softech_Post_Query_Manager;

class sitsel_testimonial extends Widget_Base
{
    /**
     * Cache group for testimonials
     */
    private const CACHE_GROUP = 'sitsel_testimonials';

    /**
     * Track editor hook registration.
     */
    private static $editor_hooks_registered = false;
    
    /**
     * Cache expiration time (1 hour)
     */
    private const CACHE_EXPIRATION = 3600;

    public function get_name()
    {
        return 'sitsel_testimonial';
    }

    public function get_title()
    {
        return __('SITS Testimonial', 'softech-addons-for-elementor');
    }

    public function get_icon()
    {
        return 'eicon-testimonial';
    }

    public function get_categories()
    {
        return ['sits-category'];
    }

    public function __construct($data = [], $args = null) {
        parent::__construct($data, $args);
        $this->register_widget_assets();
        $this->register_editor_hooks();
        
        // Clear cache when posts are updated
        add_action('save_post', [$this, 'clear_testimonial_cache']);
        add_action('delete_post', [$this, 'clear_testimonial_cache']);
        add_action('trash_post', [$this, 'clear_testimonial_cache']);
    }
    
    /**
     * Clear testimonial cache when content is updated
     * 
     * @param int $post_id Post ID
     */
    public function clear_testimonial_cache($post_id) {
        $post_type = get_post_type($post_id);
        
        // Clear both object cache and transients
        wp_cache_delete('sitsel_testimonials_' . $post_type, self::CACHE_GROUP);
        wp_cache_delete('sitsel_elementor_templates', self::CACHE_GROUP);
        
        // Also clear query-specific caches
        $pattern = 'sitsel_testimonials_' . $post_type . '_*';
        wp_cache_flush_group(self::CACHE_GROUP);
    }

    private function register_editor_hooks()
    {
        if ( self::$editor_hooks_registered ) {
            return;
        }

        add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_editor_script' ] );
        self::$editor_hooks_registered = true;
    }

    public function enqueue_editor_script()
    {
        $this->register_widget_assets();
        wp_enqueue_script( 'sitsel-testimonial-editor-js' );
    }

   public function _register_controls()
{
    // ── Source Type ───────────────────────────────────────────────────
    $this->start_controls_section('content_section', [
        'label' => esc_html__('Testimonials Content', 'softech-addons-for-elementor'),
    ]);

    $this->add_control('content_source_type', [
        'label'   => esc_html__('Content Source', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::SELECT,
        'default' => 'manual',
        'options' => [
            'manual'  => esc_html__('Manual Input', 'softech-addons-for-elementor'),
            'dynamic' => esc_html__('Dynamic', 'softech-addons-for-elementor'),
        ],
    ]);

    $this->end_controls_section(); // close content_section

    // ── Query Section (Query Manager) — only when source = dynamic ────

Softech_Post_Query_Manager::register_controls( $this, [
    'source_control' => 'content_source_type',
    'section_id'     => 'section_testimonial_query',
    'section_label'  => esc_html__( 'Query', 'softech-addons-for-elementor' ),
    'mode'           => 'post_only',  // ← ADD THIS
] );



    // ── Dynamic Display Options ───────────────────────────────────────
    $this->start_controls_section('dynamic_display_section', [
        'label'     => esc_html__('Dynamic Display', 'softech-addons-for-elementor'),
        'condition' => ['content_source_type' => 'dynamic'],
    ]);

    $this->add_control('template_id', [
        'label'       => esc_html__('Select Layout Template', 'softech-addons-for-elementor'),
        'type'        => Controls_Manager::SELECT2,
        'label_block' => true,
        'options'     => $this->sitsel_get_elementor_templates_without_default_kit(),
        'default'     => '',
    ]);

    $this->add_control('edit_template_button', [
        'type'            => Controls_Manager::RAW_HTML,
        'raw'             => '<a href="#" id="sitsel-edit-template-button" class="elementor-button elementor-button-success" target="_blank" style="display:none;">'
                           . esc_html__('Edit Template', 'softech-addons-for-elementor') . '</a>',
        'content_classes' => 'sitsel-edit-template-button-wrap',
    ]);

    $this->add_control('image_size', [
        'label' => esc_html__('Image Resolution', 'softech-addons-for-elementor'),
        'type' => Controls_Manager::SELECT,
        'options' => [
            'thumbnail' => 'Thumbnail',
            'medium' => 'Medium',
            'large' => 'Large',
            'full' => 'Full',
        ],
        'default' => 'full',
    ]);

    $this->end_controls_section();

    // ── Manual Content ────────────────────────────────────────────────
    $this->start_controls_section('manual_content_section', [
        'label'     => esc_html__('Manual Testimonials', 'softech-addons-for-elementor'),
        'condition' => ['content_source_type' => 'manual'],
    ]);

    $repeater = new Repeater();

    $repeater->add_control('show_media', [
        'label'   => esc_html__('Show Media', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::SELECT,
        'default' => 'none',
        'options' => [
            'image' => esc_html__('Image', 'softech-addons-for-elementor'),
            'video' => esc_html__('Video', 'softech-addons-for-elementor'),
            'none'  => esc_html__('None', 'softech-addons-for-elementor'),
        ],
    ]);

    $repeater->add_control('company_logo', [
        'label'     => esc_html__('Image', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::MEDIA,
        'dynamic'   => ['active' => true],
        'default'   => ['url' => Utils::get_placeholder_image_src()],
        'condition' => ['show_media' => 'image'],
    ]);

    $repeater->add_control('company_video', [
        'label'       => esc_html__('Video', 'softech-addons-for-elementor'),
        'type'        => Controls_Manager::MEDIA,
        'media_types' => ['video'],
        'dynamic'     => ['active' => true],
        'condition'   => ['show_media' => 'video'],
    ]);

    $repeater->add_control('author', [
        'label'   => esc_html__('Author', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::TEXT,
        'dynamic' => ['active' => true],
        'default' => __('John Doe', 'softech-addons-for-elementor'),
    ]);

    $repeater->add_control('job', [
        'label'   => esc_html__('Job', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::TEXT,
        'dynamic' => ['active' => true],
        'default' => __('Sony CEO', 'softech-addons-for-elementor'),
    ]);

    $repeater->add_control('author_image', [
        'label'   => esc_html__('Author Image', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::MEDIA,
        'dynamic' => ['active' => true],
        'default' => ['url' => Utils::get_placeholder_image_src()],
    ]);

    $repeater->add_control('rating', [
        'label'   => esc_html__('Rating', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::NUMBER,
        'default' => 4.5,
        'min'     => 0,
        'max'     => 5,
        'step'    => 0.1,
    ]);

    $repeater->add_control('title', [
        'label'   => esc_html__('Title', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::TEXT,
        'default' => __('Awesome Theme', 'softech-addons-for-elementor'),
    ]);

    $repeater->add_control('content', [
        'label'   => esc_html__('Description', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::WYSIWYG,
        'dynamic' => ['active' => true],
        'default' => __('Lorem ipsum dolor sit amet...', 'softech-addons-for-elementor'),
    ]);

    $this->add_control('testimonials', [
        'label'       => esc_html__('Testimonials', 'softech-addons-for-elementor'),
        'type'        => Controls_Manager::REPEATER,
        'fields'      => $repeater->get_controls(),
        'title_field' => '{{{ author }}}',
        'default'     => [
            [
                'show_media'   => 'none',
                'author'       => 'Alice Smith',
                'job'          => 'Marketing Manager',
                'content'      => 'Amazing experience and support.',
                'author_image' => ['url' => Utils::get_placeholder_image_src()],
                'company_logo' => ['url' => Utils::get_placeholder_image_src()],
            ],
            [
                'show_media'   => 'none',
                'author'       => 'Bob Johnson',
                'job'          => 'Product Designer',
                'content'      => 'Helped us scale quickly and easily.',
                'author_image' => ['url' => Utils::get_placeholder_image_src()],
                'company_logo' => ['url' => Utils::get_placeholder_image_src()],
            ],
            [
                'show_media'   => 'none',
                'author'       => 'Carol White',
                'job'          => 'Developer Advocate',
                'content'      => 'The best platform I have used in years.',
                'author_image' => ['url' => Utils::get_placeholder_image_src()],
                'company_logo' => ['url' => Utils::get_placeholder_image_src()],
            ],
        ],
    ]);

    $this->end_controls_section();

    // ── Swiper + Style sections unchanged ────────────────────────────
    $this->start_controls_section('layout_section', [
        'label' => esc_html__('Settings', 'softech-addons-for-elementor'),
        'tab' => Controls_Manager::TAB_CONTENT,
    ]);

    $this->add_control('layout_type', [
        'label'   => esc_html__('Layout Type', 'softech-addons-for-elementor'),
        'type'    => Controls_Manager::SELECT,
        'default' => 'slider',
        'options' => [
            'grid'    => esc_html__('Grid', 'softech-addons-for-elementor'),
            'masonry' => esc_html__('Masonry', 'softech-addons-for-elementor'),
            'slider'  => esc_html__('Slider', 'softech-addons-for-elementor'),
        ],
    ]);

  $this->add_responsive_control('columns', [
    'label' => esc_html__('Columns', 'softech-addons-for-elementor'),
    'type' => Controls_Manager::SELECT,
    'options' => [
        '1' => '1',
        '2' => '2',
        '3' => '3',
        '4' => '4',
        '5' => '5',
        '6' => '6',
    ],
    'default' => '3',
    'tablet_default' => '2',
    'mobile_default' => '1',
    'render_type' => 'template',
    'selectors' => [
        '{{WRAPPER}} .sitsel-testimonial-slider'                                        => '--sitsel-testimonial-columns: {{VALUE}};',
        '{{WRAPPER}} .sitsel-testimonial-layout-grid .sitsel-testimonial-list'          => 'display: grid !important; grid-template-columns: repeat({{VALUE}}, minmax(0, 1fr)) !important;',
        '{{WRAPPER}} .sitsel-testimonial-layout-masonry .sitsel-testimonial-list'       => 'column-count: {{VALUE}};',
    ],
]);

    $this->add_responsive_control('item_gap', [
    'label' => esc_html__('Item Gap', 'softech-addons-for-elementor'),
    'type' => Controls_Manager::SLIDER,
    'size_units' => ['px', 'em', 'rem'],
    'range' => [
        'px'  => ['min' => 0, 'max' => 100],
        'em'  => ['min' => 0, 'max' => 10, 'step' => 0.1],
        'rem' => ['min' => 0, 'max' => 10, 'step' => 0.1],
    ],
    'default' => ['size' => 20, 'unit' => 'px'],
    'condition' => ['layout_type!' => 'slider'],
    'selectors' => [
        '{{WRAPPER}} .sitsel-testimonial-layout-grid .sitsel-testimonial-list'    => 'gap: {{SIZE}}{{UNIT}};',
        '{{WRAPPER}} .sitsel-testimonial-layout-masonry .sitsel-testimonial-list' => 'column-gap: {{SIZE}}{{UNIT}};',
        '{{WRAPPER}} .sitsel-testimonial-layout-masonry .sitsel-testimonial-item' => 'margin-bottom: {{SIZE}}{{UNIT}};',
    ],
]);

    $this->add_control('enable_pagination', [
        'label' => esc_html__('Enable Pagination', 'softech-addons-for-elementor'),
        'type' => Controls_Manager::SWITCHER,
        'default' => 'yes',
        'condition' => ['layout_type!' => 'slider'],
    ]);

    $this->add_control('items_per_page', [
        'label' => esc_html__('Testimonials Per Page', 'softech-addons-for-elementor'),
        'type' => Controls_Manager::NUMBER,
        'default' => 6,
        'min' => 1,
        'condition' => [
            'layout_type!' => 'slider',
            'enable_pagination' => 'yes',
        ],
    ]);

    $this->add_control('pagination_type', [
    'label' => esc_html__('Pagination Type', 'softech-addons-for-elementor'),
    'type' => Controls_Manager::SELECT,
    'default' => 'numbers',
    'options' => [
        'numbers'    => esc_html__('Number Pagination', 'softech-addons-for-elementor'),
        'prev_next'  => esc_html__('Prev / Next', 'softech-addons-for-elementor'),
        'none'       => esc_html__('None', 'softech-addons-for-elementor'),
    ],
    'condition' => [
        'layout_type!' => 'slider',
        'enable_pagination' => 'yes',
    ],
]);

$this->add_control('pagination_alignment', [
    'label'   => esc_html__('Pagination Alignment', 'softech-addons-for-elementor'),
    'type'    => Controls_Manager::CHOOSE,
    'options' => [
        'flex-start' => [
            'title' => esc_html__('Left', 'softech-addons-for-elementor'),
            'icon'  => 'eicon-text-align-left',
        ],
        'center' => [
            'title' => esc_html__('Center', 'softech-addons-for-elementor'),
            'icon'  => 'eicon-text-align-center',
        ],
        'flex-end' => [
            'title' => esc_html__('Right', 'softech-addons-for-elementor'),
            'icon'  => 'eicon-text-align-right',
        ],
    ],
    'default'   => 'center',
    'condition' => [
        'layout_type!'      => 'slider',
        'enable_pagination' => 'yes',
    ],
    'selectors' => [
        '{{WRAPPER}} .sitsel-testimonial-pagination-inner' => 'display: flex; justify-content: {{VALUE}};',
    ],
]);

    
    $this->add_control('quote_icon', [
        'label'     => __('Select Quote Icon', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::ICONS,
        'default'   => ['value' => 'fas fa-quote-left', 'library' => 'fa-solid'],
        'condition' => ['content_source_type' => 'manual'],
    ]);

    $this->add_control('rating', [
        'label'     => esc_html__('Rating', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::SWITCHER,
        'default'   => 'yes',
        'condition' => ['content_source_type' => 'manual'],
    ]);

    $this->add_control('rating_scale', [
        'label'     => esc_html__('Rating Scale', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::SELECT,
        'options'   => ['5' => '5 Stars', '10' => '10 Stars'],
        'default'   => '5',
        'condition' => ['content_source_type' => 'manual', 'rating' => 'yes'],
    ]);

    $this->end_controls_section();

    $this->start_controls_section('swiper_settings', [
        'label' => esc_html__('Slider Settings', 'softech-addons-for-elementor'),
        'condition' => ['layout_type' => 'slider'],
    ]);

        $this->add_control('autoplay', [
            'label' => esc_html__('Autoplay', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'false',
        ]);

        $this->add_control('speed', [
            'label' => esc_html__('Autoplay Speed (ms)', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::NUMBER,
            'default' => 3000,
            'condition' => ['autoplay' => 'yes'],
        ]);

        $this->add_control('slides_to_scroll', [
            'label' => esc_html__('Slides to Scroll', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::NUMBER,
            'default' => 1,
            'min' => 1,
        ]);

        $this->add_control('gutter', [
            'label' => esc_html__('Gutter', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SLIDER,
            'range' => [
                'px' => ['min' => 0, 'max' => 100],
            ],
            'default' => ['size' => 15],
        ]);

        $this->add_control('navigation', [
            'label' => esc_html__('Navigation', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control(
            'nav_prev_icon',
            [
                'label' => esc_html__('Previous Arrow Icon', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-angle-left',
                    'library' => 'fa-solid',
                ],
                'condition' => [
                    'navigation' => 'yes',
                ],
            ]
        );
        
        $this->add_control(
            'nav_next_icon',
            [
                'label' => esc_html__('Next Arrow Icon', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fas fa-angle-right',
                    'library' => 'fa-solid',
                ],
                'condition' => [
                    'navigation' => 'yes',
                ],
            ]
        );

        $this->add_control('pagination', [
            'label' => esc_html__('Pagination', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'no',
        ]);

        $this->add_control(
            'controls_position',
            [
                'label' => esc_html__('Controls Position', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'inside',
                'options' => [
                    'inside'  => esc_html__('Inside Slider', 'softech-addons-for-elementor'),
                    'outside' => esc_html__('Outside Slider', 'softech-addons-for-elementor'),
                ],
                'condition' => [
                    'pagination' => 'yes',
                ],
            ]
        );

        $this->add_control('loop', [
            'label' => esc_html__('Infinite Loop', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control('effect', [
            'label' => esc_html__('Effect', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SELECT,
            'options' => [
                'slide' => 'Slide',
                'fade' => 'Fade',
            ],
            'default' => 'slide',
        ]);

        $this->add_control('transition_speed', [
            'label' => esc_html__('Transition Duration (sec)', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::NUMBER,
            'default' => 0.7,
            'step' => 0.1,
        ]);

    $this->end_controls_section();

    $this->register_style_controls();
    }

    /**
     * Register all style controls
     */
    private function register_style_controls()
    {
        // Testimonial Box Style
        $this->start_controls_section('style_testimonial', [
            'label' => esc_html__('Testimonial Box', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('testimonial_bg', [
            'label' => esc_html__('Background Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-box' => 'background-color: {{VALUE}};',
            ],
            'default' => "#fff",
        ]);

        $this->add_group_control(\Elementor\Group_Control_Box_Shadow::get_type(), [
            'name' => 'testimonial_box_shadow',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-box',
        ]);

        $this->add_group_control(\Elementor\Group_Control_Border::get_type(), [
            'name' => 'testimonial_border',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-box',
        ]);
		
		$this->add_responsive_control('testimonial_padding', [
            'label' => esc_html__('Padding', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::DIMENSIONS,
			 'size_units' => ['px', '%', 'em', 'rem'],
            'selectors' => [
					'{{WRAPPER}} .sitsel-testimonial-box' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]);


        $this->end_controls_section();

        // Author Name Style
        $this->start_controls_section('style_author', [
            'label' => esc_html__('Author Name', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('author_color', [
            'label' => esc_html__('Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-author' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'author_typography',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-author',
        ]);

        $this->end_controls_section();

        // Job Title Style
        $this->start_controls_section('style_job', [
            'label' => esc_html__('Job Title', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('job_color', [
            'label' => esc_html__('Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-job' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'job_typography',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-job',
        ]);

        $this->end_controls_section();

        // Testimonial Title Style
        $this->start_controls_section('style_title', [
            'label' => esc_html__('Testimonial Title', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('title_color', [
            'label' => esc_html__('Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-title' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'title_typography',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-title',
        ]);

        $this->end_controls_section();

        // Content Style
        $this->start_controls_section('style_content', [
            'label' => esc_html__('Content', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('content_color', [
            'label' => esc_html__('Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-content' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_group_control(\Elementor\Group_Control_Typography::get_type(), [
            'name' => 'content_typography',
            'selector' => '{{WRAPPER}} .sitsel-testimonial-content',
        ]);

        $this->end_controls_section();

        // Author Image Style
        $this->start_controls_section('style_author_img', [
            'label' => esc_html__('Author Image', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control('author_img_size', [
            'label' => esc_html__('Size (px)', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 20, 'max' => 150]],
            'selectors' => [
                '{{WRAPPER}} .sitsel-author-img' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}}; object-fit: cover; border-radius: 50%;',
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('style_testimonial_media_image', [
            'label' => esc_html__('Image Media', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control('company_logo_width', [
            'label'     => esc_html__('Image Width', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::SLIDER,
            'size_units' => ['px', '%'],
            'range'     => [
                'px' => ['min' => 20, 'max' => 500],
                '%'  => ['min' => 5, 'max' => 100],
            ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-media-image' => 'width: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_responsive_control('company_logo_radius', [
            'label'     => esc_html__('Image Border Radius', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::DIMENSIONS,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-media-image' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;',
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('style_testimonial_media_video', [
            'label' => esc_html__('Video Media', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_responsive_control('company_video_width', [
            'label'     => esc_html__('Video Width', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::SLIDER,
            'size_units' => ['px', '%'],
            'range'     => [
                'px' => ['min' => 100, 'max' => 1200],
                '%'  => ['min' => 20, 'max' => 100],
            ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-media-video' => 'width: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->add_control('company_video_ratio', [
            'label'     => esc_html__('Video Ratio', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::SELECT,
            'default'   => '16 / 9',
            'options'   => [
                '1 / 1'   => esc_html__('1:1', 'softech-addons-for-elementor'),
                '4 / 3'   => esc_html__('4:3', 'softech-addons-for-elementor'),
                '16 / 9'  => esc_html__('16:9', 'softech-addons-for-elementor'),
                '21 / 9'  => esc_html__('21:9', 'softech-addons-for-elementor'),
            ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-media-video' => 'aspect-ratio: {{VALUE}};',
            ],
        ]);

        $this->add_responsive_control('company_video_radius', [
            'label'     => esc_html__('Video Border Radius', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::DIMENSIONS,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-media-video' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;',
            ],
        ]);

        $this->end_controls_section();

        // Quote Icon Style
        $this->start_controls_section('style_quote_icon', [
            'label' => esc_html__('Quote Icon', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('quote_icon_color', [
            'label' => esc_html__('Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-quote-icon path' => 'color: {{VALUE}}; fill: {{VALUE}};',
                '{{WRAPPER}} .sitsel-quote-icon svg path' => 'fill: {{VALUE}};',
            ],
        ]);

        $this->add_responsive_control('quote_icon_size', [
            'label' => esc_html__('Size', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SLIDER,
            'range' => [
                'px' => [
                    'min' => 10,
                    'max' => 150,
                ],
            ],
            'default' => [
                'size' => 20,
                'unit' => 'px',
            ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-quote-icon' => 'font-size: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-quote-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        // Star Rating Style
        $this->start_controls_section('style_rating', [
            'label' => esc_html__('Rating Stars', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
        ]);

        $this->add_control('star_color', [
            'label' => esc_html__('Star Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-star.filled' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('star_empty_color', [
            'label' => esc_html__('Empty Star Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-star' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_responsive_control('star_size', [
            'label' => esc_html__('Size', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SLIDER,
            'range' => ['px' => ['min' => 10, 'max' => 50]],
            'selectors' => [
                '{{WRAPPER}} .sitsel-star' => 'font-size: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_section();

        $this->start_controls_section('style_grid_pagination', [
            'label' => esc_html__('Grid / Masonry Pagination', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition' => [
                'layout_type!' => 'slider',
                'enable_pagination' => 'yes',
            ],
        ]);

        $this->add_responsive_control('grid_pagination_gap', [
            'label'      => esc_html__('Gap', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em', 'rem'],
            'range'      => [
                'px'  => ['min' => 0, 'max' => 60],
                'em'  => ['min' => 0, 'max' => 5, 'step' => 0.1],
                'rem' => ['min' => 0, 'max' => 5, 'step' => 0.1],
            ],
            'default' => ['size' => 10, 'unit' => 'px'],
            'selectors' => [
        '{{WRAPPER}} .sitsel-testimonial-pagination-inner' => 'display: flex; gap: {{SIZE}}{{UNIT}};',
    ],
        ]);

        $this->add_responsive_control('pagination_button_padding', [
            'label' => esc_html__('Button Padding', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::DIMENSIONS,
            'size_units' => ['px'],
            'default' => [
                'top' => 8,
                'right' => 14,
                'bottom' => 8,
                'left' => 14,
                'unit' => 'px',
                'isLinked' => false,
            ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);

        $this->start_controls_tabs('grid_pagination_tabs');

        $this->start_controls_tab('grid_pagination_normal_tab', [
            'label' => esc_html__('Normal', 'softech-addons-for-elementor'),
        ]);

        $this->add_control('pagination_text_color', [
            'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_bg_color', [
            'label' => esc_html__('Background Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_border_color', [
            'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn' => 'border-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_border_radius', [
            'label' => esc_html__('Border Radius', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => ['px' => ['min' => 0, 'max' => 40]],
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn' => 'border-radius: {{SIZE}}{{UNIT}};',
            ],
        ]);

        $this->end_controls_tab();

        $this->start_controls_tab('grid_pagination_hover_tab', [
            'label' => esc_html__('Hover', 'softech-addons-for-elementor'),
        ]);

        $this->add_control('pagination_hover_text_color', [
            'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn:hover:not(:disabled), {{WRAPPER}} .sitsel-testimonial-page-btn:focus-visible:not(:disabled)' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_hover_bg_color', [
            'label' => esc_html__('Background Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn:hover:not(:disabled), {{WRAPPER}} .sitsel-testimonial-page-btn:focus-visible:not(:disabled)' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_hover_border_color', [
            'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn:hover:not(:disabled), {{WRAPPER}} .sitsel-testimonial-page-btn:focus-visible:not(:disabled)' => 'border-color: {{VALUE}};',
            ],
        ]);

        $this->end_controls_tab();

        $this->start_controls_tab('grid_pagination_active_tab', [
            'label' => esc_html__('Active', 'softech-addons-for-elementor'),
        ]);

        $this->add_control('pagination_active_text_color', [
            'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-active' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_active_bg_color', [
            'label' => esc_html__('Background Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-active' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_active_border_color', [
            'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-active' => 'border-color: {{VALUE}};',
            ],
        ]);

        $this->end_controls_tab();

        $this->start_controls_tab('grid_pagination_disabled_tab', [
            'label' => esc_html__('Disabled', 'softech-addons-for-elementor'),
        ]);

        $this->add_control('pagination_disabled_text_color', [
            'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-disabled, {{WRAPPER}} .sitsel-testimonial-page-btn:disabled' => 'color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_disabled_bg_color', [
            'label' => esc_html__('Background Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-disabled, {{WRAPPER}} .sitsel-testimonial-page-btn:disabled' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->add_control('pagination_disabled_border_color', [
            'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-testimonial-page-btn.is-disabled, {{WRAPPER}} .sitsel-testimonial-page-btn:disabled' => 'border-color: {{VALUE}};',
            ],
        ]);

        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();

        // Pagination Style
        $this->start_controls_section('style_pagination', [
            'label' => esc_html__('Pagination Dots', 'softech-addons-for-elementor'),
            'tab' => Controls_Manager::TAB_STYLE,
            'condition' => ['layout_type' => 'slider'],
        ]);

        $this->add_control('pagination_color', [
            'label' => esc_html__('Dot Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-pagination-bullet' => 'background-color: {{VALUE}};',
            ],
        ]);

       $this->add_control('pagination_active_color', [
            'label' => esc_html__('Active Dot Color', 'softech-addons-for-elementor'),
            'type' => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-pagination-bullet-active' => 'background-color: {{VALUE}};',
            ],
        ]);

        $this->end_controls_section();

        // Navigation Style
        $this->start_controls_section('style_navigation', [
            'label'     => esc_html__('Navigation Arrows', 'softech-addons-for-elementor'),
            'tab'       => Controls_Manager::TAB_STYLE,
            'condition' => ['layout_type' => 'slider'],
        ]);
        
        // — Shared layout controls (not state-specific) ——————————————
        $this->add_responsive_control('nav_icon_size', [
            'label'      => esc_html__('Arrow Icon Size', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px', 'em', 'rem'],
            'range'      => ['px' => ['min' => 10, 'max' => 100]],
            'default'    => ['size' => 30, 'unit' => 'px'],
            'selectors'  => [
                '{{WRAPPER}} .swiper-button-prev svg,
                 {{WRAPPER}} .swiper-button-next svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);
        
        $this->add_responsive_control('nav_arrow_position', [
            'label'     => esc_html__('Vertical Position (Top)', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::SLIDER,
            'range'     => ['%' => ['min' => 0, 'max' => 100]],
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next,
                 {{WRAPPER}} .swiper-button-prev' => 'top: {{SIZE}}%;',
            ],
        ]);
        
        // — State tabs ————————————————————————————————————————————————
        $this->start_controls_tabs('nav_arrow_tabs');
        
        // ── Normal ──
        $this->start_controls_tab('nav_arrow_normal_tab', [
            'label' => esc_html__('Normal', 'softech-addons-for-elementor'),
        ]);
        
        $this->add_control('nav_arrow_color', [
            'label'     => esc_html__('Icon Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next,
                 {{WRAPPER}} .swiper-button-prev'         => 'color: {{VALUE}};',
                '{{WRAPPER}} .swiper-button-next svg path,
                 {{WRAPPER}} .swiper-button-prev svg path' => 'fill: {{VALUE}};',
            ],
        ]);
        
        $this->add_control('nav_arrow_bg', [
            'label'     => esc_html__('Background', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next,
                 {{WRAPPER}} .swiper-button-prev' => 'background-color: {{VALUE}};',
            ],
        ]);
        
       $this->add_group_control(\Elementor\Group_Control_Border::get_type(), [
    'name'     => 'nav_arrow_border',
    'selector' => '{{WRAPPER}} .swiper-button-next, {{WRAPPER}} .swiper-button-prev',
]);
        
        $this->add_control('nav_arrow_border_radius', [
            'label'      => esc_html__('Border Radius', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px', '%'],
            'range'      => ['px' => ['min' => 0, 'max' => 100], '%' => ['min' => 0, 'max' => 50]],
            'selectors'  => [
                '{{WRAPPER}} .swiper-button-next,
                 {{WRAPPER}} .swiper-button-prev' => 'border-radius: {{SIZE}}{{UNIT}};',
            ],
        ]);
        
        $this->add_responsive_control('nav_arrow_padding', [
            'label'      => esc_html__('Padding', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => ['px'],
            'selectors'  => [
                '{{WRAPPER}} .swiper-button-next,
                 {{WRAPPER}} .swiper-button-prev' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]);
        
        $this->end_controls_tab();
        
        // ── Hover ──
        $this->start_controls_tab('nav_arrow_hover_tab', [
            'label' => esc_html__('Hover', 'softech-addons-for-elementor'),
        ]);
        
        $this->add_control('nav_arrow_hover_color', [
            'label'     => esc_html__('Icon Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next:hover,
                 {{WRAPPER}} .swiper-button-prev:hover'         => 'color: {{VALUE}};',
                '{{WRAPPER}} .swiper-button-next:hover svg path,
                 {{WRAPPER}} .swiper-button-prev:hover svg path' => 'fill: {{VALUE}};',
            ],
        ]);
        
        $this->add_control('nav_arrow_hover_bg', [
            'label'     => esc_html__('Background', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next:hover,
                 {{WRAPPER}} .swiper-button-prev:hover' => 'background-color: {{VALUE}};',
            ],
        ]);
        
        $this->add_group_control(\Elementor\Group_Control_Border::get_type(), [
    'name'     => 'nav_arrow_hover_border',
    'selector' => '{{WRAPPER}} .swiper-button-next:hover, {{WRAPPER}} .swiper-button-prev:hover',
]);
        
        $this->end_controls_tab();
        
        // ── Active ──
        $this->start_controls_tab('nav_arrow_active_tab', [
            'label' => esc_html__('Active', 'softech-addons-for-elementor'),
        ]);
        
        $this->add_control('nav_arrow_active_color', [
            'label'     => esc_html__('Icon Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next.is-active,
                 {{WRAPPER}} .swiper-button-prev.is-active'         => 'color: {{VALUE}};',
                '{{WRAPPER}} .swiper-button-next.is-active svg path,
                 {{WRAPPER}} .swiper-button-prev.is-active svg path' => 'fill: {{VALUE}};',
            ],
        ]);
        
        $this->add_control('nav_arrow_active_bg', [
            'label'     => esc_html__('Background', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .swiper-button-next.is-active,
                 {{WRAPPER}} .swiper-button-prev.is-active' => 'background-color: {{VALUE}};',
            ],
        ]);
        
        $this->add_group_control(\Elementor\Group_Control_Border::get_type(), [
    'name'     => 'nav_arrow_active_border',
    'selector' => '{{WRAPPER}} .swiper-button-next.is-active, {{WRAPPER}} .swiper-button-prev.is-active',
]);
        
        $this->end_controls_tab();
        $this->end_controls_tabs();
        
        $this->end_controls_section();
    }

    /**
     * Register widget assets with proper versioning
     */
    private function register_widget_assets()
    {
        $plugin_url = SITS_ELEMENTOR_ADDONS_PLUGIN_URL;
        $version = SITS_ELEMENTOR_ADDONS_VERSION;

        // Register common CSS
        if (!wp_style_is('sitsel-common-css', 'registered')) {
            wp_register_style(
                'sitsel-common-css',
                $plugin_url . 'assets/css/sitsel-comman.css',
                [],
                $version
            );
        }

        // Register JS
        if (!wp_script_is('sitsel-testimonial-js', 'registered')) {
            wp_register_script(
                'sitsel-testimonial-js',
                $plugin_url . 'assets/js/sitsel-testimonial.js',
                ['jquery'],
                $version,
                true
            );
        }

        if (!wp_script_is('sitsel-testimonial-editor-js', 'registered')) {
            wp_register_script(
                'sitsel-testimonial-editor-js',
                $plugin_url . 'assets/js/sitsel-testimonial-editor.js',
                ['jquery'],
                $version,
                true
            );
        }
    }

    /**
     * Render the widget output on the frontend
     */
    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $layout_type = $this->get_layout_type( $settings );
        $columns = $this->get_responsive_columns( $settings );
        $enable_pagination = $settings['enable_pagination'] === 'yes';
        $pagination_type = $this->get_pagination_type( $settings );
        $items_per_page = $this->get_items_per_page( $settings );

        $effect = $settings['effect'] ?? 'slide';
        $loop = $settings['loop'] === 'yes';
        $pagination = $settings['pagination'] === 'yes';
        $navigation = $settings['navigation'] === 'yes';
        $transition_speed = floatval($settings['transition_speed'] ?? 0.7) * 1000;
        $content_source_type = $settings['content_source_type'];
        $controls_position = $settings['controls_position'] ?? 'inside';

        if ( 'slider' === $layout_type ) {
            echo '<div class="sitsel-swiper-wrap sitsel-swiper-pending">';
            echo '<div class="swiper sitsel-testimonial-slider sitsel-layout-slider sitsel-testimonial-layout" data-layout-type="slider"'
                . ' data-effect="' . esc_attr($effect) . '"'
                . ' data-loop="' . esc_attr($loop ? 'true' : 'false') . '"'
                . ' data-speed="' . esc_attr($transition_speed) . '"'
                . ' data-controls-position="' . esc_attr($controls_position) . '"'
                . ' data-pagination="' . esc_attr($pagination ? 'true' : 'false') . '"'
                . ' data-navigation="' . esc_attr($navigation ? 'true' : 'false') . '"'
                . ' data-scroll="' . esc_attr($settings['slides_to_scroll'] ?? 1 ) . '"'
                . ' data-gutter="' . esc_attr($settings['gutter']['size'] ?? 15 ) . '"'
                . ' data-columns="' . esc_attr($columns['desktop']) . '"'
                . ' data-columns-tablet="' . esc_attr($columns['tablet']) . '"'
                . ' data-columns-mobile="' . esc_attr($columns['mobile']) . '">';

            echo '<div class="swiper-wrapper">';

            if ( $content_source_type === 'dynamic' ) {
                $this->render_dynamic_testimonials( $settings, $layout_type );
            } else {
                $this->render_manual_testimonials( $settings, $layout_type );
            }

            echo '</div>'; // .swiper-wrapper
            echo '</div>'; // swiper

            $this->render_slider_controls($settings);
            echo '</div>'; // sitsel-swiper-wrap
            return;
        }

        $pending_class = ( $enable_pagination && ! empty( $items_per_page ) ) ? ' sitsel-pagination-pending' : '';
        echo '<div class="sitsel-testimonial-layout sitsel-testimonial-layout-' . esc_attr( $layout_type ) . esc_attr( $pending_class ) . '" data-layout-type="' . esc_attr( $layout_type ) . '" data-enable-pagination="' . esc_attr( $enable_pagination ? 'yes' : 'no' ) . '" data-pagination-type="' . esc_attr( $pagination_type ) . '" data-items-per-page="' . esc_attr( $items_per_page ) . '">';
        echo '<div class="sitsel-testimonial-list">';

        if ( $content_source_type === 'dynamic' ) {
            $this->render_dynamic_testimonials( $settings, $layout_type );
        } else {
            $this->render_manual_testimonials( $settings, $layout_type );
        }

        echo '</div>';
        echo '<div class="sitsel-testimonial-pagination" aria-label="' . esc_attr__( 'Testimonial pagination', 'softech-addons-for-elementor' ) . '"></div>';
        echo '</div>';
    }

    private function get_layout_type( array $settings ): string
    {
        $layout_type = sanitize_key( (string) ( $settings['layout_type'] ?? 'slider' ) );

        return in_array( $layout_type, [ 'grid', 'masonry', 'slider' ], true ) ? $layout_type : 'slider';
    }

    private function get_item_wrapper_class( string $layout_type ): string
    {
        return 'slider' === $layout_type ? 'swiper-slide' : 'sitsel-testimonial-item';
    }

    private function get_item_inner_class( string $layout_type ): string
    {
        return 'slider' === $layout_type ? 'sitsel-slide-inner' : 'sitsel-grid-item-inner';
    }

    private function get_columns_count( array $settings ): int
    {
        $columns = $settings['columns'] ?? 3;

        if ( is_array( $columns ) ) {
            $columns = $columns['size'] ?? $columns['value'] ?? 3;
        }

        $columns = max( 1, (int) $columns );

        return min( 6, $columns );
    }

    private function get_responsive_columns( array $settings ): array
    {
        $desktop = $this->get_columns_count( $settings );
        $tablet = $settings['columns_tablet'] ?? $desktop;
        $mobile = $settings['columns_mobile'] ?? 1;

        if ( is_array( $tablet ) ) {
            $tablet = $tablet['size'] ?? $tablet['value'] ?? $desktop;
        }

        if ( is_array( $mobile ) ) {
            $mobile = $mobile['size'] ?? $mobile['value'] ?? 1;
        }

        return [
            'desktop' => min( 6, max( 1, (int) $desktop ) ),
            'tablet'  => min( 6, max( 1, (int) $tablet ) ),
            'mobile'  => min( 6, max( 1, (int) $mobile ) ),
        ];
    }

    private function get_item_gap( array $settings ): int
    {
        $gap = $settings['item_gap']['size'] ?? 20;

        return max( 0, (int) $gap );
    }

    private function get_items_per_page( array $settings ): int
    {
        $items_per_page = $settings['items_per_page'] ?? 6;

        if ( is_array( $items_per_page ) ) {
            $items_per_page = $items_per_page['size'] ?? $items_per_page['value'] ?? 6;
        }

        return max( 1, (int) $items_per_page );
    }

    private function get_pagination_type( array $settings ): string
    {
        $pagination_type = sanitize_key( (string) ( $settings['pagination_type'] ?? 'numbers' ) );

        return in_array( $pagination_type, [ 'numbers', 'prev_next', 'none' ], true ) ? $pagination_type : 'numbers';
    }

    /**
     * Render dynamic testimonials with optimized caching
     * 
     * @param array $settings Widget settings
     */
   private function render_dynamic_testimonials( array $settings, string $layout_type ) {
    $paged      = Softech_Post_Query_Manager::get_paged();
    $query_args = Softech_Post_Query_Manager::build_query_args( $settings, $paged );
    $query      = new \WP_Query( $query_args );

    if ( ! $query->have_posts() ) {
        echo '<div class="sitsel-not-found">' . esc_html( $settings['not_found_text'] ?? '' ) . '</div>';
        wp_reset_postdata();
        return;
    }

    // Batch load meta for performance
    $post_ids = wp_list_pluck( $query->posts, 'ID' );
    update_meta_cache( 'post', $post_ids );

    while ( $query->have_posts() ) {
        $query->the_post();

        echo '<div class="' . esc_attr( $this->get_item_wrapper_class( $layout_type ) ) . '">';
        echo '<div class="sitsel-testimonial-box ' . esc_attr( $this->get_item_inner_class( $layout_type ) ) . '">';

        if ( ! empty( $settings['template_id'] ) ) {
            $this->render_elementor_template( $settings['template_id'], get_the_ID() );
        } else {
            $this->render_default_dynamic_layout( get_post(), $settings );
        }

        echo '</div>';
        echo '</div>';
    }

    wp_reset_postdata();
}

    /**
     * Render Elementor template with post context
     * 
     * @param int $template_id Template ID
     * @param int $post_id Current post ID
     */
    
    private function render_elementor_template( $template_id, $post_id ) {
        // Elementor returns the full template markup here, including inline styles required for rendering.
        echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor template output is intentionally rendered as HTML.
            absint( $template_id ),
            true
        );
    }

    /**
     * Render default dynamic layout (no template)
     * 
     * @param WP_Post $post Post object
     * @param array $settings Widget settings
     */
    private function render_default_dynamic_layout($post, $settings)
    {
       $thumbnail = get_the_post_thumbnail(
            $post->ID,
            $settings['image_size'] ?? 'full',
            ['class' => 'sitsel-dynamic-testimonial-image']
        );
        
        if ( $thumbnail ) {
            echo wp_kses_post( $thumbnail );
        }

        
        echo '<h4 class="sitsel-testimonial-title">' . esc_html(get_the_title($post->ID)) . '</h4>';
        
        $author = get_post_meta($post->ID, 'author_name', true);
		$job = get_post_meta($post->ID, 'job_title', true);
        
        $excerpt = get_the_excerpt($post->ID);
        if ($excerpt) {
            echo '<div class="sitsel-testimonial-content">' . wp_kses_post(wpautop($excerpt)) . '</div>';
        }
        
        if ($author || $job) {
            echo '<div class="sitsel-testimonial-meta">';
            if ($author) {
                echo '<h4 class="sitsel-testimonial-author">' . esc_html($author) . '</h4>';
            }
            if ($job) {
                echo '<span class="sitsel-testimonial-job">' . esc_html($job) . '</span>';
            }
            echo '</div>';
        }
    }

    /**
     * Render manual testimonials
     * 
     * @param array $settings Widget settings
     */
    private function render_manual_testimonials( array $settings, string $layout_type )
    {
        if (empty($settings['testimonials']) || !is_array($settings['testimonials'])) {
            return;
        }

        $placeholder_img = plugin_dir_url(dirname(__DIR__)) . 'assets/images/placeholder-image.webp';

        foreach ($settings['testimonials'] as $item) {
            $title = $item['title'] ?? '';
            $content = $item['content'] ?? '';
            $author = $item['author'] ?? '';
            $job = $item['job'] ?? '';
            $rating = $item['rating'] ?? '';
            $author_img = $item['author_image']['url'] ?? $placeholder_img;
            $show_media = $item['show_media'] ?? 'image';
            $company_logo = $item['company_logo']['url'] ?? '';
            $company_video = $item['company_video'] ?? [];

            echo '<div class="' . esc_attr( $this->get_item_wrapper_class( $layout_type ) ) . '">';
            echo '<div class="sitsel-testimonial-box ' . esc_attr( $this->get_item_inner_class( $layout_type ) ) . '">';

            // Testimonial media
            if ( 'video' === $show_media && ! empty( $company_video ) ) {
                $this->render_testimonial_video_media( $company_video );
            } elseif ( 'image' === $show_media && $company_logo ) {
                echo '<div class="sitsel-testimonial-media sitsel-testimonial-media-image">';
                echo '<img class="sitsel-company-logo" 
                    src="' . esc_url( $company_logo ) . '" 
                    alt="' . esc_attr__( 'Company Logo', 'softech-addons-for-elementor' ) . '" 
                    loading="lazy" 
                    decoding="async" />';
                echo '</div>';
            }

            // Quote Icon
            if (!empty($settings['quote_icon']['value'])) {
               echo '<span class="sitsel-quote-icon" role="presentation" aria-label="' . 
                     esc_attr__('Testimonial quote', 'softech-addons-for-elementor') . '">';
                \Elementor\Icons_Manager::render_icon($settings['quote_icon'], ['aria-hidden' => 'true']);
               echo '</span>';
            }

            // Title
            if ($title) {
                echo '<div class="sitsel-testimonial-title">' . esc_html($title) . '</div>';
            }

            // Content
            if ($content) {
                echo '<div class="sitsel-testimonial-content">' . wp_kses_post($content) . '</div>';
            }

            // Meta
            echo '<div class="sitsel-testimonial-meta">';
            echo '<div class="sitsel-author-profile">';
            if ( $author_img ) {
                echo '<img class="sitsel-author-img" 
                    src="' . esc_url( $author_img ) . '" 
                    alt="' . esc_attr( $author ) . '" 
                    loading="lazy" 
                    decoding="async" />';
            }
            echo '</div>';

            echo '<div>';
            if ($author) {
                echo '<h4 class="sitsel-testimonial-author">' . esc_html($author) . '</h4>';
            }
            if ($job) {
                echo '<span class="sitsel-testimonial-job">' . esc_html($job) . '</span>';
            }

            // Rating
            if (!empty($settings['rating']) && $settings['rating'] === 'yes' && $rating) {
                $max = (int) ($settings['rating_scale'] ?? 5);
                $score = (float) $rating;
                echo '<div class="sitsel-testimonial-rating">';
                for ($i = 1; $i <= $max; $i++) {
                    echo '<span class="sitsel-star' . ($i <= $score ? ' filled' : '') . '">&#9733;</span>';
                }
                echo '</div>';
            }
            echo '</div>'; // close author/job
            echo '</div>'; // close testimonial-meta

            echo '</div>'; // close testimonial-box
            echo '</div>'; // close swiper-slide
        }
    }

    /**
     * Render a manual testimonial video.
     *
     * @param array $video_media Video media array.
     */
    private function render_testimonial_video_media( array $video_media ): void
    {
        $video_url = '';

        if ( ! empty( $video_media['id'] ) ) {
            $video_url = wp_get_attachment_url( absint( $video_media['id'] ) );
        }

        if ( '' === $video_url && ! empty( $video_media['url'] ) ) {
            $video_url = $video_media['url'];
        }

        if ( '' === $video_url ) {
            return;
        }

        echo '<div class="sitsel-testimonial-media sitsel-testimonial-media-video">';

        $path = (string) wp_parse_url( $video_url, PHP_URL_PATH );
        $extension = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) );

        if ( in_array( $extension, [ 'mp4', 'webm', 'ogg' ], true ) ) {
            echo '<div class="sitsel-testimonial-video">';
            echo '<video controls playsinline preload="metadata">';
            echo '<source src="' . esc_url( $video_url ) . '" type="video/' . esc_attr( $extension ) . '">';
            echo esc_html__( 'Your browser does not support the video tag.', 'softech-addons-for-elementor' );
            echo '</video>';
            echo '</div>';
        } else {
            echo '<a class="sitsel-testimonial-video-link" href="' . esc_url( $video_url ) . '" target="_blank" rel="noopener noreferrer">';
            echo esc_html__( 'Watch video', 'softech-addons-for-elementor' );
            echo '</a>';
        }

        echo '</div>';
    }

    /**
     * Render slider controls (pagination and navigation)
     * 
     * @param array $settings Widget settings
     */
    private function render_slider_controls($settings)
    {
        $pagination = $settings['pagination'] === 'yes';
        $navigation = $settings['navigation'] === 'yes';
        $controls_position = $settings['controls_position'] ?? 'inside';

        if ($pagination) {
            $controls_class = $controls_position === 'outside'
                ? 'sitsel-swiper-controls outside'
                : 'sitsel-swiper-controls inside';

            echo '<div class="' . esc_attr($controls_class) . '">';
            echo '<div class="swiper-pagination"></div>';
            echo '</div>';
        }

        // Navigation
        if ($navigation) {
            echo '<div class="swiper-button-prev">';
            if (!empty($settings['nav_prev_icon']['value'])) {
                \Elementor\Icons_Manager::render_icon(
                    $settings['nav_prev_icon'],
                    ['aria-hidden' => 'true']
                );
            }
            echo '</div>';

            echo '<div class="swiper-button-next">';
            if (!empty($settings['nav_next_icon']['value'])) {
                \Elementor\Icons_Manager::render_icon(
                    $settings['nav_next_icon'],
                    ['aria-hidden' => 'true']
                );
            }
            echo '</div>';
        }
    }

  

    /**
     * Get Elementor templates with caching (excluding Default Kit)
     * 
     * @return array Templates array
     */
    protected function sitsel_get_elementor_templates_without_default_kit()
    {
         if (!class_exists('\Elementor\Plugin')) {
                return [];
            }
            
            $instance = \Elementor\Plugin::instance();
            if (!$instance || !isset($instance->frontend)) {
                return [];
            }
        $cache_key = 'sitsel_elementor_templates';
        $templates = wp_cache_get($cache_key, self::CACHE_GROUP);

        if (false === $templates) {
            $args = [
                'post_type'      => 'elementor_library',
                'post_status'    => 'publish',
                'posts_per_page' => 100, // Cap the limit
                'fields'         => 'ids', // Only get IDs for performance
                'orderby'        => 'title',
                'order'          => 'ASC',
            ];

            $query = new \WP_Query($args);
            $templates = [];

            foreach ($query->posts as $post_id) {
                $post = get_post($post_id);
                if ($post && $post->post_title !== 'Default Kit') {
                    $templates[$post_id] = $post->post_title;
                }
            }

            wp_cache_set($cache_key, $templates, self::CACHE_GROUP, self::CACHE_EXPIRATION);
            wp_reset_postdata();
        }

        return $templates;
    }

    /**
     * Get script dependencies
     * 
     * @return array Script handles
     */
    public function get_script_depends()
    {
        // $this->register_widget_assets();
        return ['sits-swiper', 'sitsel-testimonial-js'];
    }

    /**
     * Get style dependencies
     * 
     * @return array Style handles
     */
    public function get_style_depends()
    {
        // $this->register_widget_assets();
        return ['sits-swiper', 'sitsel-common-css'];
    }
}
