<?php
/**
 * Sign-in tab — passwordless login and two-factor.
 *
 * @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( 'Sign in by phone', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php
			printf(
				/* translators: %s: the shortcode, e.g. [texttango_phone_login]. */
				esc_html__( 'Lets customers sign in with a code sent by SMS instead of a password. Place the form anywhere with %s, or show it on the WordPress login screen.', 'texttango' ),
				'<code class="tt-rounded tt-bg-slate-100 tt-px-1 tt-py-0.5 tt-text-xs">' . esc_html( (string) $args['otp_shortcode'] ) . '</code>'
			);
			?>
		</p>

		<label class="tt-mb-3 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="otp_login_enabled" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['otp_login_enabled'] ) ); ?> />
			<span><?php esc_html_e( 'Enable passwordless sign-in by phone', 'texttango' ); ?></span>
		</label>

		<label class="tt-mb-3 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="otp_on_wp_login" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['otp_on_wp_login'] ) ); ?> />
			<span><?php esc_html_e( 'Also show the phone form on the WordPress login screen', 'texttango' ); ?></span>
		</label>

		<label class="tt-mb-5 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="otp_allow_registration" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['otp_allow_registration'] ) ); ?> />
			<span>
				<?php esc_html_e( 'Create an account when an unrecognised number verifies a code', 'texttango' ); ?>
				<span class="tt-hint tt-block">
					<?php esc_html_e( 'When off, unknown numbers get the same reply as known ones but no code is sent and no account is made.', 'texttango' ); ?>
				</span>
			</span>
		</label>

		<div class="tt-mb-5 tt-grid tt-grid-cols-1 tt-gap-5 sm:tt-grid-cols-2">
			<div>
				<label class="tt-label" for="texttango-otp-role"><?php esc_html_e( 'Role for new accounts', 'texttango' ); ?></label>
				<select class="tt-input" id="texttango-otp-role" name="otp_registration_role">
					<option value=""><?php esc_html_e( 'Use the WordPress default', 'texttango' ); ?></option>
					<?php foreach ( (array) $args['roles'] as $texttango_role => $texttango_role_label ) : ?>
						<?php
						if ( ! \TextTango\Otp\UserRepository::is_safe_self_registration_role( (string) $texttango_role ) ) :
							continue;
endif;
						?>
						<option value="<?php echo esc_attr( (string) $texttango_role ); ?>"
							<?php selected( (string) $texttango_settings['otp_registration_role'], (string) $texttango_role ); ?>>
							<?php echo esc_html( (string) $texttango_role_label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
			</div>

			<div>
				<label class="tt-label" for="texttango-otp-ttl"><?php esc_html_e( 'Code lifetime (seconds)', 'texttango' ); ?></label>
				<input class="tt-input" id="texttango-otp-ttl" name="otp_ttl" type="number" min="60" max="900" step="30"
					value="<?php echo esc_attr( (string) $texttango_settings['otp_ttl'] ); ?>" />
				<p class="tt-hint"><?php esc_html_e( 'Between 60 and 900.', 'texttango' ); ?></p>
			</div>
		</div>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-otp-template"><?php esc_html_e( 'Code message', 'texttango' ); ?></label>
			<textarea class="tt-input" id="texttango-otp-template" name="otp_template" rows="2"
				placeholder="<?php echo esc_attr( (string) $args['otp_default_tpl'] ); ?>"><?php echo esc_textarea( (string) $texttango_settings['otp_template'] ); ?></textarea>
			<p class="tt-hint">
				<?php esc_html_e( 'Must include {otp_code}. Also available: {store_name}, {minutes}, {site_url}. Leave blank for the default.', 'texttango' ); ?>
			</p>
		</div>

		<h3 class="tt-m-0 tt-mb-3 tt-mt-6 tt-p-0 tt-text-sm tt-font-semibold tt-uppercase tt-tracking-wide tt-text-slate-500">
			<?php esc_html_e( 'Abuse limits', 'texttango' ); ?>
		</h3>

		<div class="tt-grid tt-grid-cols-1 tt-gap-5 sm:tt-grid-cols-3">
			<div>
				<label class="tt-label" for="texttango-otp-attempts"><?php esc_html_e( 'Attempts per code', 'texttango' ); ?></label>
				<input class="tt-input" id="texttango-otp-attempts" name="otp_max_attempts" type="number" min="3" max="10"
					value="<?php echo esc_attr( (string) $texttango_settings['otp_max_attempts'] ); ?>" />
				<p class="tt-hint"><?php esc_html_e( 'Then the code is destroyed.', 'texttango' ); ?></p>
			</div>
			<div>
				<label class="tt-label" for="texttango-otp-phone-limit"><?php esc_html_e( 'Codes per number', 'texttango' ); ?></label>
				<input class="tt-input" id="texttango-otp-phone-limit" name="otp_phone_limit" type="number" min="1" max="20"
					value="<?php echo esc_attr( (string) $texttango_settings['otp_phone_limit'] ); ?>" />
				<p class="tt-hint"><?php esc_html_e( 'Per 15 minutes.', 'texttango' ); ?></p>
			</div>
			<div>
				<label class="tt-label" for="texttango-otp-ip-limit"><?php esc_html_e( 'Codes per IP address', 'texttango' ); ?></label>
				<input class="tt-input" id="texttango-otp-ip-limit" name="otp_ip_limit" type="number" min="1" max="100"
					value="<?php echo esc_attr( (string) $texttango_settings['otp_ip_limit'] ); ?>" />
				<p class="tt-hint"><?php esc_html_e( 'Per hour.', 'texttango' ); ?></p>
			</div>
		</div>
	</div>

	<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( 'Two-factor authentication', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'Asks for an SMS code after a correct password. Users can opt in from their profile, and whole roles can be required below.', 'texttango' ); ?>
		</p>

		<label class="tt-mb-4 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="otp_2fa_enabled" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['otp_2fa_enabled'] ) ); ?> />
			<span><?php esc_html_e( 'Enable SMS two-factor authentication', 'texttango' ); ?></span>
		</label>

		<span class="tt-label"><?php esc_html_e( 'Require it for these roles', 'texttango' ); ?></span>
		<div class="tt-mt-1 tt-grid tt-grid-cols-2 tt-gap-2 sm:tt-grid-cols-3">
			<?php foreach ( (array) $args['roles'] as $texttango_role => $texttango_role_label ) : ?>
				<label class="tt-flex tt-items-center tt-gap-2 tt-text-sm tt-text-slate-700">
					<input type="checkbox" name="otp_2fa_roles[]" value="<?php echo esc_attr( (string) $texttango_role ); ?>"
						<?php checked( in_array( (string) $texttango_role, (array) $texttango_settings['otp_2fa_roles'], true ) ); ?> />
					<?php echo esc_html( (string) $texttango_role_label ); ?>
				</label>
			<?php endforeach; ?>
		</div>
		<p class="tt-hint tt-mt-3">
			<?php esc_html_e( 'A user with no verified phone number is never challenged, so enforcing a role cannot lock anyone out.', 'texttango' ); ?>
		</p>
	</div>

	<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( 'Verify phone at checkout', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'Requires a customer to enter a code texted to the phone number they checked out with before the order can be placed. Works for guest and signed-in checkout, on both classic and block checkout.', 'texttango' ); ?>
		</p>

		<label class="tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
			<input type="checkbox" name="checkout_otp_enabled" value="1" class="tt-mt-0.5"
				<?php checked( ! empty( $texttango_settings['checkout_otp_enabled'] ) ); ?> />
			<span><?php esc_html_e( 'Require phone verification before an order can be placed', 'texttango' ); ?></span>
		</label>
	</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>
