<?php
/**
 * Native DIVI module wrapping the shared visit-form core.
 *
 * This is an OPTIONAL integration: the module class extends DIVI's
 * ET_Builder_Module and is only defined and registered when the Divi Builder is
 * active, so the plugin has no hard dependency on DIVI and runs fully without it
 * (optional-integration rule). The module wraps the same
 * WrenchPress_Visit_Form::render() core as the shortcode and block, recording
 * 'divi' as the lead source.
 *
 * DIVI modules render LATE (DIVI 5 lazy-loads modules after wp_head), so asset
 * enqueue uses the did_action('wp_enqueue_scripts') guard (RESEARCH Pattern 2);
 * the structural CSS is additionally inlined once per request by the render
 * core, so styling is timing-independent.
 *
 * @package WrenchPress
 */

defined( 'ABSPATH' ) || exit;

/**
 * Define and register the DIVI module, but only when the Divi Builder is active.
 * The class extends ET_Builder_Module, so it is declared lazily inside this
 * callback to avoid referencing a missing parent when DIVI is absent.
 *
 * @return void
 */
function wrenchpress_register_divi_module() {
	if ( ! class_exists( 'ET_Builder_Module' ) ) {
		return;
	}

	/**
	 * Class WrenchPress_Divi_Module
	 */
	class WrenchPress_Divi_Module extends ET_Builder_Module {

		/**
		 * Module slug.
		 *
		 * @var string
		 */
		public $slug = 'wrenchpress_visit_form';

		/**
		 * Visual Builder support flag.
		 *
		 * @var string
		 */
		public $vb_support = 'on';

		/**
		 * Initialize the module metadata.
		 *
		 * @return void
		 */
		public function init() {
			$this->name = esc_html__( 'PitDesk Visit Form', 'pitdesk-repair-crm' );
		}

		/**
		 * The module exposes no bespoke fields: the form is self-contained.
		 *
		 * @return array
		 */
		public function get_fields() {
			return array();
		}

		/**
		 * Render the module output. DIVI module render RETURNS its markup (it does
		 * not echo). Assets are enqueued through the did_action-guarded late path
		 * so they load under DIVI 5 lazy-loading; the structural CSS is inlined by
		 * the render core regardless.
		 *
		 * @param array  $attrs       Module attributes.
		 * @param string $content     Inner content.
		 * @param string $render_slug Render slug.
		 * @return string
		 */
		public function render( $attrs, $content, $render_slug ) {
			unset( $attrs, $content, $render_slug );

			WrenchPress_Visit_Form::enqueue_assets();

			return WrenchPress_Visit_Form::render( array( 'source' => 'divi' ) );
		}
	}

	new WrenchPress_Divi_Module();
}
