<?php
/**
 * Team Surveys — inline setup UI (since 2.2.2).
 *
 * Replaces the earlier "go to settings" link. Lets the admin either:
 *   - Pick a suggested team CPT from the scored list, or
 *   - Pick any non-core public CPT from a fallback dropdown, or
 *   - Proceed without a team CPT (workflow runs without auto-publishing
 *     into team-member post meta).
 *
 * Loaded by:
 *   - BW_Schema_Survey_Admin::render_queue_page()
 *   - BW_Schema_Survey_Admin::render_add_page()
 *
 * Variables in scope:
 *   - $suggestions  array  Scored matches from suggest_team_post_types()
 *   - $all_options  array  All eligible CPTs for fallback dropdown
 *   - $nonce        string Nonce for the setup form
 *
 * @package BW_AI_Schema_Pro
 * @since 2.2.2
 */

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

/** @var array  $suggestions */
/** @var array  $all_options */
/** @var string $nonce */
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Team Surveys — Setup', 'bw-ai-schema-pro' ); ?></h1>

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

	<p style="max-width: 720px;">
		<?php esc_html_e( 'Team Surveys needs to know which post type holds your team members on this site. Pick one below and the workflow will use it to match submissions to existing team-member entries and publish schema data automatically.', 'bw-ai-schema-pro' ); ?>
	</p>

	<form method="post" style="max-width: 720px;">
		<input type="hidden" name="_bw_schema_survey_setup_nonce" value="<?php echo esc_attr( $nonce ); ?>" />

		<?php if ( ! empty( $suggestions ) ) : ?>
			<h2 style="margin-top: 1.5em;"><?php esc_html_e( 'Likely matches', 'bw-ai-schema-pro' ); ?></h2>
			<p class="description">
				<?php esc_html_e( "These post types look like they might hold your team members. Pick one if it's right.", 'bw-ai-schema-pro' ); ?>
			</p>
			<table class="widefat striped" style="max-width: 720px;">
				<tbody>
					<?php foreach ( $suggestions as $i => $s ) : ?>
						<tr>
							<td style="width: 1%; white-space: nowrap;">
								<label style="display: block; padding: 4px 0;">
									<input
										type="radio"
										name="team_post_type"
										value="<?php echo esc_attr( $s['slug'] ); ?>"
										<?php checked( 0 === $i ); ?>
									/>
								</label>
							</td>
							<td>
								<strong><?php echo esc_html( $s['label'] ); ?></strong>
								<small style="color: #6c7781; margin-left: 8px;">
									<code><?php echo esc_html( $s['slug'] ); ?></code>
								</small>
							</td>
							<td style="width: 1%; white-space: nowrap; color: #6c7781;">
								<?php
								/* translators: %d: number of published posts in this CPT */
								printf( esc_html( _n( '%d published', '%d published', $s['count'], 'bw-ai-schema-pro' ) ), (int) $s['count'] );
								?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>

		<?php if ( ! empty( $all_options ) ) : ?>
			<h2 style="margin-top: 1.5em;">
				<?php
				echo empty( $suggestions )
					? esc_html__( 'Pick a post type', 'bw-ai-schema-pro' )
					: esc_html__( "Don't see it? Pick from all post types:", 'bw-ai-schema-pro' );
				?>
			</h2>
			<p>
				<select name="team_post_type_fallback" style="min-width: 280px;">
					<option value=""><?php esc_html_e( '— Choose another post type —', 'bw-ai-schema-pro' ); ?></option>
					<?php foreach ( $all_options as $o ) : ?>
						<option value="<?php echo esc_attr( $o['slug'] ); ?>">
							<?php echo esc_html( $o['label'] ); ?> (<?php echo esc_html( $o['slug'] ); ?>) — <?php echo (int) $o['count']; ?> published
						</option>
					<?php endforeach; ?>
				</select>
				<small class="description" style="display: block; margin-top: 4px;">
					<?php esc_html_e( "Selecting from this dropdown overrides the radio choice above.", 'bw-ai-schema-pro' ); ?>
				</small>
			</p>
		<?php endif; ?>

		<p style="margin-top: 1.5em;">
			<button
				type="submit"
				name="bw_schema_survey_setup_submit"
				value="connect"
				class="button button-primary"
				<?php disabled( empty( $suggestions ) && empty( $all_options ) ); ?>
			>
				<?php esc_html_e( 'Connect this post type', 'bw-ai-schema-pro' ); ?>
			</button>
		</p>

		<?php if ( empty( $suggestions ) && empty( $all_options ) ) : ?>
			<div class="notice notice-info inline" style="max-width: 720px; margin-top: 1em;">
				<p>
					<?php esc_html_e( "We couldn't find any non-core post types on this site. You'll need to register a team-member CPT (via your theme, a plugin like Custom Post Type UI, or code) before you can use the full Team Surveys workflow.", 'bw-ai-schema-pro' ); ?>
				</p>
			</div>
		<?php endif; ?>
	</form>

	<hr style="margin: 2em 0; max-width: 720px;" />

	<h2><?php esc_html_e( 'Or proceed without a team post type', 'bw-ai-schema-pro' ); ?></h2>
	<div class="notice notice-warning inline" style="max-width: 720px;">
		<p>
			<strong><?php esc_html_e( 'Heads up:', 'bw-ai-schema-pro' ); ?></strong>
			<?php esc_html_e( "Without a team post type, the survey can still collect and review submissions, but it can't automatically associate them with team-member pages or publish schema. You can connect a post type later — submissions will wait in the holding queue.", 'bw-ai-schema-pro' ); ?>
		</p>
	</div>

	<form method="post" style="max-width: 720px; margin-top: 1em;">
		<input type="hidden" name="_bw_schema_survey_setup_nonce" value="<?php echo esc_attr( $nonce ); ?>" />
		<p>
			<button
				type="submit"
				name="bw_schema_survey_setup_submit"
				value="proceed_without"
				class="button"
				onclick="return confirm('<?php echo esc_js( __( "Proceed without a team post type? You can connect one later from the Team Surveys page.", 'bw-ai-schema-pro' ) ); ?>');"
			>
				<?php esc_html_e( 'Proceed without a team post type', 'bw-ai-schema-pro' ); ?>
			</button>
		</p>
	</form>
</div>
