<?php
/**
 * File Path: templates/partials/arc-signup-form.php
 *
 * ARC Signup Form partial -- mbs/arc-signup block render output.
 *
 * Variables in scope (provided by MBS_Block_Arc_Signup::render()):
 *   int    $post_id
 *   array  $atts            (title, buttonLabel, successMessage, showMessageField,
 *                            consentText, requireConsent)
 *   string $captcha_provider ('', 'recaptcha_v3', 'hcaptcha', 'turnstile')
 *   string $captcha_site_key
 *
 * PRO gate is enforced in MBS_Block_Arc_Signup::render() before this file loads.
 *
 * @package    My_Book_Showroom
 * @subpackage Templates/Partials
 * @since      1.2.0
 */

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

$uid = 'mbs-arc-' . (int) $post_id; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
?>
<div class="mbs-arc-signup" data-post-id="<?php echo (int) $post_id; ?>">
	<h3 class="mbs-arc-signup__title"><?php echo esc_html( $atts['title'] ); ?></h3>
	<form class="mbs-arc-signup__form" novalidate>
		<?php wp_nonce_field( 'mbs_arc_apply_' . $post_id, 'mbs_arc_nonce' ); ?>
		<?php
		// Pass block attributes to JS via a data element.
		$js_data = array( // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variable; not a global
			'postId'          => (int) $post_id,
			'successMessage'  => esc_js( $atts['successMessage'] ),
			'captchaProvider' => esc_js( $captcha_provider ),
			'captchaSiteKey'  => esc_js( $captcha_site_key ),
			'restBase'        => esc_url_raw( rest_url( 'mbs/v1/arc/apply' ) ),
			'restNonce'       => wp_create_nonce( 'wp_rest' ),
		);
		?>
		<script>
		window.MbsArcSignupData = window.MbsArcSignupData || {};
		window.MbsArcSignupData[<?php echo (int) $post_id; ?>] = <?php echo wp_json_encode( $js_data ); ?>;
		</script>

		<?php /* Honeypot: hidden from humans, filled by bots. */ ?>
		<div style="display:none" aria-hidden="true">
			<input type="text" name="mbs_hp" value="" tabindex="-1" autocomplete="off">
		</div>

		<div class="mbs-arc-signup__field">
			<label for="<?php echo esc_attr( $uid ); ?>-name">
				<?php esc_html_e( 'Your Name', 'my-book-showroom' ); ?>
			</label>
			<input type="text"
			       id="<?php echo esc_attr( $uid ); ?>-name"
			       name="mbs_arc_name"
			       required
			       autocomplete="name">
		</div>

		<div class="mbs-arc-signup__field">
			<label for="<?php echo esc_attr( $uid ); ?>-email">
				<?php esc_html_e( 'Email Address', 'my-book-showroom' ); ?>
			</label>
			<input type="email"
			       id="<?php echo esc_attr( $uid ); ?>-email"
			       name="mbs_arc_email"
			       required
			       autocomplete="email">
		</div>

		<?php if ( ! empty( $atts['showMessageField'] ) ) : ?>
		<div class="mbs-arc-signup__field">
			<label for="<?php echo esc_attr( $uid ); ?>-message">
				<?php esc_html_e( 'Tell us about yourself (optional)', 'my-book-showroom' ); ?>
			</label>
			<textarea id="<?php echo esc_attr( $uid ); ?>-message"
			          name="mbs_arc_message"
			          rows="4"></textarea>
		</div>
		<?php endif; ?>

		<?php if ( ! empty( $atts['requireConsent'] ) && '' !== $atts['consentText'] ) : ?>
		<div class="mbs-arc-signup__field mbs-arc-signup__consent">
			<label>
				<input type="checkbox" name="mbs_arc_consent" required>
				<?php echo esc_html( $atts['consentText'] ); ?>
			</label>
		</div>
		<?php endif; ?>

		<?php /* CAPTCHA widget injected here by front-end JS. */ ?>
		<div class="mbs-arc-signup__captcha" id="<?php echo esc_attr( $uid ); ?>-captcha"></div>

		<button type="submit" class="mbs-arc-signup__submit button">
			<?php echo esc_html( $atts['buttonLabel'] ); ?>
		</button>

		<div class="mbs-arc-signup__result" role="alert" aria-live="polite"></div>
	</form>
</div>
