<?php
/**
 * Elementor UGC Gallery widget.
 *
 * @package Ugcify\Elementor\Widgets
 */

namespace Ugcify\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Widget_Base;
use Ugcify\Helpers\FeatureGate;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class UGCGalleryWidget
 */
class UGCGalleryWidget extends Widget_Base {
	/**
	 * Get widget name.
	 *
	 * @return string
	 */
	public function get_name() {
		return 'ugcify_gallery';
	}

	/**
	 * Get widget title.
	 *
	 * @return string
	 */
	public function get_title() {
		return __( 'UGC Gallery', 'ugcify' );
	}

	/**
	 * Get widget icon.
	 *
	 * @return string
	 */
	public function get_icon() {
		return 'eicon-gallery-grid';
	}

	/**
	 * Get widget categories.
	 *
	 * @return array
	 */
	public function get_categories() {
		return array( 'general' );
	}

	/**
	 * Get widget keywords.
	 *
	 * @return array
	 */
	public function get_keywords() {
		return array(
			'ugcify',
			'reviews',
			'gallery',
			'woocommerce',
		);
	}

	/**
	 * Authoritative default settings for this widget.
	 *
	 * @return array
	 */
	public static function get_default_settings() {
		return array(
			'productId'               => '',
			'categories'              => array(),
			'tags'                    => array(),
			'blockTemplate'           => 'minimal_gallery',
			'minimalWidgetType'       => 'grid',
			'widgetType'              => 'grid',
			'gridStyle'               => 'masonry',
			'columnsDesktop'          => '4',
			'gapSpacing'              => '16',
			'showHeaderTitle'         => true,
			'title'                   => 'Customers are saying',
			'titleFontWeight'         => '600',
			'titleFontSize'           => 'medium',
			'emptyReviewText'         => 'No customer review yet. Be the first to review!',
			'primaryColor'            => '#201CFE',
			'titleColor'              => '#000000',
			'bodyFontColor'           => '#444444',
			'cardBgColor'             => '#FFFFFF',
			'ratingColor'             => '#FFA800',
			'buttonTextColor'         => '#FFFFFF',
			'filterFillColor'         => '#FFFFFF',
			'filterStrokeColor'       => '#201CFE',
			'filterIconColor'         => '#292D32',
			'readMoreColor'           => '#000000',
			'loadMoreTextColor'       => '#201CFE',
			'loadMoreBgColor'         => '#FFFFFF',
			'loadMoreBorderColor'     => '#201CFE',
			'reviewerNameFormat'      => 'full',
			'reviewerNameFontSize'    => 'medium',
			'showVerifiedCheck'       => true,
			'showInfluencerBadge'     => true,
			'reviewDateFormat'        => 'd M, Y',
			'starRatingSize'          => 'medium',
			'reviewTextSize'          => 'medium',
			'reviewTextLength'        => 'large',
			'reviewCardBorderRadius'  => '10',
			'reviewColumn'            => '3',
			'reviewsPerPage'          => '3',
			'showReviews'             => true,
			'showLoadMoreButton'      => true,
			'loadMoreTextSize'        => 'medium',
			'loadMoreBorderRadius'    => '10',
			'lazyLoadImages'          => true,
			'autoplayVideo'           => false,
			'enableLightbox'          => true,
			'showMediaCarousel'       => true,
			'mediaCarouselItemsToShow' => '10',
			'tagStyle'                => 'pulsing_dot',
			'directAddToCart'         => true,
			'enableUpvotes'           => false,
			'socialShareButtons'      => false,
			'customersAlsoBought'     => false,
			'showStories'             => true,
			'storiesTitle'            => 'Customer Stories',
			'storiesMaxCount'         => 8,
			'showAddYours'            => true,
			'showCommunity'           => true,
			'communityTitle'          => 'Community Reviews',
			'communitySubtitle'       => 'See what our customers are saying',
			'showShareBtn'            => true,
		);
	}

	/**
	 * Register controls.
	 *
	 * @return void
	 */
	protected function register_controls() {
		$this->register_review_filter_controls();
		$this->register_general_controls();
		$this->register_template_specific_controls();
		$this->register_header_controls();
		$this->register_color_style_controls();
		$this->register_review_section_controls();
		$this->register_media_behavior_controls();
		$this->register_action_controls();
	}

	/**
	 * Register review filters controls.
	 *
	 * @return void
	 */
	private function register_review_filter_controls() {
		$defaults = self::get_default_settings();

		$this->start_controls_section(
			'ugcify_review_filters',
			array(
				'label' => __( 'Review Filters', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		if ( ! class_exists( 'WooCommerce' ) ) {
			$this->add_control(
				'woocommerce_missing_notice',
				array(
					'type'            => Controls_Manager::RAW_HTML,
					'raw'             => esc_html__( 'WooCommerce is required for product/category/tag filter controls.', 'ugcify' ),
					'content_classes' => 'elementor-panel-alert elementor-panel-alert-warning',
				)
			);
			$this->end_controls_section();
			return;
		}

		$this->add_control(
			'productId',
			array(
				'label'         => __( 'Product', 'ugcify' ),
				'type'          => Controls_Manager::SELECT2,
				'options'       => $this->get_product_options_for_control(),
				'default'       => $defaults['productId'],
				'label_block'   => true,
				'render_type'   => 'template',
				'select2options' => array(
					'ajax' => array(
						'url'      => $this->get_block_data_url(
							'block-data/products',
							array(
								'format' => 'select2',
								'limit'  => 20,
							)
						),
						'dataType' => 'json',
						'delay'    => 250,
						'cache'    => true,
					),
				),
				'description'   => __( 'Select one product. Leave empty to use taxonomy filters or current product context.', 'ugcify' ),
			)
		);

		$this->add_control(
			'categories',
			array(
				'label'         => __( 'Categories', 'ugcify' ),
				'type'          => Controls_Manager::SELECT2,
				'multiple'      => true,
				'options'       => $this->get_term_options_for_control( 'product_cat' ),
				'default'       => $defaults['categories'],
				'label_block'   => true,
				'render_type'   => 'template',
				'select2options' => array(
					'ajax' => array(
						'url'      => $this->get_block_data_url(
							'block-data/terms',
							array(
								'format'   => 'select2',
								'limit'    => 20,
								'taxonomy' => 'product_cat',
							)
						),
						'dataType' => 'json',
						'delay'    => 250,
						'cache'    => true,
					),
				),
			)
		);

		$this->add_control(
			'tags',
			array(
				'label'         => __( 'Tags', 'ugcify' ),
				'type'          => Controls_Manager::SELECT2,
				'multiple'      => true,
				'options'       => $this->get_term_options_for_control( 'product_tag' ),
				'default'       => $defaults['tags'],
				'label_block'   => true,
				'render_type'   => 'template',
				'select2options' => array(
					'ajax' => array(
						'url'      => $this->get_block_data_url(
							'block-data/terms',
							array(
								'format'   => 'select2',
								'limit'    => 20,
								'taxonomy' => 'product_tag',
							)
						),
						'dataType' => 'json',
						'delay'    => 250,
						'cache'    => true,
					),
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Register general controls.
	 *
	 * @return void
	 */
	private function register_general_controls() {
		$defaults = self::get_default_settings();
		$can_use_reels = FeatureGate::is_enabled( 'block_studio_reels' );
		$can_use_carousel = FeatureGate::is_enabled( 'block_studio_gallery' );
		$is_pro = (bool) apply_filters( 'ugcify_is_pro_active', false );
		$plan = (string) apply_filters( 'ugcify_license_plan', '' );
		$upgrade_url = esc_url( apply_filters( 'ugcify_upgrade_url', admin_url( 'admin.php?page=ugcify#/license' ) ) );

		$this->start_controls_section(
			'ugcify_general',
			array(
				'label' => __( 'Template', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$status_label = $is_pro ? __( 'Subscribed', 'ugcify' ) : __( 'Free Plan', 'ugcify' );
		if ( $is_pro && '' !== $plan ) {
			$status_label .= ' (' . $plan . ')';
		}

		$this->add_control(
			'subscription_status_notice',
			array(
				'type' => Controls_Manager::RAW_HTML,
				'raw'  => sprintf(
					'<div style="margin-bottom:10px;"><strong>%1$s</strong><br/>%2$s<br/><a href="%3$s" target="_blank" rel="noopener noreferrer">%4$s</a></div>',
					esc_html( $status_label ),
					esc_html( $is_pro ? __( 'Premium features available', 'ugcify' ) : __( 'Upgrade to unlock Reels and Carousel templates.', 'ugcify' ) ),
					esc_url( $upgrade_url ),
					esc_html__( 'Upgrade plan', 'ugcify' )
				),
			)
		);

		$this->add_control(
			'blockTemplate',
			array(
				'label'       => __( 'Apply Template', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array_filter(
					array(
						'minimal_gallery' => __( 'Minimal Gallery', 'ugcify' ),
						'reels'           => $can_use_reels ? __( 'Reels', 'ugcify' ) : null,
						'carousel'        => $can_use_carousel ? __( 'Carousel', 'ugcify' ) : null,
					)
				),
				'default'     => $defaults['blockTemplate'],
				'render_type' => 'template',
			)
		);

		if ( ! $can_use_reels || ! $can_use_carousel ) {
			$this->add_control(
				'template_lock_notice',
				array(
					'type' => Controls_Manager::RAW_HTML,
					'raw'  => '<div style="color:#757575;">' . esc_html__( 'Available only for subscribed users.', 'ugcify' ) . '</div>',
				)
			);
		}

		$this->add_control(
			'minimalWidgetType',
			array(
				'label'       => __( 'Widget Type', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'grid'    => __( 'Grid', 'ugcify' ),
					'masonry' => __( 'Masonry', 'ugcify' ),
					'slider'  => __( 'Slider', 'ugcify' ),
				),
				'default'     => $defaults['minimalWidgetType'],
				'prefix_class' => 'ugcify-widget-type-',
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate' => 'minimal_gallery',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Register template-specific controls.
	 *
	 * @return void
	 */
	private function register_template_specific_controls() {
		$defaults = self::get_default_settings();

		$this->start_controls_section(
			'ugcify_reels_settings',
			array(
				'label'     => __( 'Reels', 'ugcify' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'blockTemplate' => 'reels',
				),
			)
		);

		$this->add_control( 'showStories', array( 'label' => __( 'Show Stories', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['showStories'] ? 'yes' : '' ) );
		$this->add_control( 'storiesTitle', array( 'label' => __( 'Stories Title', 'ugcify' ), 'type' => Controls_Manager::TEXT, 'default' => $defaults['storiesTitle'] ) );
		$this->add_control( 'storiesMaxCount', array( 'label' => __( 'Stories Max Count', 'ugcify' ), 'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => 20, 'default' => absint( $defaults['storiesMaxCount'] ) ) );
		$this->add_control( 'showAddYours', array( 'label' => __( 'Show Add Yours', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['showAddYours'] ? 'yes' : '' ) );
		$this->add_control( 'showCommunity', array( 'label' => __( 'Show Community', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['showCommunity'] ? 'yes' : '' ) );
		$this->add_control( 'communityTitle', array( 'label' => __( 'Community Title', 'ugcify' ), 'type' => Controls_Manager::TEXT, 'default' => $defaults['communityTitle'] ) );
		$this->add_control( 'communitySubtitle', array( 'label' => __( 'Community Subtitle', 'ugcify' ), 'type' => Controls_Manager::TEXT, 'default' => $defaults['communitySubtitle'] ) );
		$this->add_control( 'showShareBtn', array( 'label' => __( 'Show Share Button', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['showShareBtn'] ? 'yes' : '' ) );
		$this->end_controls_section();

		$this->start_controls_section(
			'ugcify_carousel_settings',
			array(
				'label'     => __( 'Media Carousel', 'ugcify' ),
				'tab'       => Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control( 'showMediaCarousel', array( 'label' => __( 'Show Carousel', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => $defaults['showMediaCarousel'] ? 'yes' : '', 'render_type' => 'template' ) );
		$this->add_control( 'mediaCarouselItemsToShow', array( 'label' => __( 'Items to Show', 'ugcify' ), 'type' => Controls_Manager::NUMBER, 'min' => 1, 'max' => 100, 'default' => absint( $defaults['mediaCarouselItemsToShow'] ), 'render_type' => 'template', 'condition' => array( 'showMediaCarousel' => 'yes' ) ) );
		$this->end_controls_section();
	}

	/**
	 * Register header controls.
	 *
	 * @return void
	 */
	private function register_header_controls() {
		$defaults = self::get_default_settings();

		$this->start_controls_section(
			'ugcify_header',
			array(
				'label' => __( 'Header Area', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'showHeaderTitle',
			array(
				'label'       => __( 'Show Header Title', 'ugcify' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => $defaults['showHeaderTitle'] ? 'yes' : '',
				'return_value' => 'yes',
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'title',
			array(
				'label'       => __( 'Title', 'ugcify' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => $defaults['title'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'titleFontWeight',
			array(
				'label'       => __( 'Title Font Weight', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'400' => '400',
					'500' => '500',
					'600' => '600',
					'700' => '700',
				),
				'default'     => $defaults['titleFontWeight'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'titleFontSize',
			array(
				'label'       => __( 'Title Font Size', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
				),
				'default'     => $defaults['titleFontSize'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'emptyReviewText',
			array(
				'label'       => __( 'Empty Review Text', 'ugcify' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => $defaults['emptyReviewText'],
				'render_type' => 'template',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Register color and style controls.
	 *
	 * @return void
	 */
	private function register_color_style_controls() {
		$defaults = self::get_default_settings();

		$this->start_controls_section(
			'ugcify_colors_styles',
			array(
				'label' => __( 'Colors and Styles', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'primaryColor',
			array(
				'label'   => __( 'Primary Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['primaryColor'],
			)
		);

		$this->add_control(
			'titleColor',
			array(
				'label'   => __( 'Title Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['titleColor'],
			)
		);

		$this->add_control(
			'bodyFontColor',
			array(
				'label'   => __( 'Body Font Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['bodyFontColor'],
			)
		);

		$this->add_control(
			'cardBgColor',
			array(
				'label'   => __( 'Card Background Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['cardBgColor'],
			)
		);

		$this->add_control(
			'ratingColor',
			array(
				'label'   => __( 'Rating Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['ratingColor'],
			)
		);

		$this->add_control(
			'buttonTextColor',
			array(
				'label'   => __( 'Button Text Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['buttonTextColor'],
			)
		);

		$this->add_control(
			'filterFillColor',
			array(
				'label'   => __( 'Filter Fill Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['filterFillColor'],
			)
		);

		$this->add_control(
			'filterStrokeColor',
			array(
				'label'   => __( 'Filter Stroke Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['filterStrokeColor'],
			)
		);

		$this->add_control(
			'filterIconColor',
			array(
				'label'   => __( 'Filter Icon Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['filterIconColor'],
			)
		);

		$this->add_control(
			'readMoreColor',
			array(
				'label'   => __( 'Read More Color', 'ugcify' ),
				'type'    => Controls_Manager::COLOR,
				'default' => $defaults['readMoreColor'],
			)
		);

		$this->add_control(
			'loadMoreTextColor',
			array(
				'label'     => __( 'Load More Text Color', 'ugcify' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => $defaults['loadMoreTextColor'],
				'condition' => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control(
			'loadMoreBgColor',
			array(
				'label'     => __( 'Load More Background Color', 'ugcify' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => $defaults['loadMoreBgColor'],
				'condition' => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control(
			'loadMoreBorderColor',
			array(
				'label'     => __( 'Load More Border Color', 'ugcify' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => $defaults['loadMoreBorderColor'],
				'condition' => array(
					'blockTemplate' => 'carousel',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Register media behavior controls.
	 *
	 * @return void
	 */
	private function register_media_behavior_controls() {
		$defaults = self::get_default_settings();
		$this->start_controls_section(
			'ugcify_media_behavior',
			array(
				'label' => __( 'Media Behavior', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control( 'lazyLoadImages', array( 'label' => __( 'Lazy Load Images', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['lazyLoadImages'] ? 'yes' : '' ) );
		$this->add_control( 'autoplayVideo', array( 'label' => __( 'Autoplay Video (Muted)', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['autoplayVideo'] ? 'yes' : '' ) );
		$this->add_control( 'enableLightbox', array( 'label' => __( 'Enable Lightbox', 'ugcify' ), 'type' => Controls_Manager::SWITCHER, 'default' => $defaults['enableLightbox'] ? 'yes' : '' ) );
		$this->end_controls_section();
	}

	/**
	 * Register review section controls.
	 *
	 * @return void
	 */
	private function register_review_section_controls() {
		$defaults = self::get_default_settings();

		$this->start_controls_section(
			'ugcify_review_section',
			array(
				'label' => __( 'Review Section', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'reviewerNameFormat',
			array(
				'label'       => __( 'Reviewer Name Format', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'full'               => __( 'Full', 'ugcify' ),
					'first_last_initial' => __( 'First + Last Initial', 'ugcify' ),
					'first_only'         => __( 'First Only', 'ugcify' ),
					'initials'           => __( 'Initials', 'ugcify' ),
				),
				'default'     => $defaults['reviewerNameFormat'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'reviewerNameFontSize',
			array(
				'label'       => __( 'Reviewer Name Font Size', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
				),
				'default'     => $defaults['reviewerNameFontSize'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'showVerifiedCheck',
			array(
				'label'        => __( 'Show Verified Check', 'ugcify' ),
				'type'         => Controls_Manager::SWITCHER,
				'default'      => $defaults['showVerifiedCheck'] ? 'yes' : '',
				'return_value' => 'yes',
				'render_type'  => 'template',
			)
		);

		$this->add_control(
			'reviewDateFormat',
			array(
				'label'       => __( 'Review Date Format', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'd M, Y' => '12 Dec, 2025',
					'M d, Y' => 'Dec 12, 2025',
					'd/m/Y'  => '12/12/2025',
					'Y-m-d'  => '2025-12-12',
				),
				'default'     => $defaults['reviewDateFormat'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'starRatingSize',
			array(
				'label'       => __( 'Star Rating Size', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
				),
				'default'     => $defaults['starRatingSize'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'reviewTextSize',
			array(
				'label'       => __( 'Review Text Size', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
				),
				'default'     => $defaults['reviewTextSize'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'reviewTextLength',
			array(
				'label'       => __( 'Review Text Length', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
					'full'   => __( 'Full', 'ugcify' ),
				),
				'default'     => $defaults['reviewTextLength'],
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'reviewCardBorderRadius',
			array(
				'label'       => __( 'Review Card Border Radius', 'ugcify' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 100,
				'step'        => 1,
				'default'     => absint( $defaults['reviewCardBorderRadius'] ),
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate!' => 'carousel',
				),
			)
		);

		$this->add_control(
			'reviewColumn',
			array(
				'label'       => __( 'Review Columns', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'1' => '1',
					'2' => '2',
					'3' => '3',
					'4' => '4',
				),
				'default'     => $defaults['reviewColumn'],
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate!' => 'carousel',
				),
			)
		);

		$this->add_control(
			'reviewsPerPage',
			array(
				'label'       => __( 'Reviews Per Page', 'ugcify' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 1,
				'max'         => 50,
				'step'        => 1,
				'default'     => absint( $defaults['reviewsPerPage'] ),
				'render_type' => 'template',
			)
		);

		$this->add_control(
			'showReviews',
			array(
				'label'       => __( 'Show Reviews', 'ugcify' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => $defaults['showReviews'] ? 'yes' : '',
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control(
			'showLoadMoreButton',
			array(
				'label'       => __( 'Show Load More Button', 'ugcify' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => $defaults['showLoadMoreButton'] ? 'yes' : '',
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control(
			'loadMoreTextSize',
			array(
				'label'       => __( 'Load More Text Size', 'ugcify' ),
				'type'        => Controls_Manager::SELECT,
				'options'     => array(
					'small'  => __( 'Small', 'ugcify' ),
					'medium' => __( 'Medium', 'ugcify' ),
					'large'  => __( 'Large', 'ugcify' ),
				),
				'default'     => $defaults['loadMoreTextSize'],
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate' => 'carousel',
				),
			)
		);
		$this->add_control(
			'loadMoreBorderRadius',
			array(
				'label'       => __( 'Load More Border Radius', 'ugcify' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 100,
				'step'        => 1,
				'default'     => absint( $defaults['loadMoreBorderRadius'] ),
				'render_type' => 'template',
				'condition'   => array(
					'blockTemplate' => 'carousel',
				),
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Register action controls.
	 *
	 * @return void
	 */
	private function register_action_controls() {
		$this->start_controls_section(
			'ugcify_actions',
			array(
				'label' => __( 'Actions', 'ugcify' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);

		$this->add_control(
			'reset_to_defaults',
			array(
				'type'        => Controls_Manager::BUTTON,
				'text'        => __( 'Reset to defaults', 'ugcify' ),
				'button_type' => 'default',
				'event'       => 'ugcCommerceElementor:resetDefaults',
			)
		);

		$this->end_controls_section();
	}

	/**
	 * Render widget.
	 *
	 * @return void
	 */
	protected function render() {
		if ( ! shortcode_exists( 'ugcify_reviews' ) ) {
			return;
		}

		$settings = $this->get_settings_for_display();
		$settings = $this->normalize_settings_for_plan( is_array( $settings ) ? $settings : array() );

		$product_id = isset( $settings['productId'] ) ? absint( $settings['productId'] ) : 0;
		$categories = $this->sanitize_slug_array( isset( $settings['categories'] ) ? $settings['categories'] : array() );
		$tags       = $this->sanitize_slug_array( isset( $settings['tags'] ) ? $settings['tags'] : array() );

		$instance_id = 'ugcify-el-' . sanitize_html_class( (string) $this->get_id() );
		if ( '' === $instance_id ) {
			$instance_id = 'ugcify-el-' . wp_unique_id();
		}

		$shortcode_attributes = array(
			'instance_id' => $instance_id,
		);

		if ( $product_id > 0 ) {
			$shortcode_attributes['product_id'] = (string) $product_id;
		}

		if ( ! empty( $categories ) ) {
			$shortcode_attributes['categories'] = implode( ',', $categories );
		}

		if ( ! empty( $tags ) ) {
			$shortcode_attributes['tags'] = implode( ',', $tags );
		}

		foreach ( self::get_template_control_keys() as $key ) {
			if ( ! isset( $settings[ $key ] ) ) {
				continue;
			}

			$sanitized_value = $this->sanitize_template_setting_value( $key, $settings[ $key ] );

			if ( is_bool( $sanitized_value ) ) {
				$shortcode_attributes[ $key ] = $sanitized_value ? '1' : '0';
			} else {
				$shortcode_attributes[ $key ] = (string) $sanitized_value;
			}
		}

		$shortcode = '[ugcify_reviews';
		foreach ( $shortcode_attributes as $key => $value ) {
			$shortcode .= sprintf(
				' %1$s="%2$s"',
				sanitize_key( $key ),
				esc_attr( $value )
			);
		}
		$shortcode .= ']';

		$output = do_shortcode( $shortcode );

		if ( $this->is_elementor_editor_preview() ) {
			$this->enqueue_elementor_preview_runtime_assets();
			$output = $this->wrap_editor_preview_output( $output );
		}

		echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Return template-setting keys only.
	 *
	 * @return array
	 */
	private static function get_template_control_keys() {
		$defaults = self::get_default_settings();
		unset( $defaults['productId'], $defaults['categories'], $defaults['tags'], $defaults['minimalWidgetType'] );
		return array_keys( $defaults );
	}

	/**
	 * Sanitize template setting value.
	 *
	 * @param string $key   Setting key.
	 * @param mixed  $value Setting value.
	 * @return mixed
	 */
	private function sanitize_template_setting_value( $key, $value ) {
		$defaults = self::get_default_settings();
		$default  = isset( $defaults[ $key ] ) ? $defaults[ $key ] : '';

		$size_values = array( 'small', 'medium', 'large' );

		switch ( $key ) {
			case 'widgetType':
			case 'minimalWidgetType':
				return in_array( $value, array( 'grid', 'masonry', 'slider', 'reels', 'gallery' ), true ) ? $value : $default;

			case 'blockTemplate':
				return in_array( $value, array( 'minimal_gallery', 'reels', 'carousel' ), true ) ? $value : $default;

			case 'titleFontWeight':
				return in_array( $value, array( '400', '500', '600', '700' ), true ) ? $value : $default;

			case 'titleFontSize':
			case 'reviewerNameFontSize':
			case 'starRatingSize':
			case 'reviewTextSize':
				return in_array( $value, $size_values, true ) ? $value : $default;

			case 'reviewerNameFormat':
				return in_array(
					$value,
					array( 'full', 'first_last_initial', 'first_only', 'initials' ),
					true
				) ? $value : $default;

			case 'reviewDateFormat':
				return in_array( $value, array( 'd M, Y', 'M d, Y', 'd/m/Y', 'Y-m-d' ), true ) ? $value : $default;

			case 'reviewTextLength':
				return in_array( $value, array( 'small', 'medium', 'large', 'full' ), true ) ? $value : $default;

			case 'reviewColumn':
				return in_array( (string) $value, array( '1', '2', '3', '4' ), true ) ? (string) $value : $default;

			case 'reviewsPerPage':
				$reviews_per_page = absint( $value );
				if ( $reviews_per_page < 1 ) {
					return (string) $default;
				}
				return (string) min( 50, $reviews_per_page );

			case 'reviewCardBorderRadius':
			case 'loadMoreBorderRadius':
				return (string) min( 100, absint( $value ) );

			case 'gridStyle':
				return in_array( $value, array( 'masonry', 'uniform_grid' ), true ) ? $value : $default;

			case 'tagStyle':
				return in_array( $value, array( 'pulsing_dot', 'simple_badge', 'none' ), true ) ? $value : $default;

			case 'showHeaderTitle':
			case 'showVerifiedCheck':
			case 'showInfluencerBadge':
			case 'showReviews':
			case 'showLoadMoreButton':
			case 'lazyLoadImages':
			case 'autoplayVideo':
			case 'enableLightbox':
			case 'showMediaCarousel':
			case 'showStories':
			case 'showAddYours':
			case 'showCommunity':
			case 'showShareBtn':
				$bool_value = filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
				return null === $bool_value ? (bool) $default : $bool_value;

			case 'columnsDesktop':
				return (string) min( 6, max( 1, absint( $value ) ) );

			case 'gapSpacing':
				return (string) min( 60, max( 0, absint( $value ) ) );

			case 'mediaCarouselItemsToShow':
				return (string) min( 100, max( 1, absint( $value ) ) );

			case 'storiesMaxCount':
				return (string) min( 20, max( 1, absint( $value ) ) );

			case 'loadMoreTextSize':
				return in_array( $value, $size_values, true ) ? $value : $default;

			case 'primaryColor':
			case 'titleColor':
			case 'bodyFontColor':
			case 'cardBgColor':
			case 'ratingColor':
			case 'buttonTextColor':
			case 'filterFillColor':
			case 'filterStrokeColor':
			case 'filterIconColor':
			case 'readMoreColor':
			case 'loadMoreTextColor':
			case 'loadMoreBgColor':
			case 'loadMoreBorderColor':
				$sanitized_color = sanitize_hex_color( (string) $value );
				return $sanitized_color ? $sanitized_color : $default;

			default:
				return sanitize_text_field( (string) $value );
		}
	}

	/**
	 * Sanitize slug array.
	 *
	 * @param mixed $raw_values Raw values.
	 * @return array
	 */
	private function sanitize_slug_array( $raw_values ) {
		if ( ! is_array( $raw_values ) ) {
			return array();
		}

		$values = array_map( 'sanitize_title', $raw_values );
		$values = array_filter( $values );
		$values = array_values( array_unique( $values ) );

		return $values;
	}

	/**
	 * Get block-data endpoint URL.
	 *
	 * @param string $path Endpoint path.
	 * @param array  $args Query args.
	 * @return string
	 */
	private function get_block_data_url( $path, $args = array() ) {
		$base_url = rest_url( 'ugcify/v1/' . ltrim( $path, '/' ) );
		$args     = is_array( $args ) ? $args : array();
		$args['_wpnonce'] = wp_create_nonce( 'wp_rest' );

		return add_query_arg( $args, $base_url );
	}

	/**
	 * Get initial product options for select control.
	 *
	 * @return array
	 */
	private function get_product_options_for_control() {
		$options = array();

		if ( ! function_exists( 'wc_get_products' ) || ! function_exists( 'wc_get_product' ) ) {
			return $options;
		}

		$products = wc_get_products(
			array(
				'status' => array( 'publish' ),
				'limit'  => 20,
				'return' => 'objects',
			)
		);

		foreach ( $products as $product ) {
			if ( ! $product ) {
				continue;
			}
			$options[ (string) $product->get_id() ] = (string) $product->get_name();
		}

		return $options;
	}

	/**
	 * Get initial term options for select controls.
	 *
	 * @param string $taxonomy Taxonomy.
	 * @return array
	 */
	private function get_term_options_for_control( $taxonomy ) {
		$options = array();

		if ( ! taxonomy_exists( $taxonomy ) ) {
			return $options;
		}

		$terms = get_terms(
			array(
				'taxonomy'   => $taxonomy,
				'hide_empty' => false,
				'number'     => 20,
				'orderby'    => 'name',
				'order'      => 'ASC',
			)
		);

		if ( is_array( $terms ) && ! is_wp_error( $terms ) ) {
			foreach ( $terms as $term ) {
				$options[ (string) $term->slug ] = (string) $term->name;
			}
		}

		return $options;
	}

	/**
	 * Determine if Elementor editor preview is active.
	 *
	 * @return bool
	 */
	private function is_elementor_editor_preview() {
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return false;
		}

		return \Elementor\Plugin::$instance->editor->is_edit_mode();
	}

	/**
	 * Wrap output for non-interactive editor preview.
	 *
	 * @param string $output Shortcode output.
	 * @return string
	 */
	private function wrap_editor_preview_output( $output ) {
		static $style_output = '';

		if ( '' === $style_output ) {
			$css_file = UGCIFY_PATH . 'assets/frontend/styles.css';
			$base_css = '';
			if ( file_exists( $css_file ) && is_readable( $css_file ) ) {
				$raw_css = file_get_contents( $css_file );
				if ( is_string( $raw_css ) ) {
					$base_css = $raw_css;
				}
			}

			$preview_css = '.elementor-editor-active .ugcify-elementor-preview-lock a,'
				. '.elementor-editor-active .ugcify-elementor-preview-lock button,'
				. '.elementor-editor-active .ugcify-elementor-preview-lock input,'
				. '.elementor-editor-active .ugcify-elementor-preview-lock select,'
				. '.elementor-editor-active .ugcify-elementor-preview-lock textarea,'
				. '.elementor-editor-active .ugcify-elementor-preview-lock .page-numbers{'
				. 'pointer-events:none !important;cursor:default !important;}';

			$style_output = '<style class="ugcify-elementor-preview-styles">' . $base_css . $preview_css . '</style>';
		}

		return $style_output . '<div class="ugcify-elementor-preview-lock">' . $output . '</div>';
	}

	/**
	 * Normalize widget settings for current plan and selected template.
	 *
	 * @param array $settings Raw settings.
	 * @return array
	 */
	private function normalize_settings_for_plan( $settings ) {
		$defaults = self::get_default_settings();
		$has_minimal_widget_type = isset( $settings['minimalWidgetType'] ) && '' !== $settings['minimalWidgetType'];
		$settings = array_merge( $defaults, $settings );

		$block_template = $this->sanitize_template_setting_value( 'blockTemplate', $settings['blockTemplate'] );
		$legacy_widget_type = $this->sanitize_template_setting_value( 'widgetType', $settings['widgetType'] );
		$minimal_widget_type = $has_minimal_widget_type ? $this->sanitize_template_setting_value( 'minimalWidgetType', $settings['minimalWidgetType'] ) : $legacy_widget_type;

		if ( empty( $settings['blockTemplate'] ) || ! in_array( $block_template, array( 'minimal_gallery', 'reels', 'carousel' ), true ) ) {
			if ( 'reels' === $legacy_widget_type ) {
				$block_template = 'reels';
			} elseif ( 'gallery' === $legacy_widget_type ) {
				$block_template = 'carousel';
			} else {
				$block_template = 'minimal_gallery';
			}
		}

		if ( 'reels' === $block_template && ! FeatureGate::is_enabled( 'block_studio_reels' ) ) {
			$block_template = 'minimal_gallery';
		}
		if ( 'carousel' === $block_template && ! FeatureGate::is_enabled( 'block_studio_gallery' ) ) {
			$block_template = 'minimal_gallery';
		}

		$settings['blockTemplate'] = $block_template;
		if ( 'reels' === $block_template ) {
			$settings['widgetType'] = 'reels';
		} elseif ( 'carousel' === $block_template ) {
			$settings['widgetType'] = 'gallery';
		} else {
			$settings['widgetType'] = in_array( $minimal_widget_type, array( 'grid', 'masonry', 'slider' ), true ) ? $minimal_widget_type : 'grid';
			if ( 'slider' !== $settings['widgetType'] ) {
				$settings['reviewColumn'] = (string) absint( $settings['columnsDesktop'] );
			}
		}

		if ( ! FeatureGate::is_enabled( 'block_studio_advanced' ) ) {
			foreach ( array(
				'primaryColor',
				'titleColor',
				'bodyFontColor',
				'cardBgColor',
				'ratingColor',
				'buttonTextColor',
				'filterFillColor',
				'filterStrokeColor',
				'filterIconColor',
				'readMoreColor',
				'loadMoreTextColor',
				'loadMoreBgColor',
				'loadMoreBorderColor',
				'lazyLoadImages',
				'autoplayVideo',
				'enableLightbox',
			) as $locked_key ) {
				$settings[ $locked_key ] = $defaults[ $locked_key ];
			}
		}

		foreach ( array( 'enableUpvotes', 'socialShareButtons', 'customersAlsoBought' ) as $locked_key ) {
			$settings[ $locked_key ] = $defaults[ $locked_key ];
		}

		return $settings;
	}

	/**
	 * Ensure frontend assets are available in Elementor editor preview iframe.
	 *
	 * @return void
	 */
	private function enqueue_elementor_preview_runtime_assets() {
		static $done = false;
		if ( $done ) {
			return;
		}

		$done = true;
		wp_enqueue_script( 'ugcify-frontend', UGCIFY_URL . 'assets/frontend/app.js', array( 'jquery' ), UGCIFY_VERSION, true );
		wp_enqueue_style( 'ugcify-frontend', UGCIFY_URL . 'assets/frontend/styles.css', array(), UGCIFY_VERSION );

		wp_localize_script(
			'ugcify-frontend',
			'ugcCommerce',
			array(
				'apiUrl'           => rest_url( 'ugcify/v1' ),
				'ajaxUrl'          => admin_url( 'admin-ajax.php' ),
				'nonce'            => wp_create_nonce( 'wp_rest' ),
				'pluginUrl'        => UGCIFY_URL,
				'productId'        => 0,
				'perPage'          => apply_filters( 'ugcify_reviews_per_page', 3 ),
				'locale'           => str_replace( '_', '-', get_locale() ),
				'gmtOffset'        => (float) get_option( 'gmt_offset', 0 ),
				'wcCartRedirect'   => false,
				'wcCartUrl'        => function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : '',
				'isProActive'      => (bool) apply_filters( 'ugcify_is_pro_active', false ),
				'licensePlan'      => (string) apply_filters( 'ugcify_license_plan', '' ),
				'features'         => array(
					'block_studio_advanced' => FeatureGate::is_enabled( 'block_studio_advanced' ),
					'block_studio_reels'    => FeatureGate::is_enabled( 'block_studio_reels' ),
					'block_studio_gallery'  => FeatureGate::is_enabled( 'block_studio_gallery' ),
				),
				'upgradeUrl'       => esc_url( apply_filters( 'ugcify_upgrade_url', admin_url( 'admin.php?page=ugcify#/license' ) ) ),
			)
		);
	}
}
