<?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_Text_Shadow;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  

class Sifency_Widget_Team_Contact_Form extends Widget_Base {
	use Sifency_Animation_Controls;

	public function get_name() {
		return 'sifency-team-contact-form';
	}

	public function get_title() {
		return esc_html__( 'Member Contact Form', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-meta-data';
	}

	public function get_categories() {
		return [ 'sifency-team-components' ];
	}

	public function get_keywords() {
		return [ 'team', 'contact', 'form', 'cf7', 'meta', 'dynamic', 'sifency' ];
	}

	protected function register_controls() {
		// --- Content Tab: Form Settings ---
		$this->start_controls_section(
			'section_form_content',
			[
				'label' => esc_html__( 'Form Settings', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'title_text',
			[
				'label'       => esc_html__( 'Title', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => [ 'active' => true ],
				'default'     => esc_html__( 'Contact Me', 'sifency-addons' ),
				'placeholder' => esc_html__( 'Enter a title', 'sifency-addons' ),
				'description' => esc_html__( 'Tip: Use the "Post Title" dynamic tag to include the team member\'s name.', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'title_tag',
			[
				'label'   => esc_html__( 'Title HTML Tag', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'h1'   => 'H1',
					'h2'   => 'H2',
					'h3'   => 'H3',
					'h4'   => 'H4',
					'h5'   => 'H5',
					'h6'   => 'H6',
					'div'  => 'div',
					'span' => 'span',
					'p'    => 'p',
				],
				'default' => 'h3',
			]
		);
		
		// Check if Contact Form 7 is active
		if ( ! function_exists( 'wpcf7_contact_form_shortcode' ) ) {
			$this->add_control(
				'cf7_not_installed_notice',
				[
					'type'            => Controls_Manager::RAW_HTML,
					'raw'             => '<div class="elementor-panel-alert elementor-panel-alert-danger">' . esc_html__( 'Contact Form 7 is not installed or activated.', 'sifency-addons' ) . '</div>',
					'content_classes' => 'elementor-control-field-description',
				]
			);
		} else {
			$this->add_control(
				'placeholder_form_id',
				[
					'label'       => esc_html__( 'Placeholder Form', 'sifency-addons' ),
					'type'        => Controls_Manager::SELECT,
					'options'     => sifency_get_cf7_forms(), // Reuse your helper function!
					'description' => esc_html__( 'This form will be shown in the editor for styling purposes if the team member has no form selected.', 'sifency-addons' ),
				]
			);
		}

		$this->end_controls_section();

		// --- Style Tab: Title Styling ---
		$this->start_controls_section(
			'section_title_style',
			[
				'label'     => esc_html__( 'Title', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'title_text!' => '' ],
			]
		);

		$this->add_control(
			'title_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-contact-form-title' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .sifency-contact-form-title',
			]
		);

		$this->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			[
				'name'     => 'title_text_shadow',
				'selector' => '{{WRAPPER}} .sifency-contact-form-title',
			]
		);
		
		$this->add_responsive_control(
			'title_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-contact-form-title' => 'margin-bottom: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->end_controls_section();
		
		$this->add_sifency_animation_controls();
	}

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

		if ( ! function_exists( 'wpcf7_contact_form_shortcode' ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="elementor-alert elementor-alert-danger">' . esc_html__( 'Error: Contact Form 7 plugin is not active.', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$real_form_id = sifency_get_field( 'team_contactform7' );
		$form_to_render_id = '';
		
		if ( ! empty( $real_form_id ) && 'none' !== $real_form_id ) {
			$form_to_render_id = $real_form_id;
		} 
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() && ! empty( $settings['placeholder_form_id'] ) && 'none' !== $settings['placeholder_form_id'] ) {
			$form_to_render_id = $settings['placeholder_form_id'];
		}

		if ( empty( $form_to_render_id ) ) {
			if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
				echo '<div class="elementor-alert elementor-alert-info">' . esc_html__( 'No contact form selected for this team member. Select a placeholder form in the controls to see a preview.', 'sifency-addons' ) . '</div>';
			}
			return;
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );

		$this->add_render_attribute( 'title', 'class', 'sifency-contact-form-title' );
		
		?>
		<div class="sifency-team-contact-form-wrapper" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<?php if ( ! empty( $settings['title_text'] ) ) : ?>
				<<?php echo tag_escape( $settings['title_tag'] ); ?> <?php echo wp_kses ( $this->get_render_attribute_string( 'title' ), [] ); ?>>
					<?php echo esc_html( $settings['title_text'] ); ?>
				</<?php echo tag_escape( $settings['title_tag'] ); ?>>
			<?php endif; ?>

			<div class="sifency-contact-form-inner">
				<?php echo do_shortcode( '[contact-form-7 id="' . esc_attr( $form_to_render_id ) . '"]' ); ?>
			</div>
		</div>
		<?php
	}

}