<?php
/**
 * Product Carousel Widget (Premium)
 *
 * Displays products in a configurable carousel with peek/partial
 * visibility of adjacent slides, multiple layout modes, and full
 * control over navigation, transitions, and card design.
 *
 * @package GGM_Commerce
 */

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Data handler (shared with product grid and other widgets)
require_once plugin_dir_path( __FILE__ ) . '../data/class-product-data-handler.php';

// Render functions for carousel output
require_once __DIR__ . '/widget-product-carousel/elementor-render.php';

/**
 * Class GGM_Commerce_Widget_Product_Carousel
 *
 * Premium widget for displaying products in a carousel/slider layout.
 */
class GGM_Commerce_Widget_Product_Carousel extends GGM_Commerce_Premium_Widget {

	/**
	 * Premium widget slug for license activation.
	 *
	 * @var string
	 */
	protected $premium_widget_slug = 'product-carousel';

	/**
	 * Get widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'ggmc-product-carousel';
	}

	/**
	 * Get widget title.
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return 'Product Carousel';
	}

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

	/**
	 * Get widget keywords.
	 *
	 * @return array Widget keywords.
	 */
	public function get_keywords() {
		return array( 'ggm', 'commerce', 'product', 'carousel', 'slider', 'featured', 'premium' );
	}

	/**
	 * Get script dependencies.
	 *
	 * @return array Script handles.
	 */
	public function get_script_depends() {
		return array( 'ggmc-product-carousel' );
	}

	/**
	 * Get static default styles for this widget.
	 *
	 * @return array Default styles.
	 */
	public static function get_static_default_styles() {
		return self::get_defaults_from_config();
	}

	/**
	 * Register widget controls.
	 */
	protected function register_controls() {
		require __DIR__ . '/widget-product-carousel/elementor-controls.php';
		require __DIR__ . '/widget-product-carousel/elementor-controls-style.php';
	}

	/**
	 * Override get_raw_data to ensure component_order includes all components.
	 */
	public function get_raw_data( $with_html_content = false ) {
		$data = parent::get_raw_data( $with_html_content );

		if ( ! isset( $data['settings'] ) ) {
			$data['settings'] = array();
		}

		$existing_order                          = $data['settings']['component_order'] ?? array();
		$data['settings']['component_order']     = $this->ensure_all_components_in_order( $existing_order );

		return $data;
	}

	/**
	 * Override get_settings_for_display to ensure component_order includes all components.
	 */
	public function get_settings_for_display( $setting_key = null ) {
		$settings = parent::get_settings_for_display( $setting_key );

		if ( $setting_key !== null && $setting_key !== 'component_order' ) {
			return $settings;
		}

		if ( $setting_key === 'component_order' ) {
			return $this->ensure_all_components_in_order( $settings ?? array() );
		}

		$existing_order                = $settings['component_order'] ?? array();
		$settings['component_order']   = $this->ensure_all_components_in_order( $existing_order );

		return $settings;
	}

	/**
	 * Ensure all components are present in the component order.
	 */
	private function ensure_all_components_in_order( $component_order ) {
		if ( ! is_array( $component_order ) ) {
			$component_order = array();
		}

		$available_components = ggmc_product_carousel_get_available_components();

		if ( empty( $component_order ) ) {
			return $available_components;
		}

		$available_ids = array_column( $available_components, 'component_id' );

		// Remove components that are no longer available (premium deactivated)
		$component_order = array_filter( $component_order, function ( $component ) use ( $available_ids ) {
			return isset( $component['component_id'] ) && in_array( $component['component_id'], $available_ids, true );
		});
		$component_order = array_values( $component_order );

		$existing_ids = array_column( $component_order, 'component_id' );

		// Insert missing components at their default position
		foreach ( $available_components as $default_index => $default_component ) {
			if ( ! in_array( $default_component['component_id'], $existing_ids, true ) ) {
				$insert_position = min( $default_index, count( $component_order ) );
				array_splice( $component_order, $insert_position, 0, array( $default_component ) );
				$existing_ids = array_column( $component_order, 'component_id' );
			}
		}

		return $component_order;
	}

	/**
	 * Render the premium content.
	 */
	protected function render_premium_content() {
		ggmc_product_carousel_render_premium_content( $this );
	}
}
