<?php
/**
 * Recaptcha Input.
 *
 * @package tripzzy
 */

namespace Tripzzy\Core\Forms\Inputs;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

use Tripzzy\Core\Http\Request;
use Tripzzy\Core\Helpers\Settings;
use Tripzzy\Core\Helpers\EscapeHelper;
use Tripzzy\Core\Helpers\Recaptcha as RecaptchaHelper;

if ( ! class_exists( 'Tripzzy\Core\Forms\Inputs\Recaptcha' ) ) {
	/**
	 * Recaptcha Input.
	 *
	 * @since 1.4.0
	 */
	class Recaptcha {

		/**
		 * Field Type.
		 *
		 * @var string
		 */
		protected static $field_type = 'recaptcha';

		/**
		 * Default CSS class for recaptcha input.
		 *
		 * @var string
		 */
		protected static $default_class = 'g-recaptcha-response-input';

		/**
		 * Init Attributes defined in individual input class.
		 *
		 * @since 1.4.0
		 */
		public static function init_attribute() {
			add_filter( 'tripzzy_filter_field_attributes', array( 'Tripzzy\Core\Forms\Inputs\Recaptcha', 'register_attribute' ) );
		}

		/**
		 * Callback to init attributes.
		 *
		 * @param array $attribute Field data along with attributes.
		 * @since 1.4.0
		 */
		public static function register_attribute( $attribute ) {
			// Only register reCAPTCHA field type if it's configured.
			if ( ! RecaptchaHelper::is_configured() ) {
				return $attribute;
			}

			$attribute[ self::$field_type ] = array(
				'label' => __( 'reCAPTCHA', 'tripzzy' ),
				'class' => 'Tripzzy\Core\Forms\Inputs\Recaptcha',
				'attr'  => array(
					'hide_properties' => array( 'enabled', 'label', 'placeholder', 'required' ),
					'required'        => true,
					'fallback_title'  => __( 'Recaptcha', 'tripzzy' ),
				),
			);
			return $attribute;
		}

		/**
		 * Render.
		 *
		 * @param array $field   Field arguments.
		 * @param bool  $display Display field flag. whether return or display.
		 *
		 * @since 1.4.0
		 */
		public static function render( $field = array(), $display = true ) {
			// If recaptcha is not configured, do not render reCAPTCHA.
			if ( ! RecaptchaHelper::is_configured() || Request::is( 'admin' ) ) {
				return;
			}

			$settings = Settings::get();
			$site_key = $settings['recaptcha_site_key'] ?? '';

			// Enqueue Google reCAPTCHA API script for v3.
			// The 'render' parameter with site key automatically loads the invisible badge.
			wp_enqueue_script(
				'google-recaptcha-api',
				'https://www.google.com/recaptcha/api.js?render=' . esc_attr( $site_key ),
				array(),
				TRIPZZY_VERSION,
				true
			);

			$field_id    = $field['id'] ?? uniqid( 'recaptcha_' );
			$field_name  = $field['name'] ?? 'g-recaptcha-response';
			$field_class = $field['class'] ?? self::$default_class;

			ob_start();
			?>
			<input type="hidden" name="<?php echo esc_attr( $field_name ); ?>" id="<?php echo esc_attr( $field_id ); ?>" class="<?php echo esc_attr( $field_class ); ?>" data-recaptcha-input="true" value="">
			<?php
			$output = ob_get_clean();

			if ( ! $display ) {
				return $output;
			}

			$allowed_html = EscapeHelper::get_allowed_html();
			echo wp_kses( $output, $allowed_html );
		}
	}
}
