<?php
/**
 * Public team-survey form template.
 *
 * Loaded from BW_Schema_Survey_Public::render_form() inside an
 * ob_start() buffer. Variables in scope (set by the caller):
 *   - $action_url    string  Form post URL (already esc_url'd)
 *   - $nonce         string  WP nonce value
 *   - $timing_token  string  HMAC-signed render timestamp
 *   - $intro_html    string  Pre-sanitized intro (wp_kses_post)
 *   - $questions     array   Question definitions from BW_Schema_Survey::survey_questions()
 *   - $team_members  array   [ ['id' => 1, 'title' => '...'], ... ]
 *
 * @package BW_AI_Schema_Pro
 * @since 2.2.0
 */

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

// All vars come from the caller — declared for static analysis clarity.
/** @var string $action_url */
/** @var string $nonce */
/** @var string $timing_token */
/** @var string $intro_html */
/** @var array  $questions */
/** @var array  $team_members */
?>
<div class="bw-schema-survey-intro">
	<?php echo $intro_html; // already wp_kses_post sanitized at caller ?>
</div>

<form method="post" action="<?php echo $action_url; // pre-escaped ?>" class="bw-schema-survey-form" autocomplete="on">

	<input type="hidden" name="<?php echo esc_attr( BW_Schema_Survey_Public::NONCE_FIELD ); ?>" value="<?php echo esc_attr( $nonce ); ?>" />
	<input type="hidden" name="<?php echo esc_attr( BW_Schema_Survey_Public::TIMING_FIELD ); ?>" value="<?php echo esc_attr( $timing_token ); ?>" />

	<?php /* Honeypot — hidden via CSS. Bots fill URL-like fields by name. */ ?>
	<div class="bw-schema-survey-hp" aria-hidden="true" style="position:absolute;left:-10000px;top:-10000px;height:0;width:0;overflow:hidden;">
		<label><?php esc_html_e( 'Leave this field blank', 'bw-ai-schema-pro' ); ?>
			<input type="text" name="<?php echo esc_attr( BW_Schema_Survey_Public::HONEYPOT_FIELD ); ?>" value="" autocomplete="off" tabindex="-1" />
		</label>
	</div>

	<fieldset class="bw-schema-survey-fieldset">
		<legend><?php esc_html_e( 'Who are you?', 'bw-ai-schema-pro' ); ?></legend>

		<p class="bw-schema-survey-field">
			<label for="bw-schema-survey-target"><?php esc_html_e( 'Select your name', 'bw-ai-schema-pro' ); ?> <span class="bw-required">*</span></label>
			<select id="bw-schema-survey-target" name="target_post_id" required>
				<option value=""><?php esc_html_e( '— Choose —', 'bw-ai-schema-pro' ); ?></option>
				<option value="__new__"><?php esc_html_e( "I'm not listed — add me", 'bw-ai-schema-pro' ); ?></option>
				<?php foreach ( $team_members as $tm ) : ?>
					<option value="<?php echo esc_attr( $tm['id'] ); ?>"><?php echo esc_html( $tm['title'] ); ?></option>
				<?php endforeach; ?>
			</select>
			<small class="bw-schema-survey-help"><?php esc_html_e( 'If your name is in the list, pick it. Otherwise choose "I\'m not listed."', 'bw-ai-schema-pro' ); ?></small>
		</p>

		<p class="bw-schema-survey-field">
			<label for="bw-schema-survey-submitter-email"><?php esc_html_e( 'Your email (so we can follow up if needed)', 'bw-ai-schema-pro' ); ?></label>
			<input type="email" id="bw-schema-survey-submitter-email" name="submitter_email" value="" />
			<small class="bw-schema-survey-help"><?php esc_html_e( 'Not published. Used only by the moderator if they have questions about your submission.', 'bw-ai-schema-pro' ); ?></small>
		</p>
	</fieldset>

	<fieldset class="bw-schema-survey-fieldset">
		<legend><?php esc_html_e( 'About you', 'bw-ai-schema-pro' ); ?></legend>

		<?php foreach ( $questions as $q ) : ?>
			<?php
			$id       = 'bw-schema-survey-' . sanitize_html_class( $q['key'] );
			$name     = $q['key'];
			$label    = $q['label'];
			$type     = $q['type'];
			$required = ! empty( $q['required'] );
			$help     = isset( $q['help'] ) ? $q['help'] : '';
			$words_max = isset( $q['words_max'] ) ? (int) $q['words_max'] : 0;
			?>
			<p class="bw-schema-survey-field bw-schema-survey-field-<?php echo esc_attr( $type ); ?>">
				<label for="<?php echo esc_attr( $id ); ?>">
					<?php echo esc_html( $label ); ?>
					<?php if ( $required ) : ?>
						<span class="bw-required">*</span>
					<?php endif; ?>
				</label>

				<?php if ( 'textarea' === $type || 'url-list' === $type ) : ?>
					<textarea
						id="<?php echo esc_attr( $id ); ?>"
						name="<?php echo esc_attr( $name ); ?>"
						rows="<?php echo 'url-list' === $type ? 4 : ( $words_max > 100 ? 6 : 4 ); ?>"
						<?php if ( $required ) {
							echo 'required';
						} ?>
					></textarea>
				<?php else : ?>
					<input
						type="<?php echo esc_attr( $type ); ?>"
						id="<?php echo esc_attr( $id ); ?>"
						name="<?php echo esc_attr( $name ); ?>"
						value=""
						<?php if ( $required ) {
							echo 'required';
						} ?>
						<?php if ( 'number' === $type ) {
							echo ' min="0" step="1"';
						} ?>
					/>
				<?php endif; ?>

				<?php if ( $help ) : ?>
					<small class="bw-schema-survey-help">
						<?php echo esc_html( $help ); ?>
						<?php if ( $words_max ) : ?>
							<?php
							/* translators: %d: max recommended word count */
							echo ' ' . esc_html( sprintf( __( 'Suggested: about %d words.', 'bw-ai-schema-pro' ), $words_max ) );
							?>
						<?php endif; ?>
					</small>
				<?php endif; ?>
			</p>
		<?php endforeach; ?>
	</fieldset>

	<p class="bw-schema-survey-submit">
		<button type="submit"><?php esc_html_e( 'Submit', 'bw-ai-schema-pro' ); ?></button>
	</p>

	<p class="bw-schema-survey-footnote">
		<small><?php esc_html_e( 'A moderator will review your submission before anything is published on the website.', 'bw-ai-schema-pro' ); ?></small>
	</p>
</form>
