<?php
/**
 * Shared Rating Stars Controls
 *
 * Provides reusable Elementor controls for rating star styling.
 * Use this class to register consistent rating controls across widgets.
 *
 * @package GGM_Commerce
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class GGM_Rating_Stars_Controls
 *
 * Registers standardized rating star styling controls for Elementor widgets.
 */
class GGM_Rating_Stars_Controls {

	/**
	 * Register all rating star controls on a widget.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	public static function register( $widget, $args = array() ) {
		$defaults = array(
			'selector'        => '.rating-stars',
			'star_selector'   => '.star',
			'filled_selector' => '.star.filled, .star.full, .star.half',
			'empty_selector'  => '.star.empty',
			'prefix'          => 'rating',
			'section_id'      => 'rating_stars_style',
			'section_label'   => __( 'Rating Stars', 'ggm-commerce' ),
			'include_text'    => false,
			'include_hover'   => false,
			'start_section'   => true,
			'end_section'     => true,
			'condition'       => array(),
		);

		$args = wp_parse_args( $args, $defaults );

		// Build full selectors.
		$star_full    = '{{WRAPPER}} ' . $args['selector'] . ' ' . $args['star_selector'];
		$star_filled  = '{{WRAPPER}} ' . $args['selector'] . ' ' . $args['filled_selector'];
		$star_empty   = '{{WRAPPER}} ' . $args['selector'] . ' ' . $args['empty_selector'];

		// Start section if requested.
		if ( $args['start_section'] ) {
			$section_args = array(
				'label' => $args['section_label'],
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			);

			if ( ! empty( $args['condition'] ) ) {
				$section_args['condition'] = $args['condition'];
			}

			$widget->start_controls_section(
				$args['section_id'],
				$section_args
			);
		}

		// Star size.
		$widget->add_responsive_control(
			$args['prefix'] . '_star_size',
			array(
				'label'      => __( 'Star Size', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'em' ),
				'range'      => array(
					'px' => array(
						'min' => 8,
						'max' => 50,
					),
					'em' => array(
						'min' => 0.5,
						'max' => 3,
						'step' => 0.1,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 16,
				),
				'selectors'  => array(
					$star_full => 'font-size: {{SIZE}}{{UNIT}};',
				),
			)
		);

		// Star spacing.
		$widget->add_responsive_control(
			$args['prefix'] . '_star_spacing',
			array(
				'label'      => __( 'Star Spacing', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 20,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 2,
				),
				'selectors'  => array(
					$star_full                    => 'margin-right: {{SIZE}}{{UNIT}};',
					$star_full . ':last-child'    => 'margin-right: 0;',
				),
			)
		);

		// Filled star color.
		$widget->add_control(
			$args['prefix'] . '_filled_color',
			array(
				'label'     => __( 'Filled Star Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#F59E0B',
				'selectors' => array(
					$star_filled => 'color: {{VALUE}};',
				),
			)
		);

		// Empty star color.
		$widget->add_control(
			$args['prefix'] . '_empty_color',
			array(
				'label'     => __( 'Empty Star Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'default'   => '#D1D5DB',
				'selectors' => array(
					$star_empty => 'color: {{VALUE}};',
				),
			)
		);

		// Hover color (optional).
		if ( $args['include_hover'] ) {
			$widget->add_control(
				$args['prefix'] . '_hover_color',
				array(
					'label'     => __( 'Hover Color', 'ggm-commerce' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => array(
						'{{WRAPPER}} ' . $args['selector'] . ':hover ' . $args['star_selector'] => 'color: {{VALUE}};',
					),
				)
			);
		}

		// Rating text controls (optional).
		if ( $args['include_text'] ) {
			self::register_text_controls( $widget, $args );
		}

		// End section if requested.
		if ( $args['end_section'] ) {
			$widget->end_controls_section();
		}
	}

	/**
	 * Register rating text controls (for displaying numeric rating).
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_text_controls( $widget, $args ) {
		// Determine text selector based on context.
		$text_selector = isset( $args['text_selector'] )
			? $args['text_selector']
			: '.rating-value, .rating-text';

		$widget->add_control(
			$args['prefix'] . '_text_heading',
			array(
				'label'     => __( 'Rating Text', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_text_typography',
				'selector' => '{{WRAPPER}} ' . $text_selector,
			)
		);

		$widget->add_control(
			$args['prefix'] . '_text_color',
			array(
				'label'     => __( 'Text Color', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} ' . $text_selector => 'color: {{VALUE}};',
				),
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_text_spacing',
			array(
				'label'      => __( 'Text Spacing', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 30,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 8,
				),
				'selectors'  => array(
					'{{WRAPPER}} ' . $text_selector => 'margin-left: {{SIZE}}{{UNIT}};',
				),
			)
		);
	}

	/**
	 * Get default styles for rating star controls.
	 *
	 * @param string $prefix Control name prefix.
	 *
	 * @return array Default control values.
	 */
	public static function get_defaults( $prefix = 'rating' ) {
		return array(
			$prefix . '_star_size'    => array(
				'unit' => 'px',
				'size' => 16,
			),
			$prefix . '_star_spacing' => array(
				'unit' => 'px',
				'size' => 2,
			),
			$prefix . '_filled_color' => '#F59E0B',
			$prefix . '_empty_color'  => '#D1D5DB',
			$prefix . '_text_color'   => '#64748b',
			$prefix . '_text_spacing' => array(
				'unit' => 'px',
				'size' => 8,
			),
		);
	}
}
