<?php
/**
 * Contact Form widget — drops one of the configured forms on any page.
 *
 * Like ListingFormWidget but unbound from the vehicle context: the editor can
 * bind it to a specific vehicle ID, leave it unbound, or drop it inside a
 * vehicle template where it picks up the current post automatically.
 * Delegates render to ContactFormBlock so the markup stays in sync with the
 * Gutenberg block.
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Elementor\Widgets;

if ( ! class_exists( '\Elementor\Widget_Base' ) ) {
	return;
}

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;
use ThemeREX\AutoDealer\Blocks\ContactFormBlock;
use ThemeREX\AutoDealer\Forms\FormConfig;

class ContactFormWidget extends Widget_Base {

	public function get_name(): string {
		return 'trx_ad_contact_form';
	}

	public function get_title(): string {
		return __( 'Contact Form', 'trx-auto-dealer' );
	}

	public function get_icon(): string {
		return 'eicon-form-horizontal';
	}

	public function get_categories(): array {
		return array( 'trx-auto-dealer' );
	}

	public function get_keywords(): array {
		return array( 'contact', 'form', 'inquiry', 'callback', 'lead' );
	}

	public function get_style_depends(): array {
		return array( 'trx-ad-frontend', 'trx-ad-widget-listing-form' );
	}

	public function get_script_depends(): array {
		return array( 'trx-ad-frontend', 'trx-ad-widget-listing-form' );
	}

	protected function register_controls(): void {
		$this->start_controls_section(
			'section_content',
			array(
				'label' => __( 'Content', 'trx-auto-dealer' ),
			)
		);

		$this->add_control(
			'form_id',
			array(
				'label'   => __( 'Form', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::SELECT,
				'options' => FormConfig::getOptions(),
				'default' => 'inquiry',
			)
		);

		$this->add_control(
			'title',
			array(
				'label'   => __( 'Title', 'trx-auto-dealer' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
			)
		);

		$this->add_control(
			'vehicle_id',
			array(
				'label'       => __( 'Bind to Vehicle', 'trx-auto-dealer' ),
				'type'        => Controls_Manager::SELECT2,
				'options'     => WidgetHelpers::vehicleOptions( __( '— Auto / unbound —', 'trx-auto-dealer' ) ),
				'default'     => '',
				'description' => __( 'Optional. Leave empty to render unbound, or attach to the current post automatically.', 'trx-auto-dealer' ),
				'label_block' => true,
			)
		);

		$this->end_controls_section();

		// The block renderer wraps the form in .wp-block-trx-auto-dealer-contact-form,
		// which carries a hardcoded frame (background/border/radius/padding/shadow)
		// in frontend.css. These controls expose that frame (tester feedback #12).
		$this->start_controls_section(
			'section_style_wrapper',
			array(
				'label' => __( 'Wrapper', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'wrapper_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'wrapper_border',
				'selector' => '{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form',
			)
		);

		$this->add_responsive_control(
			'wrapper_padding',
			array(
				'label'      => __( 'Padding', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_control(
			'wrapper_radius',
			array(
				'label'      => __( 'Border Radius', 'trx-auto-dealer' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px' ),
				'selectors'  => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'wrapper_shadow',
				'selector' => '{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form',
			)
		);

		$this->add_control(
			'wrapper_frameless',
			array(
				'label'        => __( 'Remove Frame', 'trx-auto-dealer' ),
				'description'  => __( 'Strip the default background, border, padding, and shadow from the form wrapper.', 'trx-auto-dealer' ),
				'type'         => Controls_Manager::SWITCHER,
				'return_value' => 'yes',
				'selectors'    => array(
					'{{WRAPPER}} .wp-block-trx-auto-dealer-contact-form' => 'background: none; border: 0; border-radius: 0; padding: 0; box-shadow: none;',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_title',
			array(
				'label'     => __( 'Title', 'trx-auto-dealer' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'title!' => '' ),
			)
		);

		$this->add_control(
			'title_color',
			array(
				'label'     => __( 'Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__heading' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'title_typography',
				'selector' => '{{WRAPPER}} .trx-ad-form__heading',
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_inputs',
			array(
				'label' => __( 'Inputs', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'input_color',
			array(
				'label'     => __( 'Text Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__input, {{WRAPPER}} .trx-ad-form__textarea, {{WRAPPER}} .trx-ad-form__select' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'input_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__input, {{WRAPPER}} .trx-ad-form__textarea, {{WRAPPER}} .trx-ad-form__select' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'input_border_color',
			array(
				'label'     => __( 'Border Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__input, {{WRAPPER}} .trx-ad-form__textarea, {{WRAPPER}} .trx-ad-form__select' => 'border-color: {{VALUE}};',
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_button',
			array(
				'label' => __( 'Button', 'trx-auto-dealer' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'btn_color',
			array(
				'label'     => __( 'Text Color', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__submit' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'btn_bg',
			array(
				'label'     => __( 'Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__submit' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'btn_hover_bg',
			array(
				'label'     => __( 'Hover Background', 'trx-auto-dealer' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .trx-ad-form__submit:hover' => 'background-color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'btn_typography',
				'selector' => '{{WRAPPER}} .trx-ad-form__submit',
			)
		);

		$this->end_controls_section();
	}

	protected function render(): void {
		$settings = $this->get_settings_for_display();
		$id       = (int) ( $settings['vehicle_id'] ?? 0 );

		// In editor without an explicit ID, surface a preview vehicle so the
		// form isn't context-empty.
		if ( $id <= 0 && ElementorPreviewContext::isEditor() ) {
			$id = ElementorPreviewContext::resolveVehicleId();
		}

		$attributes = array(
			'formId'    => (string) ( $settings['form_id'] ?? 'inquiry' ),
			'title'     => (string) ( $settings['title'] ?? '' ),
			'vehicleId' => $id,
		);

		echo wp_kses( ( new ContactFormBlock() )->render( $attributes ), \ThemeREX\AutoDealer\allowedHtml() );
	}
}
