<?php
/**
 * Testing tab — sandbox mode and the test send.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by SettingsPage::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

$texttango_settings = (array) ( $args['settings'] ?? array() );
?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
	<input type="hidden" name="action" value="<?php echo esc_attr( (string) $args['save_action'] ); ?>" />
	<input type="hidden" name="texttango_tab" value="<?php echo esc_attr( (string) $args['tab'] ); ?>" />
	<?php wp_nonce_field( (string) $args['save_nonce'] ); ?>

	<div class="tt-card tt-mb-4">
		<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
			<?php esc_html_e( 'Sandbox / test mode', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'Stops this site spending credits or texting real customers. Leave it on Automatic so a staging copy of a live shop can never message anyone.', 'texttango' ); ?>
		</p>

		<div class="tt-grid tt-grid-cols-1 tt-gap-5 sm:tt-grid-cols-2">
			<div>
				<label class="tt-label" for="texttango-sandbox-mode"><?php esc_html_e( 'Mode', 'texttango' ); ?></label>
				<select class="tt-input" id="texttango-sandbox-mode" name="sandbox_mode">
					<option value="auto" <?php selected( (string) $texttango_settings['sandbox_mode'], 'auto' ); ?>>
						<?php esc_html_e( 'Automatic — on unless the environment is production', 'texttango' ); ?>
					</option>
					<option value="on" <?php selected( (string) $texttango_settings['sandbox_mode'], 'on' ); ?>>
						<?php esc_html_e( 'Always on', 'texttango' ); ?>
					</option>
					<option value="off" <?php selected( (string) $texttango_settings['sandbox_mode'], 'off' ); ?>>
						<?php esc_html_e( 'Off — send everything for real', 'texttango' ); ?>
					</option>
				</select>
				<p class="tt-hint">
					<?php
					printf(
						/* translators: %s: environment type reported by WordPress. */
						esc_html__( 'This site reports its environment as "%s".', 'texttango' ),
						esc_html( (string) ( $args['sandbox_env_note'] ?? '' ) )
					);
					?>
				</p>
			</div>

			<div>
				<label class="tt-label" for="texttango-sandbox-behavior"><?php esc_html_e( 'While sandboxed', 'texttango' ); ?></label>
				<select class="tt-input" id="texttango-sandbox-behavior" name="sandbox_behavior">
					<option value="log_only" <?php selected( (string) $texttango_settings['sandbox_behavior'], 'log_only' ); ?>>
						<?php esc_html_e( 'Log only — never call the API', 'texttango' ); ?>
					</option>
					<option value="redirect" <?php selected( (string) $texttango_settings['sandbox_behavior'], 'redirect' ); ?>>
						<?php esc_html_e( 'Redirect every message to one test number', 'texttango' ); ?>
					</option>
				</select>
				<p class="tt-hint">
					<?php esc_html_e( 'Redirecting really sends, and really costs credits — but only ever to your own number.', 'texttango' ); ?>
				</p>
			</div>
		</div>

		<div class="tt-mt-5">
			<label class="tt-label" for="texttango-sandbox-number"><?php esc_html_e( 'Sandbox test number', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-sandbox-number"
				name="sandbox_test_number"
				type="tel"
				placeholder="+233…"
				value="<?php echo esc_attr( (string) $texttango_settings['sandbox_test_number'] ); ?>" />
			<p class="tt-hint"><?php esc_html_e( 'Required only when redirecting. Stored in E.164.', 'texttango' ); ?></p>
		</div>
	</div>

	<div class="tt-flex tt-flex-wrap tt-items-center tt-gap-3">
		<button type="submit" class="tt-btn tt-btn-primary">
			<?php esc_html_e( 'Save settings', 'texttango' ); ?>
		</button>
	</div>
</form>

<div class="tt-card tt-mb-4 tt-mt-6">
	<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
		<?php esc_html_e( 'Send a test SMS', 'texttango' ); ?>
	</h2>
	<p class="tt-mb-4 tt-text-sm tt-text-slate-500">
		<?php esc_html_e( 'Proves the whole path end to end: token, sender ID, and network.', 'texttango' ); ?>
		<strong><?php esc_html_e( 'This always sends for real and uses credits, even while sandbox mode is on — that is the only way it can prove anything.', 'texttango' ); ?></strong>
	</p>

	<div class="tt-flex tt-flex-wrap tt-items-end tt-gap-3">
		<div class="tt-grow">
			<label class="tt-label" for="texttango-test-phone"><?php esc_html_e( 'Your phone number', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-test-phone"
				type="tel"
				placeholder="+233…"
				data-texttango-test-phone
				value="<?php echo esc_attr( (string) $texttango_settings['test_phone'] ); ?>" />
		</div>
		<button
			type="button"
			class="tt-btn tt-btn-secondary"
			data-texttango-action="send-test-sms"
			data-sandbox="<?php echo esc_attr( ! empty( $args['sandbox_active'] ) ? '1' : '0' ); ?>"
			<?php disabled( empty( $args['has_api_key'] ) ); ?>>
			<?php esc_html_e( 'Send test SMS', 'texttango' ); ?>
		</button>
	</div>
	<p class="tt-text-sm tt-mt-3" data-texttango-test-result aria-live="polite"></p>
</div>
