<?php
namespace Sifency\Traits;

if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Utils;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Numeric_Pagination;
use Sifency\Traits\Sifency_Addons_Section_Header;

trait Sifency_Product_Slider_Controls {
	use Sifency_Addons_Numeric_Pagination;
	use Sifency_Addons_Section_Header;

	protected function add_sifency_product_slider_controls( $args = array() ) {
		$this->start_controls_section( 'section_query', array( 'label' => 'Query' ) );
		$this->add_control(
			'source',
			array(
				'label'   => 'Source',
				'type'    => Controls_Manager::SELECT,
				'default' => 'products',
				'options' => array(
					'products'         => 'Latest Products',
					'sale'             => 'Sale',
					'featured'         => 'Featured',
					'related_products' => 'Related Products',
				),
			)
		);
		$this->add_control(
			'posts_per_page',
			array(
				'label'   => 'Products per Page',
				'type'    => Controls_Manager::NUMBER,
				'default' => 8,
			)
		);
		$this->add_control(
			'orderby',
			array(
				'label'   => 'Order By',
				'type'    => Controls_Manager::SELECT,
				'default' => 'date',
				'options' => array(
					'date'  => 'Date',
					'title' => 'Title',
					'price' => 'Price',
					'rand'  => 'Random',
				),
			)
		);
		$this->add_control(
			'order',
			array(
				'label'   => 'Order',
				'type'    => Controls_Manager::SELECT,
				'default' => 'DESC',
				'options' => array(
					'ASC'  => 'ASC',
					'DESC' => 'DESC',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_layout_settings',
			array(
				'label' => __( 'Layout Settings', 'sifency-addons' ),
			)
		);

		$this->add_control(
			'show_sale_flash',
			array(
				'label'        => __( 'Show Sale Flash', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'sifency-addons' ),
				'label_off'    => __( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_rating',
			array(
				'label'        => __( 'Show Rating', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'sifency-addons' ),
				'label_off'    => __( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_price',
			array(
				'label'        => __( 'Show Price', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'sifency-addons' ),
				'label_off'    => __( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->add_control(
			'show_add_to_cart',
			array(
				'label'        => __( 'Show Add to Cart', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => __( 'Show', 'sifency-addons' ),
				'label_off'    => __( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();

		$this->add_sifency_addons_slider_controls();

		// ==================================================
		// Style Tab
		// ==================================================
		$this->start_controls_section(
			'section_rating_style',
			array(
				'label'     => __( 'Rating', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_rating' => 'yes' ),
			)
		);

		$this->add_control(
			'rating_color',
			array(
				'label'     => __( 'Star Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .star-rating span::before' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'rating_empty_color',
			array(
				'label'     => __( 'Empty Star Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'rating_spacing',
			array(
				'label'      => __( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .star-rating' => 'margin-bottom: {{SIZE}}{{UNIT}};',
				),
			)
		);
		$this->end_controls_section();
	}

	/**
	 * Helper: Generate WP_Query Arguments for product slider based on settings
	 */
	protected function get_sifency_product_slider_query_args( $settings ) {
		$args = array(
			'post_type'      => 'product',
			'post_status'    => 'publish',
			'posts_per_page' => $settings['posts_per_page'] ?? 8,
			'orderby'        => $settings['orderby'] ?? 'date',
			'order'          => $settings['order'] ?? 'DESC',
		);

		if ( 'sale' === $settings['source'] ) {
			$args['post__in'] = array_merge( array( 0 ), wc_get_product_ids_on_sale() );
		} elseif ( 'featured' === $settings['source'] ) {
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'product_visibility',
					'field'    => 'name',
					'terms'    => 'featured',
					'operator' => 'IN',
				),
			);
		} elseif ( 'related_products' === $settings['source'] ) {
			global $product;
			if ( $product && is_a( $product, 'WC_Product' ) ) {
				$related_ids = wc_get_related_products( $product->get_id(), $settings['posts_per_page'] ?? 8 );
				if ( ! empty( $related_ids ) ) {
					$args['post__in'] = $related_ids;
					$args['orderby']  = 'post__in';
				} else {
					$args['post__in'] = array( 0 );
				}
			} else {
				$args['post__in'] = array( 0 );
			}
		}

		return $args;
	}
}
