<?php
/**
 * Team Survey — manual admin-entry form ("Path 1" from the spec).
 *
 * Variables in scope:
 *   - $tpt          string Team CPT slug
 *   - $questions    array  Survey question definitions
 *   - $team_members array  [ ['id' => 1, 'title' => '...'], ... ]
 *   - $nonce        string
 *
 * @package BW_AI_Schema_Pro
 * @since 2.2.0
 */

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

/** @var string $tpt */
/** @var array  $questions */
/** @var array  $team_members */
/** @var string $nonce */

$queue_url = BW_Schema_Survey_Admin::url_queue();
?>
<div class="wrap">
	<h1 class="wp-heading-inline"><?php esc_html_e( 'Add team info', 'bw-ai-schema-pro' ); ?></h1>
	<a href="<?php echo esc_url( $queue_url ); ?>" class="page-title-action"><?php esc_html_e( '← Back to queue', 'bw-ai-schema-pro' ); ?></a>
	<hr class="wp-header-end" />

	<?php settings_errors( 'bw_schema_survey' ); ?>

	<p class="description">
		<?php esc_html_e( "Fill out info for a team member directly — no link, no waiting. After you save, you'll be taken to the schema-review screen where you can refine the structure before publishing.", 'bw-ai-schema-pro' ); ?>
	</p>

	<form method="post">
		<input type="hidden" name="_bw_schema_survey_add_nonce" value="<?php echo esc_attr( $nonce ); ?>" />

		<h2><?php esc_html_e( 'Team member', 'bw-ai-schema-pro' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="bw-schema-add-target"><?php esc_html_e( 'Which team member?', 'bw-ai-schema-pro' ); ?></label></th>
				<td>
					<select id="bw-schema-add-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( 'Add a new team member (creates a draft)', 'bw-ai-schema-pro' ); ?></option>
						<?php foreach ( $team_members as $tm ) : ?>
							<option value="<?php echo (int) $tm['id']; ?>"><?php echo esc_html( $tm['title'] ); ?></option>
						<?php endforeach; ?>
					</select>
					<p class="description">
						<?php
						/* translators: %s: team CPT slug */
						printf( esc_html__( 'Pulls from the "%s" post type. Pick an existing team member to update their info, or add a new one (you can promote them to a real team CPT entry afterward from triage).', 'bw-ai-schema-pro' ), esc_html( $tpt ) );
						?>
					</p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Their info', 'bw-ai-schema-pro' ); ?></h2>
		<table class="form-table">
			<?php foreach ( $questions as $q ) : ?>
				<?php
				$id        = 'bw-schema-add-' . 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;
				?>
				<tr>
					<th>
						<label for="<?php echo esc_attr( $id ); ?>">
							<?php echo esc_html( $label ); ?>
							<?php if ( $required ) : ?>
								<span style="color:#d63638;">*</span>
							<?php endif; ?>
						</label>
					</th>
					<td>
						<?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 : 3 ); ?>"
								class="large-text"
							></textarea>
						<?php else : ?>
							<input
								type="<?php echo esc_attr( $type ); ?>"
								id="<?php echo esc_attr( $id ); ?>"
								name="<?php echo esc_attr( $name ); ?>"
								value=""
								class="<?php echo 'number' === $type ? '' : 'regular-text'; ?>"
								<?php if ( 'number' === $type ) {
									echo ' min="0" step="1"';
								} ?>
							/>
						<?php endif; ?>
						<?php if ( $help ) : ?>
							<p class="description">
								<?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; ?>
							</p>
						<?php endif; ?>
					</td>
				</tr>
			<?php endforeach; ?>
		</table>

		<p>
			<button type="submit" name="bw_schema_survey_add_submit" value="1" class="button button-primary">
				<?php esc_html_e( 'Save and go to schema review', 'bw-ai-schema-pro' ); ?>
			</button>
			<a href="<?php echo esc_url( $queue_url ); ?>" class="button"><?php esc_html_e( 'Cancel', 'bw-ai-schema-pro' ); ?></a>
		</p>
	</form>
</div>
