<?php
/**
 * Shared Table Styling Controls
 *
 * Provides reusable Elementor controls for table styling.
 * Use this class to register consistent table controls across widgets.
 *
 * @package GGM_Commerce
 * @since 1.0.0
 */

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

/**
 * Class GGM_Table_Styling_Controls
 *
 * Registers standardized table styling controls for Elementor widgets.
 *
 * Supports three tiers:
 * - 'basic': Table border, header styling, cell padding
 * - 'standard': Basic + row backgrounds, hover, cell typography
 * - 'advanced': Standard + shadows, row borders, hover effects
 */
class GGM_Table_Styling_Controls {

	/**
	 * Register table styling 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(
			'table_selector'  => '.ggmc-table',
			'header_selector' => 'thead th',
			'row_selector'    => 'tbody tr',
			'cell_selector'   => 'th, td',
			'prefix'          => 'table',
			'section_id'      => 'table_style',
			'section_label'   => __( 'Table Style', 'ggm-commerce' ),
			'tier'            => 'standard', // 'basic', 'standard', or 'advanced'.
			'start_section'   => true,
			'end_section'     => true,
			'condition'       => array(),
		);

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

		// 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
			);
		}

		// Basic tier controls (always included).
		self::register_basic_controls( $widget, $args );

		// Standard tier controls.
		if ( in_array( $args['tier'], array( 'standard', 'advanced' ), true ) ) {
			self::register_standard_controls( $widget, $args );
		}

		// Advanced tier controls.
		if ( 'advanced' === $args['tier'] ) {
			self::register_advanced_controls( $widget, $args );
		}

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

	/**
	 * Register basic tier controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_basic_controls( $widget, $args ) {
		$table_selector  = '{{WRAPPER}} ' . $args['table_selector'];
		$header_selector = $table_selector . ' ' . $args['header_selector'];
		$cell_selector   = $table_selector . ' ' . $args['cell_selector'];

		// Table border.
		$widget->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => $args['prefix'] . '_border',
				'selector' => $table_selector,
			)
		);

		// Table border radius.
		$widget->add_responsive_control(
			$args['prefix'] . '_border_radius',
			array(
				'label'      => __( 'Border Radius', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					$table_selector => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}; overflow: hidden;',
				),
			)
		);

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

		$widget->add_control(
			$args['prefix'] . '_header_background',
			array(
				'label'     => __( 'Header Background', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					$header_selector => 'background-color: {{VALUE}};',
				),
			)
		);

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

		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_header_typography',
				'selector' => $header_selector,
			)
		);

		// Cell padding.
		$widget->add_control(
			$args['prefix'] . '_cell_heading',
			array(
				'label'     => __( 'Cells', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_cell_padding',
			array(
				'label'      => __( 'Cell Padding', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					$cell_selector => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);
	}

	/**
	 * Register standard tier controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_standard_controls( $widget, $args ) {
		$table_selector = '{{WRAPPER}} ' . $args['table_selector'];
		$row_selector   = $table_selector . ' ' . $args['row_selector'];
		$cell_selector  = $table_selector . ' ' . $args['cell_selector'];

		// Row styling.
		$widget->add_control(
			$args['prefix'] . '_row_heading',
			array(
				'label'     => __( 'Rows', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_control(
			$args['prefix'] . '_row_background',
			array(
				'label'     => __( 'Row Background', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					$row_selector => 'background-color: {{VALUE}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_row_alt_background',
			array(
				'label'     => __( 'Alternate Row Background', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					$row_selector . ':nth-child(even)' => 'background-color: {{VALUE}};',
				),
			)
		);

		$widget->add_control(
			$args['prefix'] . '_row_hover_background',
			array(
				'label'     => __( 'Row Hover Background', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					$row_selector . ':hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		// Cell typography and colors.
		$widget->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => $args['prefix'] . '_cell_typography',
				'label'    => __( 'Cell Typography', 'ggm-commerce' ),
				'selector' => $table_selector . ' tbody td',
			)
		);

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

		// Cell borders.
		$widget->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => $args['prefix'] . '_cell_border',
				'label'    => __( 'Cell Border', 'ggm-commerce' ),
				'selector' => $cell_selector,
			)
		);
	}

	/**
	 * Register advanced tier controls.
	 *
	 * @param \Elementor\Widget_Base $widget The widget instance.
	 * @param array                  $args   Configuration options.
	 *
	 * @return void
	 */
	private static function register_advanced_controls( $widget, $args ) {
		$table_selector = '{{WRAPPER}} ' . $args['table_selector'];
		$row_selector   = $table_selector . ' ' . $args['row_selector'];

		// Table box shadow.
		$widget->add_control(
			$args['prefix'] . '_advanced_heading',
			array(
				'label'     => __( 'Advanced', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $args['prefix'] . '_box_shadow',
				'selector' => $table_selector,
			)
		);

		// Row-level styling.
		$widget->add_group_control(
			\Elementor\Group_Control_Border::get_type(),
			array(
				'name'     => $args['prefix'] . '_row_border',
				'label'    => __( 'Row Border', 'ggm-commerce' ),
				'selector' => $row_selector,
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => $args['prefix'] . '_row_box_shadow',
				'label'    => __( 'Row Shadow', 'ggm-commerce' ),
				'selector' => $row_selector,
			)
		);

		// Hover effects.
		$widget->add_control(
			$args['prefix'] . '_hover_heading',
			array(
				'label'     => __( 'Hover Effects', 'ggm-commerce' ),
				'type'      => \Elementor\Controls_Manager::HEADING,
				'separator' => 'before',
			)
		);

		$widget->add_control(
			$args['prefix'] . '_hover_lift',
			array(
				'label'        => __( 'Enable Hover Lift', 'ggm-commerce' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => __( 'Yes', 'ggm-commerce' ),
				'label_off'    => __( 'No', 'ggm-commerce' ),
				'return_value' => 'yes',
				'default'      => '',
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_hover_lift_offset',
			array(
				'label'      => __( 'Lift Offset', '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(
					$row_selector . ':hover' => 'transform: translateY(-{{SIZE}}{{UNIT}});',
				),
				'condition'  => array(
					$args['prefix'] . '_hover_lift' => 'yes',
				),
			)
		);

		$widget->add_responsive_control(
			$args['prefix'] . '_hover_transition',
			array(
				'label'      => __( 'Transition Duration', 'ggm-commerce' ),
				'type'       => \Elementor\Controls_Manager::SLIDER,
				'size_units' => array( 'ms' ),
				'range'      => array(
					'ms' => array(
						'min'  => 0,
						'max'  => 1000,
						'step' => 50,
					),
				),
				'default'    => array(
					'unit' => 'ms',
					'size' => 300,
				),
				'selectors'  => array(
					$row_selector => 'transition: all {{SIZE}}{{UNIT}} ease;',
				),
			)
		);

		$widget->add_group_control(
			\Elementor\Group_Control_Box_Shadow::get_type(),
			array(
				'name'      => $args['prefix'] . '_row_hover_box_shadow',
				'label'     => __( 'Row Hover Shadow', 'ggm-commerce' ),
				'selector'  => $row_selector . ':hover',
				'condition' => array(
					$args['prefix'] . '_hover_lift' => 'yes',
				),
			)
		);
	}

	/**
	 * Get default styles for table controls.
	 *
	 * @param string $prefix Control name prefix.
	 * @param string $tier   Control tier ('basic', 'standard', 'advanced').
	 * @param array  $design_system Design system values.
	 *
	 * @return array Default control values.
	 */
	public static function get_defaults( $prefix = 'table', $tier = 'standard', $design_system = array() ) {
		// Default design system values if not provided.
		$defaults_design = array(
			'colors' => array(
				'text_primary'   => '#0f172a',
				'text_secondary' => '#475569',
				'surface'        => '#f8fafc',
				'border'         => '#e2e8f0',
			),
		);

		$design_system = wp_parse_args( $design_system, $defaults_design );

		// Basic tier defaults.
		$defaults = array(
			$prefix . '_border_border'       => 'solid',
			$prefix . '_border_width'        => array(
				'top'      => '1',
				'right'    => '1',
				'bottom'   => '1',
				'left'     => '1',
				'unit'     => 'px',
				'isLinked' => true,
			),
			$prefix . '_border_color'        => $design_system['colors']['border'],
			$prefix . '_border_radius'       => array(
				'top'      => '8',
				'right'    => '8',
				'bottom'   => '8',
				'left'     => '8',
				'unit'     => 'px',
				'isLinked' => true,
			),
			$prefix . '_header_background'   => $design_system['colors']['surface'],
			$prefix . '_header_text_color'   => $design_system['colors']['text_primary'],
			$prefix . '_cell_padding'        => array(
				'top'      => '12',
				'right'    => '16',
				'bottom'   => '12',
				'left'     => '16',
				'unit'     => 'px',
				'isLinked' => false,
			),
		);

		// Standard tier defaults.
		if ( in_array( $tier, array( 'standard', 'advanced' ), true ) ) {
			$defaults[ $prefix . '_row_background' ]       = '#ffffff';
			$defaults[ $prefix . '_row_alt_background' ]   = $design_system['colors']['surface'];
			$defaults[ $prefix . '_row_hover_background' ] = '#f1f5f9';
			$defaults[ $prefix . '_cell_text_color' ]      = $design_system['colors']['text_secondary'];
			$defaults[ $prefix . '_cell_border_border' ]   = 'solid';
			$defaults[ $prefix . '_cell_border_width' ]    = array(
				'top'      => '0',
				'right'    => '0',
				'bottom'   => '1',
				'left'     => '0',
				'unit'     => 'px',
				'isLinked' => false,
			);
			$defaults[ $prefix . '_cell_border_color' ]    = $design_system['colors']['border'];
		}

		// Advanced tier defaults.
		if ( 'advanced' === $tier ) {
			$defaults[ $prefix . '_hover_lift' ]        = '';
			$defaults[ $prefix . '_hover_lift_offset' ] = array(
				'unit' => 'px',
				'size' => 2,
			);
			$defaults[ $prefix . '_hover_transition' ]  = array(
				'unit' => 'ms',
				'size' => 300,
			);
		}

		return $defaults;
	}
}
