<?php
/**
 * File Path: includes/blocks/class-mbs-block-arc-signup.php
 *
 * Block: mbs/arc-signup (PRO)
 *
 * Renders the ARC reviewer self-signup form for a specific book page.
 * FREE installs return an HTML comment — zero visible output.
 *
 * Sprint F-027: ARC Signup Block.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Blocks
 * @since      1.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Block_Arc_Signup { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render callback for the mbs/arc-signup block.
	 *
	 * @param array $attributes Block attributes from the editor.
	 * @return string Rendered HTML or HTML comment.
	 */
	public static function render( array $attributes ): string {
		if ( ! mbs_is_pro() ) {
			return '<!-- MBS: arc-signup requires PRO license -->';
		}

		// Resolve post ID: explicit attribute > loop context.
		$post_id = isset( $attributes['bookId'] ) && $attributes['bookId'] > 0
			? absint( $attributes['bookId'] )
			: (int) get_the_ID();

		if ( ! $post_id ) {
			return '';
		}

		$book = get_post( $post_id );
		if ( ! $book || MBS_POST_TYPE !== $book->post_type ) {
			return '';
		}

		// Merge attribute defaults.
		$atts = wp_parse_args( $attributes, array(
			'title'            => __( 'Apply to Review This Book', 'my-book-showroom' ),
			'buttonLabel'      => __( 'Submit Application', 'my-book-showroom' ),
			'successMessage'   => __( "Thank you! We'll be in touch.", 'my-book-showroom' ),
			'showMessageField' => true,
			'consentText'      => MBS_ESP_Arc_Signup::DEFAULT_CONSENT_TEXT,
			'requireConsent'   => true,
		) );

		// CAPTCHA settings for front-end localization.
		$captcha_provider  = (string) get_option( 'mbs_setting_email_captcha_provider', '' );
		$captcha_site_key  = '';
		if ( '' !== $captcha_provider ) {
			$key_option       = 'mbs_setting_email_captcha_' . $captcha_provider . '_site_key';
			$captcha_site_key = (string) mbs_decrypt_option( $key_option );
		}

		// Enqueue front-end assets -- wp_enqueue_* deduplicates on multi-block pages.
		if ( ! is_admin() ) {
			$css_ver = defined( 'MBS_DEV_MODE' ) && MBS_DEV_MODE
				? (string) filemtime( MBS_DIR . 'assets/css/mbs-arc-signup.css' )
				: MBS_VERSION;
			wp_enqueue_style( 'mbs-arc-signup', MBS_ASSETS_URL . 'css/mbs-arc-signup.css', array(), $css_ver );
			wp_enqueue_script( 'mbs-arc-signup', MBS_ASSETS_URL . 'js/mbs-arc-signup.js', array( 'wp-i18n' ), MBS_VERSION, true );
			wp_set_script_translations( 'mbs-arc-signup', 'my-book-showroom', MBS_DIR . 'languages' );

			// Enqueue the appropriate CAPTCHA provider script when configured.
			if ( 'recaptcha_v3' === $captcha_provider && '' !== $captcha_site_key ) {
				wp_enqueue_script(
					'mbs-recaptcha-v3',
					'https://www.google.com/recaptcha/api.js?render=' . rawurlencode( $captcha_site_key ),
					array(),
					null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- external CDN script; version is implicit in the CDN URL
					true
				);
			} elseif ( 'hcaptcha' === $captcha_provider && '' !== $captcha_site_key ) {
				wp_enqueue_script( 'mbs-hcaptcha', 'https://js.hcaptcha.com/1/api.js', array(), null, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- external CDN script
			} elseif ( 'turnstile' === $captcha_provider && '' !== $captcha_site_key ) {
				wp_enqueue_script( 'mbs-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js', array(), null, false ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion,PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent -- external CDN resource required for ARC signup block functionality; no self-hosted alternative available
			}
		}

		ob_start();
		include MBS_TEMPLATES_DIR . 'partials/arc-signup-form.php';
		return (string) ob_get_clean();
	}
}
