<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Master_Accordion extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-master-accordion',
			'title'          => 'Sifency Master Accordion',
			'icon'           => 'eicon-accordion',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'sifency-master-accordion-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'faq', 'accordion', 'toggle', 'questions' ),
		);
	}

	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Accordion Items', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'question',
			array(
				'label'       => esc_html__( 'Question', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'How does your process work?', 'sifency-addons' ),
				'label_block' => true,
			)
		);

		$repeater->add_control(
			'answer',
			array(
				'label'   => esc_html__( 'Answer', 'sifency-addons' ),
				'type'    => Controls_Manager::WYSIWYG,
				'default' => esc_html__( 'We begin with discovery and strategy to understand your business, audience, and goals. From there, we develop brand concepts and turn it into a complete, usable brand system.', 'sifency-addons' ),
			)
		);

		$repeater->add_control(
			'active',
			array(
				'label'        => esc_html__( 'Active by Default', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'default'      => 'no',
			)
		);

		$this->add_control(
			'accordion_items',
			array(
				'label'       => esc_html__( 'FAQ Items', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'question' => 'How does your process work?',
						'active'   => 'yes',
					),
					array( 'question' => 'What do you actually deliver?' ),
					array( 'question' => 'How involved do we need to be?' ),
				),
				'title_field' => '{{{ question }}}',
			)
		);

		$this->add_control(
			'close_others',
			array(
				'label'       => esc_html__( 'Close Others When Opening', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Only one item can be opened at a time.',
				'separator'   => 'before',
			)
		);

		$this->end_controls_section();

		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Accordion Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'bg_color',
			array(
				'label'     => esc_html__( 'Item Background', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#f5f5f5',
				'selectors' => array( '{{WRAPPER}} .sifency-acc-item' => 'background-color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'question_color',
			array(
				'label'     => esc_html__( 'Question Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#000000',
				'selectors' => array( '{{WRAPPER}} .sifency-acc-title' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'answer_color',
			array(
				'label'     => esc_html__( 'Answer Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#666666',
				'selectors' => array( '{{WRAPPER}} .sifency-acc-content' => 'color: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'icon_color',
			array(
				'label'     => esc_html__( 'Icon Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#ff3c00',
				'selectors' => array( '{{WRAPPER}} .sifency-acc-icon svg' => 'fill: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( '{{WRAPPER}} .sifency-acc-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				'default'    => array(
					'top'    => '16',
					'bottom' => '16',
					'left'   => '16',
					'right'  => '16',
					'unit'   => 'px',
				),
			)
		);

		$this->add_responsive_control(
			'padding',
			array(
				'label'      => esc_html__( 'Padding', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em', '%' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-acc-header' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					'{{WRAPPER}} .sifency-acc-content-inner' => 'padding: 0 {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'default'    => array(
					'top'    => '24',
					'bottom' => '24',
					'left'   => '30',
					'right'  => '30',
					'unit'   => 'px',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'question_typography',
				'label'    => esc_html__( 'Question Typography', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-acc-title',
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'answer_typography',
				'label'    => esc_html__( 'Answer Typography', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-acc-content',
			)
		);

		$this->end_controls_section();
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$items      = $settings['accordion_items'];

		if ( empty( $items ) ) {
			return;
		}

		$close_others = $settings['close_others'] === 'yes' ? 'true' : 'false';

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-accordion-wrapper" data-close-others="<?php echo esc_attr( $close_others ); ?>">
			
			<?php
			foreach ( $items as $index => $item ) :
				$is_active = $item['active'] === 'yes' ? 'sifency-is-active' : '';
				?>
				<div class="sifency-acc-item <?php echo esc_attr( $is_active ); ?>" data-index="<?php echo esc_attr( $index ); ?>">
					
					<!-- Header (Clickable) -->
					<div class="sifency-acc-header">
						<h4 class="sifency-acc-title"><?php echo esc_html( $item['question'] ); ?></h4>
						
						<div class="sifency-acc-icon">
							<!-- Custom Plus/Minus SVG -->
							<svg viewBox="0 0 24 24" fill="currentColor">
								<rect x="11" y="4" width="2" height="16" class="sifency-icon-v" />
								<rect x="4" y="11" width="16" height="2" class="sifency-icon-h" />
							</svg>
						</div>
					</div>

					<!-- Content (Hidden initially via JS) -->
					<div class="sifency-acc-content">
						<div class="sifency-acc-content-inner">
							<?php echo wp_kses_post( $item['answer'] ); ?>
						</div>
					</div>

				</div>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
