<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Digital_Product_Faq extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() { return 'sifency-digital-product-faq'; }
	public function get_title() { return esc_html__( 'Product FAQ', 'sifency-addons' ); }
	public function get_icon() { return 'eicon-meta-data'; }
	public function get_categories() { return [ 'sifency-digital-components' ]; }
	public function get_keywords() { return [ 'faq', 'accordion', 'toggle', 'question', 'answer', 'meta', 'sifency' ]; }
    public function get_style_depends() { return [ 'sifency-digital-style' ]; }
    public function get_script_depends() { return [ 'sifency-digital-script' ]; }
	protected function register_controls() {
		// --- Content Tab: Accordion Settings ---
		$this->start_controls_section( 'section_accordion_content', [ 'label' => esc_html__( 'Accordion', 'sifency-addons' ) ] );
		$this->add_control( 'icon', [ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'type' => Controls_Manager::ICONS, 'default' => [ 'value' => 'fas fa-chevron-down', 'library' => 'fa-solid' ] ] );
		$this->add_control( 'allow_multiple_open', [ 'label' => esc_html__( 'Multiple Open Items', 'sifency-addons' ), 'type' => Controls_Manager::SWITCHER, 'label_on' => 'Yes', 'label_off' => 'No', 'return_value' => 'yes', 'default' => 'no', 'description' => 'Allow multiple FAQ items to be open at the same time.' ] );
		$this->end_controls_section();

		// --- Style Tab: General ---
		$this->start_controls_section( 'section_general_style', [ 'label' => esc_html__( 'Accordion', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_group_control( Group_Control_Border::get_type(), [ 'name' => 'accordion_border', 'selector' => '{{WRAPPER}} .sifency-faq-accordion' ] );
		$this->add_responsive_control( 'accordion_radius', [ 'label' => 'Border Radius', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-faq-accordion' => 'border-radius: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ] ] );
		$this->end_controls_section();

		// --- Style Tab: Question ---
		$this->start_controls_section( 'section_question_style', [ 'label' => esc_html__( 'Question', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'question_typography', 'selector' => '{{WRAPPER}} .sifency-faq-question-text' ] );
		$this->start_controls_tabs( 'question_style_tabs' );
		$this->start_controls_tab('question_normal', ['label' => 'Normal']);
		$this->add_control( 'question_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-question' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'question_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-question' => 'background-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->start_controls_tab('question_active', ['label' => 'Active / Hover']);
		$this->add_control( 'question_active_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-item:hover .sifency-faq-question, {{WRAPPER}} .sifency-faq-item--active .sifency-faq-question' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'question_active_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-item:hover .sifency-faq-question, {{WRAPPER}} .sifency-faq-item--active .sifency-faq-question' => 'background-color: {{VALUE}};' ] ] );
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->add_responsive_control( 'question_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-faq-question' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ], 'separator' => 'before' ] );
		$this->end_controls_section();

		// --- Style Tab: Answer ---
		$this->start_controls_section( 'section_answer_style', [ 'label' => esc_html__( 'Answer', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE ] );
		$this->add_group_control( Group_Control_Typography::get_type(), [ 'name' => 'answer_typography', 'selector' => '{{WRAPPER}} .sifency-faq-answer' ] );
		$this->add_control( 'answer_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-answer' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'answer_bg_color', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-answer' => 'background-color: {{VALUE}};' ] ] );
		$this->add_responsive_control( 'answer_padding', [ 'label' => 'Padding', 'type' => Controls_Manager::DIMENSIONS, 'selectors' => [ '{{WRAPPER}} .sifency-faq-answer' => 'padding: {{TOP}}px {{RIGHT}}px {{BOTTOM}}px {{LEFT}}px;' ] ] );
		$this->end_controls_section();

		// --- Style Tab: Icon ---
		$this->start_controls_section( 'section_icon_style', [ 'label' => esc_html__( 'Icon', 'sifency-addons' ), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => [ 'icon[value]!' => '' ] ] );
		$this->add_control( 'icon_color', [ 'label' => 'Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-icon' => 'color: {{VALUE}};' ] ] );
		$this->add_control( 'icon_active_color', [ 'label' => 'Active Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-faq-item--active .sifency-faq-icon' => 'color: {{VALUE}};' ] ] );
		$this->add_responsive_control( 'icon_size', [ 'label' => 'Size', 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .sifency-faq-icon i' => 'font-size: {{SIZE}}px;' ] ] );
		$this->end_controls_section();
		
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		$faq_list = sifency_get_field( 'digital_faq' );

		if ( empty( $faq_list ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				$faq_list = [
					[ 'faq_question' => 'What is the refund policy?', 'faq_answer' => 'We offer a 30-day money-back guarantee on all our products, no questions asked.' ],
					[ 'faq_question' => 'Is this a one-time purchase?', 'faq_answer' => 'Yes, this product is a one-time purchase. You will receive lifetime updates.' ],
					[ 'faq_question' => 'Do you offer customer support?', 'faq_answer' => 'Absolutely! We provide premium customer support 24/7. You can reach us via email or our support ticket system.' ],
				];
			} else {
				return; // Render nothing on live site if empty
			}
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$this->add_render_attribute( 'accordion', 'class', 'sifency-faq-accordion' );
		$this->add_render_attribute( 'accordion', 'data-allow-multiple', $settings['allow_multiple_open'] );

		?>
		<div <?php echo $animation_attrs; // phpcs:ignore ?>>
			<div <?php echo wp_kses ( $this->get_render_attribute_string( 'accordion' ), [] ); ?>>
				<?php
				foreach ( $faq_list as $index => $item ) :
					if ( empty( $item['faq_question'] ) ) continue;

					$item_key = 'item_' . $index;
					$this->add_render_attribute( $item_key, 'class', 'sifency-faq-item' );
				?>
					<div <?php echo wp_kses ( $this->get_render_attribute_string( $item_key ), [] ); ?>>
						<div class="sifency-faq-question" role="button" tabindex="0">
							<span class="sifency-faq-question-text"><?php echo esc_html( $item['faq_question'] ); ?></span>
							<?php if ( ! empty( $settings['icon']['value'] ) ) : ?>
								<span class="sifency-faq-icon"><?php Icons_Manager::render_icon( $settings['icon'], [ 'aria-hidden' => 'true' ] ); ?></span>
							<?php endif; ?>
						</div>
						<div class="sifency-faq-answer">
							<?php 
							$answer = isset( $item['faq_answer'] ) ? $item['faq_answer'] : '';
							echo wp_kses_post( wpautop( $answer ) );
							?>
						</div>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}
}