<?php
/**
 * Product Filter Widget - Content Controls
 *
 * Included via require inside register_controls(). $this refers to the widget instance.
 *
 * @package GGM_Commerce
 */

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

// $this is the GGM_Commerce_Product_Filter_Widget instance.

// -------------------------------------------------------------------------
// Filter Options Section
// -------------------------------------------------------------------------

$this->start_controls_section(
	'content_section',
	[
		'label' => __( 'Filter Options', 'ggm-commerce' ),
		'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
	]
);

$this->add_control(
	'show_title',
	[
		'label'        => __( 'Show Title', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'yes',
	]
);

$this->add_responsive_control(
	'filter_title',
	[
		'label'       => __( 'Filter Title', 'ggm-commerce' ),
		'type'        => \Elementor\Controls_Manager::TEXT,
		'default'     => __( 'Filter Products', 'ggm-commerce' ),
		'placeholder' => __( 'Enter filter title', 'ggm-commerce' ),
		'condition'   => [
			'show_title' => 'yes',
		],
	]
);

$this->add_control(
	'show_search',
	[
		'label'        => __( 'Show Search Box', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'yes',
	]
);

$this->add_control(
	'show_categories',
	[
		'label'        => __( 'Show Categories Filter', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'yes',
	]
);

$this->add_control(
	'show_tags',
	[
		'label'        => __( 'Show Tags Filter', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'yes',
	]
);

$this->add_control(
	'hide_empty',
	[
		'label'       => __( 'Hide Empty Categories/Tags', 'ggm-commerce' ),
		'type'        => \Elementor\Controls_Manager::SWITCHER,
		'label_on'    => __( 'Yes', 'ggm-commerce' ),
		'label_off'   => __( 'No', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'     => 'yes',
		'description' => __( 'Hide categories and tags with no products', 'ggm-commerce' ),
	]
);

$this->add_control(
	'show_price_range',
	[
		'label'        => __( 'Show Price Range', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'yes',
	]
);

$this->add_control(
	'price_range_display_type',
	[
		'label'     => __( 'Price Range Display', 'ggm-commerce' ),
		'type'      => \Elementor\Controls_Manager::SELECT,
		'default'   => 'slider',
		'options'   => [
			'slider' => __( 'Range Slider', 'ggm-commerce' ),
			'inputs' => __( 'Min/Max Inputs', 'ggm-commerce' ),
		],
		'condition' => [
			'show_price_range' => 'yes',
		],
	]
);

$this->add_control(
	'min_price',
	[
		'label'     => __( 'Minimum Price', 'ggm-commerce' ),
		'type'      => \Elementor\Controls_Manager::NUMBER,
		'default'   => 0,
		'min'       => 0,
		'step'      => 1,
		'condition' => [
			'show_price_range' => 'yes',
		],
	]
);

$this->add_control(
	'max_price',
	[
		'label'     => __( 'Maximum Price', 'ggm-commerce' ),
		'type'      => \Elementor\Controls_Manager::NUMBER,
		'default'   => 1000,
		'min'       => 1,
		'step'      => 1,
		'condition' => [
			'show_price_range' => 'yes',
		],
	]
);

$this->add_control(
	'show_stock_filter',
	[
		'label'        => __( 'Show Stock Filter', 'ggm-commerce' ),
		'type'         => \Elementor\Controls_Manager::SWITCHER,
		'label_on'     => __( 'Show', 'ggm-commerce' ),
		'label_off'    => __( 'Hide', 'ggm-commerce' ),
		'return_value' => 'yes',
		'default'      => 'no',
	]
);

$this->end_controls_section();

// -------------------------------------------------------------------------
// Attribute Filters Section (only if Custom Attributes premium is active)
// -------------------------------------------------------------------------

if ( class_exists( 'GGM_Commerce_License' ) ) {
	$license = GGM_Commerce_License::instance();

	if ( $license->is_widget_activated( 'custom-attributes' ) && class_exists( 'GGM_Commerce_Attributes' ) ) {
		$attributes_manager = GGM_Commerce_Attributes::get_instance();
		$filterable_attrs   = $attributes_manager->get_filterable_attributes();

		// Build options array for SELECT2
		$attr_options = array();
		foreach ( $filterable_attrs as $attr ) {
			$label = $attr->name;
			if ( ! empty( $attr->group_name ) ) {
				$label .= ' (' . $attr->group_name . ')';
			}
			$attr_options[ $attr->id ] = $label;
		}

		$this->start_controls_section(
			'attribute_filters_section',
			[
				'label' => __( 'Attribute Filters', 'ggm-commerce' ),
				'tab'   => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		if ( empty( $attr_options ) ) {
			$this->add_control(
				'no_filterable_attrs_notice',
				[
					'type'            => \Elementor\Controls_Manager::RAW_HTML,
					'raw'             => '<div style="padding: 10px; background: #f0f0f1; border-radius: 3px;">' .
						__( 'No filterable attributes found. Go to Goose Commerce > Attributes and enable the "Filterable" toggle on attributes you want to use as filters.', 'ggm-commerce' ) .
						'</div>',
					'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
				]
			);
		} else {
			$this->add_control(
				'show_attribute_filters',
				[
					'label'        => __( 'Show Attribute Filters', 'ggm-commerce' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'label_on'     => __( 'Show', 'ggm-commerce' ),
					'label_off'    => __( 'Hide', 'ggm-commerce' ),
					'return_value' => 'yes',
					'default'      => 'no',
				]
			);

			$this->add_control(
				'selected_attributes',
				[
					'label'       => __( 'Attributes to Display', 'ggm-commerce' ),
					'type'        => \Elementor\Controls_Manager::SELECT2,
					'multiple'    => true,
					'options'     => $attr_options,
					'description' => __( 'Select which filterable attributes to show. Leave empty to show all.', 'ggm-commerce' ),
					'condition'   => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'attribute_filter_type',
				[
					'label'       => __( 'Select Attribute Display', 'ggm-commerce' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'default'     => 'checkbox',
					'options'     => [
						'checkbox' => __( 'Checkboxes', 'ggm-commerce' ),
						'dropdown' => __( 'Dropdown', 'ggm-commerce' ),
					],
					'description' => __( 'How to display select-type attributes', 'ggm-commerce' ),
					'condition'   => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'number_attribute_type',
				[
					'label'     => __( 'Number Attribute Display', 'ggm-commerce' ),
					'type'      => \Elementor\Controls_Manager::SELECT,
					'default'   => 'slider',
					'options'   => [
						'slider' => __( 'Range Slider', 'ggm-commerce' ),
						'inputs' => __( 'Min/Max Inputs', 'ggm-commerce' ),
					],
					'condition' => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'show_attribute_units',
				[
					'label'        => __( 'Show Units', 'ggm-commerce' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'label_on'     => __( 'Show', 'ggm-commerce' ),
					'label_off'    => __( 'Hide', 'ggm-commerce' ),
					'return_value' => 'yes',
					'default'      => 'yes',
					'description'  => __( 'Display unit suffix (kg, cm, etc.) next to values', 'ggm-commerce' ),
					'condition'    => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'show_attribute_counts',
				[
					'label'        => __( 'Show Product Counts', 'ggm-commerce' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'label_on'     => __( 'Show', 'ggm-commerce' ),
					'label_off'    => __( 'Hide', 'ggm-commerce' ),
					'return_value' => 'yes',
					'default'      => 'yes',
					'description'  => __( 'Show count of products for each option (select attributes only)', 'ggm-commerce' ),
					'condition'    => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'hide_empty_attributes',
				[
					'label'        => __( 'Hide Empty Attribute Values', 'ggm-commerce' ),
					'type'         => \Elementor\Controls_Manager::SWITCHER,
					'label_on'     => __( 'Yes', 'ggm-commerce' ),
					'label_off'    => __( 'No', 'ggm-commerce' ),
					'return_value' => 'yes',
					'default'      => 'yes',
					'description'  => __( 'Hide attribute values with no matching products as filters are applied', 'ggm-commerce' ),
					'condition'    => [
						'show_attribute_filters' => 'yes',
					],
				]
			);

			$this->add_control(
				'attribute_label_display',
				[
					'label'       => __( 'Label Display', 'ggm-commerce' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'default'     => 'name',
					'options'     => [
						'name'      => __( 'Name Only', 'ggm-commerce' ),
						'name_icon' => __( 'Name + Icon', 'ggm-commerce' ),
						'icon'      => __( 'Icon Only', 'ggm-commerce' ),
					],
					'description' => __( 'How to display attribute filter labels (requires icon set on attribute)', 'ggm-commerce' ),
					'condition'   => [
						'show_attribute_filters' => 'yes',
					],
				]
			);
		}

		$this->end_controls_section();
	}
}
