<?php
/**
 * Team Survey settings.
 *
 * Variables in scope:
 *   - $slug          string
 *   - $token         string
 *   - $public_url    string
 *   - $enabled       int
 *   - $expires_at    int
 *   - $opens_at      int
 *   - $days_left     int
 *   - $intro_html    string
 *   - $notify_email  string
 *   - $notify_on     int
 *   - $grant_editors int
 *   - $tpt           string|false
 *
 * @package BW_AI_Schema_Pro
 * @since 2.2.0
 */

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

/** @var string $slug */
/** @var string $token */
/** @var string $public_url */
/** @var int    $enabled */
/** @var int    $expires_at */
/** @var int    $opens_at */
/** @var int    $days_left */
/** @var string $intro_html */
/** @var string $notify_email */
/** @var int    $notify_on */
/** @var int    $grant_editors */
/** @var string|false $tpt */

$is_open = BW_Schema_Survey::is_open();
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Team Survey settings', 'bw-ai-schema-pro' ); ?></h1>

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

	<?php if ( ! $tpt ) : ?>
		<div class="notice notice-warning"><p>
			<?php esc_html_e( "You haven't mapped a team post type yet. Team Survey can be configured, but it won't accept submissions until you do.", 'bw-ai-schema-pro' ); ?>
			<a href="<?php echo esc_url( admin_url( 'options-general.php?page=bw-ai-schema-settings' ) ); ?>"><?php esc_html_e( 'Map a team post type →', 'bw-ai-schema-pro' ); ?></a>
		</p></div>
	<?php endif; ?>

	<h2><?php esc_html_e( 'Survey window', 'bw-ai-schema-pro' ); ?></h2>

	<form method="post">
		<?php wp_nonce_field( BW_Schema_Survey_Admin::NONCE_SETTINGS, '_bw_schema_survey_settings_nonce' ); ?>

		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Status', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<?php if ( $is_open ) : ?>
						<strong style="color:#1a7f37;"><?php esc_html_e( 'Open', 'bw-ai-schema-pro' ); ?></strong>
						—
						<?php
						/* translators: %d: number of days remaining */
						printf( esc_html__( '%d day(s) remaining (closes %s).', 'bw-ai-schema-pro' ), (int) $days_left, esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $expires_at ) ) );
						?>
					<?php else : ?>
						<strong><?php esc_html_e( 'Closed', 'bw-ai-schema-pro' ); ?></strong>
					<?php endif; ?>
				</td>
			</tr>
			<tr>
				<th>
					<label for="bw-schema-open-days">
						<?php echo $is_open
							? esc_html__( 'Renew for', 'bw-ai-schema-pro' )
							: esc_html__( 'Open for', 'bw-ai-schema-pro' ); ?>
					</label>
				</th>
				<td>
					<select id="bw-schema-open-days" name="open_days">
						<?php for ( $d = 1; $d <= BW_Schema_Survey::MAX_OPEN_DAYS; $d++ ) : ?>
							<option value="<?php echo (int) $d; ?>" <?php selected( $d, 7 ); ?>>
								<?php
								/* translators: %d: number of days */
								printf( esc_html( _n( '%d day', '%d days', $d, 'bw-ai-schema-pro' ) ), $d );
								?>
							</option>
						<?php endfor; ?>
					</select>

					<button type="submit" name="bw_schema_survey_settings_submit" value="<?php echo $is_open ? 'renew' : 'open'; ?>" class="button button-primary">
						<?php echo $is_open ? esc_html__( 'Renew', 'bw-ai-schema-pro' ) : esc_html__( 'Open survey', 'bw-ai-schema-pro' ); ?>
					</button>

					<?php if ( $is_open ) : ?>
						<button type="submit" name="bw_schema_survey_settings_submit" value="close" class="button">
							<?php esc_html_e( 'Close now', 'bw-ai-schema-pro' ); ?>
						</button>
					<?php endif; ?>

					<p class="description">
						<?php
						/* translators: %d: maximum days */
						printf( esc_html__( 'Maximum %d days at a time. Renew when needed.', 'bw-ai-schema-pro' ), (int) BW_Schema_Survey::MAX_OPEN_DAYS );
						?>
					</p>
				</td>
			</tr>
		</table>
	</form>

	<h2><?php esc_html_e( 'Survey URL', 'bw-ai-schema-pro' ); ?></h2>

	<form method="post">
		<?php wp_nonce_field( BW_Schema_Survey_Admin::NONCE_SETTINGS, '_bw_schema_survey_settings_nonce' ); ?>

		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Public link', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<code style="word-break: break-all; display: block; padding: 6px; background: #f6f7f7;">
						<?php echo esc_html( $public_url ); ?>
					</code>
					<p class="description">
						<?php esc_html_e( 'Anyone with this link can submit while the survey is open. The page is blocked from search engines.', 'bw-ai-schema-pro' ); ?>
					</p>
				</td>
			</tr>
			<tr>
				<th><?php esc_html_e( 'Regenerate URL', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<button type="submit" name="bw_schema_survey_settings_submit" value="rotate_token" class="button" onclick="return confirm('<?php echo esc_js( __( 'Generate a new URL? The old one will stop working immediately.', 'bw-ai-schema-pro' ) ); ?>');">
						<?php esc_html_e( 'Regenerate', 'bw-ai-schema-pro' ); ?>
					</button>
				</td>
			</tr>
			<tr>
				<th><label for="bw-schema-survey-slug"><?php esc_html_e( 'URL slug', 'bw-ai-schema-pro' ); ?></label></th>
				<td>
					<input type="text" id="bw-schema-survey-slug" name="survey_slug" value="<?php echo esc_attr( $slug ); ?>" />
					<p class="description"><?php esc_html_e( 'Becomes the first path segment. Default: bw-team-survey.', 'bw-ai-schema-pro' ); ?></p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Notifications', 'bw-ai-schema-pro' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><label for="bw-schema-survey-notify-enabled"><?php esc_html_e( 'Email on new submission', 'bw-ai-schema-pro' ); ?></label></th>
				<td>
					<label>
						<input type="checkbox" id="bw-schema-survey-notify-enabled" name="notify_enabled" value="1" <?php checked( $notify_on ); ?> />
						<?php esc_html_e( 'Send me an email when a new submission arrives', 'bw-ai-schema-pro' ); ?>
					</label>
				</td>
			</tr>
			<tr>
				<th><label for="bw-schema-survey-notify-email"><?php esc_html_e( 'Notification address', 'bw-ai-schema-pro' ); ?></label></th>
				<td>
					<input type="email" id="bw-schema-survey-notify-email" name="notify_email" value="<?php echo esc_attr( $notify_email ); ?>" class="regular-text" />
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Who can moderate', 'bw-ai-schema-pro' ); ?></h2>
		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Editors', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<label>
						<input type="checkbox" name="grant_editors" value="1" <?php checked( $grant_editors ); ?> />
						<?php esc_html_e( 'Also let Editors moderate team surveys', 'bw-ai-schema-pro' ); ?>
					</label>
					<p class="description"><?php esc_html_e( 'Administrators can always moderate.', 'bw-ai-schema-pro' ); ?></p>
				</td>
			</tr>
		</table>

		<h2><?php esc_html_e( 'Public-form intro', 'bw-ai-schema-pro' ); ?></h2>
		<?php
		wp_editor( $intro_html, 'bw_schema_survey_intro', array(
			'textarea_name' => 'intro_html',
			'textarea_rows' => 6,
			'media_buttons' => false,
		) );
		?>

		<p style="margin-top: 1em;">
			<button type="submit" name="bw_schema_survey_settings_submit" value="save" class="button button-primary">
				<?php esc_html_e( 'Save settings', 'bw-ai-schema-pro' ); ?>
			</button>
		</p>
	</form>

	<hr style="margin: 2em 0;" />

	<h2 style="color: #b32d2e;"><?php esc_html_e( 'Reset', 'bw-ai-schema-pro' ); ?></h2>
	<p class="description">
		<?php esc_html_e( 'For testing. These actions are immediate and cannot be undone.', 'bw-ai-schema-pro' ); ?>
	</p>

	<?php
	$pending_count = BW_Schema_Survey_Store::count_by_status( 'all' );
	?>

	<form method="post" style="margin-top: 1em;">
		<?php wp_nonce_field( BW_Schema_Survey_Admin::NONCE_SETTINGS, '_bw_schema_survey_settings_nonce' ); ?>

		<table class="form-table">
			<tr>
				<th><?php esc_html_e( 'Delete all submissions', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<button
						type="submit"
						name="bw_schema_survey_settings_submit"
						value="clear_submissions"
						class="button"
						onclick="return confirm('<?php echo esc_js( __( 'Delete ALL survey submissions? Settings and the URL stay the same. This cannot be undone.', 'bw-ai-schema-pro' ) ); ?>');"
					>
						<?php
						/* translators: %d: number of submissions currently stored */
						printf( esc_html__( 'Delete all %d submission(s)', 'bw-ai-schema-pro' ), (int) $pending_count );
						?>
					</button>
					<p class="description">
						<?php esc_html_e( 'Empties the responses table. Survey window, URL, and settings are unchanged. Use between test runs when you want to re-submit but keep the same setup.', 'bw-ai-schema-pro' ); ?>
					</p>
				</td>
			</tr>

			<tr>
				<th><?php esc_html_e( 'Full reset', 'bw-ai-schema-pro' ); ?></th>
				<td>
					<button
						type="submit"
						name="bw_schema_survey_settings_submit"
						value="full_reset"
						class="button button-secondary"
						style="color: #b32d2e; border-color: #b32d2e;"
						onclick="return confirm('<?php echo esc_js( __( 'Reset the entire Team Survey feature? This will: close the window, delete all submissions, generate a new URL, and restore settings to defaults. The old URL will stop working immediately. This cannot be undone. Continue?', 'bw-ai-schema-pro' ) ); ?>');"
					>
						<?php esc_html_e( 'Reset everything', 'bw-ai-schema-pro' ); ?>
					</button>
					<p class="description">
						<?php esc_html_e( 'Closes the window, deletes all submissions, rotates the URL, and resets every Team Survey setting back to defaults (intro text, notifications, capability grant, slug). Schema data already published onto team posts is NOT touched — edit those posts directly to clear.', 'bw-ai-schema-pro' ); ?>
					</p>
				</td>
			</tr>
		</table>
	</form>
</div>
