<?php
/**
 * Native Elementor widget wrapping the shared visit-form core.
 *
 * This is an OPTIONAL integration: the class is only declared and registered
 * when Elementor is active, so the plugin has no hard dependency on Elementor
 * and functions fully without it (optional-integration rule). The
 * widget wraps the same WrenchPress_Visit_Form::render() core as the shortcode
 * and block, recording 'elementor' as the lead source.
 *
 * Assets are declared to Elementor via get_style_depends()/get_script_depends()
 * rather than enqueued inside render(); Elementor enqueues the registered
 * handles when the widget is on the page (RESEARCH Supporting stack).
 *
 * @package WrenchPress
 */

defined( 'ABSPATH' ) || exit;

/**
 * Register the Elementor widget on the widgets-register hook, but only when
 * Elementor is present. The class itself extends an Elementor base class, so it
 * is defined lazily inside the callback to avoid referencing a missing parent
 * when Elementor is not installed.
 *
 * @return void
 */
function wrenchpress_register_elementor_widget() {
	if ( ! did_action( 'elementor/loaded' ) || ! class_exists( '\Elementor\Widget_Base' ) ) {
		return;
	}

	/**
	 * Class WrenchPress_Elementor_Widget
	 */
	class WrenchPress_Elementor_Widget extends \Elementor\Widget_Base {

		/**
		 * Machine name used by Elementor.
		 *
		 * @return string
		 */
		public function get_name() {
			return 'wrenchpress_visit_form';
		}

		/**
		 * Title shown in the Elementor panel.
		 *
		 * @return string
		 */
		public function get_title() {
			return __( 'PitDesk Visit Form', 'pitdesk-repair-crm' );
		}

		/**
		 * Panel icon.
		 *
		 * @return string
		 */
		public function get_icon() {
			return 'eicon-form-horizontal';
		}

		/**
		 * Panel categories.
		 *
		 * @return string[]
		 */
		public function get_categories() {
			return array( 'general' );
		}

		/**
		 * Keywords for the Elementor search.
		 *
		 * @return string[]
		 */
		public function get_keywords() {
			return array( 'pitdesk', 'visit', 'form', 'crm', 'request' );
		}

		/**
		 * Declare the form style handle so Elementor enqueues it for this widget.
		 * The structural CSS is also inlined once per request by the render core,
		 * so the form is styled even if a handle is absent; declaring it is the
		 * Elementor-correct path and harmless.
		 *
		 * @return string[]
		 */
		public function get_style_depends() {
			return array( WrenchPress_Visit_Form::ASSET_HANDLE );
		}

		/**
		 * Declare the form script handle so Elementor enqueues it for this widget.
		 * Do NOT call wp_enqueue_* inside render() (RESEARCH Supporting stack).
		 *
		 * @return string[]
		 */
		public function get_script_depends() {
			return array( WrenchPress_Visit_Form::ASSET_HANDLE );
		}

		/**
		 * Render the widget. Elementor's Widget_Base::render() ECHOES its output
		 * (unlike the shortcode, which returns a string), so echo the shared core
		 * markup with the elementor source recorded on the lead.
		 *
		 * @return void
		 */
		protected function render() {
			echo WrenchPress_Visit_Form::render( array( 'source' => 'elementor' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render() returns fully escaped, self-contained form markup.
		}
	}

	\Elementor\Plugin::instance()->widgets_manager->register( new WrenchPress_Elementor_Widget() );
}

/**
 * Ensure the form's style/script handles are registered on wp_enqueue_scripts so
 * Elementor can enqueue them via the widget's declared dependencies.
 *
 * @return void
 */
function wrenchpress_register_visit_form_assets() {
	wp_register_style(
		WrenchPress_Visit_Form::ASSET_HANDLE,
		WRENCHPRESS_URL . 'assets/css/visit-form.css',
		array(),
		'0.2.0'
	);
	wp_register_script(
		WrenchPress_Visit_Form::ASSET_HANDLE,
		WRENCHPRESS_URL . 'assets/js/visit-form.js',
		array(),
		'0.2.0',
		true
	);
}
