<?php
/**
 * Frontend forms wiring.
 *
 * Thin shim over the already-real {@see \ThemeREX\AutoDealer\Forms\FormRenderer}
 * and {@see \ThemeREX\AutoDealer\Forms\FormHandler}. This class exposes:
 *
 *   - `[trx_ad_inquiry_form]` shortcode for embedding any registered form
 *     in a page or post (Gutenberg shortcode block, Elementor shortcode
 *     widget, etc.).
 *   - Auto-injection on the single-listing template so themes that don't
 *     render the inquiry form sidebar still get a form below the content.
 *
 * Phase 7 owns lead generation and the registered form definitions; this
 * class just makes sure those forms are reachable from a frontend render
 * context without each theme having to hand-wire the renderer.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Frontend;

defined( 'ABSPATH' ) || exit;
use ThemeREX\AutoDealer\Forms\FormRenderer;
use ThemeREX\AutoDealer\PostTypes\VehiclePostType;

/**
 * Stateless frontend-form integration.
 */
final class FrontendForms {

	public const SHORTCODE = 'trx_ad_inquiry_form';

	/**
	 * Register hooks.
	 */
	public function register(): void {
		add_shortcode( self::SHORTCODE, array( $this, 'renderShortcode' ) );

		/**
		 * Allow themes to opt out of the auto-injected form below the
		 * single-listing content. Default: true (auto-injected).
		 *
		 * @param bool $enabled
		 */
		if ( apply_filters( 'trx_ad_auto_inject_inquiry_form', true ) ) {
			add_filter( 'the_content', array( $this, 'maybeAppendInquiryForm' ), 99 );
		}
	}

	/**
	 * Shortcode entry point.
	 *
	 * @param array<string,mixed>|string $atts Shortcode attributes.
	 * @return string
	 */
	public function renderShortcode( $atts = array() ): string {
		$atts = shortcode_atts(
			array(
				'form'    => 'inquiry',
				'post_id' => 0,
				'title'   => '',
			),
			is_array( $atts ) ? $atts : array(),
			self::SHORTCODE
		);

		$post_id = (int) $atts['post_id'];
		if ( $post_id <= 0 && is_singular( VehiclePostType::POST_TYPE ) ) {
			$post_id = (int) get_the_ID();
		}

		// Escaped at the callback's return boundary -- WP outputs shortcode
		// return values directly (wp.org 2026-08 review round 4).
		return wp_kses(
			FormRenderer::render(
				(string) $atts['form'],
				$post_id,
				array(
					'title' => (string) $atts['title'],
				)
			),
			\ThemeREX\AutoDealer\allowedHtml()
		);
	}

	/**
	 * Append an inquiry form to single-listing content when the theme
	 * doesn't already render the sidebar version.
	 *
	 * @param string $content
	 * @return string
	 */
	public function maybeAppendInquiryForm( string $content ): string {
		if ( ! is_singular( VehiclePostType::POST_TYPE ) || ! in_the_loop() || ! is_main_query() ) {
			return $content;
		}

		// The plugin's own single-vehicle.php already renders an inquiry
		// form in the sidebar. Skip when our template is in scope so we
		// don't double-render.
		if ( did_action( 'trx_ad_single_template_loaded' ) ) {
			return $content;
		}

		$form = FormRenderer::render(
			'inquiry',
			(int) get_the_ID(),
			array(
				'title' => __( 'Inquire About This Vehicle', 'trx-auto-dealer' ),
			)
		);

		// Only the plugin's own appended fragment is kses'd -- $content
		// belongs to the theme/other plugins and must pass through as-is.
		return $content . wp_kses( $form, \ThemeREX\AutoDealer\allowedHtml() );
	}
}
