<?php
/**
 * Connection tab.
 *
 * @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( 'Connection', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-5 tt-text-sm tt-text-slate-500">
			<?php
			printf(
				/* translators: %s: link to the TextTango dashboard token page. */
				esc_html__( 'API tokens cannot be created from WordPress. Create one in the TextTango dashboard and paste it here: %s', 'texttango' ),
				'<a class="tt-font-medium tt-text-brand-600" target="_blank" rel="noopener noreferrer" href="' . esc_url( (string) $args['token_page'] ) . '">' . esc_html__( 'Profile → API Tokens', 'texttango' ) . '</a>'
			);
			?>
		</p>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-base-url"><?php esc_html_e( 'API base URL', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-base-url"
				name="api_base_url"
				type="url"
				value="<?php echo esc_attr( (string) $texttango_settings['api_base_url'] ); ?>"
				placeholder="https://app.texttango.com/api" />
			<p class="tt-hint">
				<?php esc_html_e( 'Override this to point at a staging or local TextTango instance. HTTPS is required except on local development hosts.', 'texttango' ); ?>
			</p>
		</div>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-api-key"><?php esc_html_e( 'API token', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-api-key"
				name="api_key"
				type="password"
				autocomplete="off"
				spellcheck="false"
				placeholder="<?php echo esc_attr( ! empty( $args['has_api_key'] ) ? __( 'Saved — leave blank to keep it', 'texttango' ) : __( 'Paste your API token', 'texttango' ) ); ?>" />
			<p class="tt-hint">
				<?php esc_html_e( 'Stored encrypted. It is never shown again, sent to JavaScript, or returned by any REST route.', 'texttango' ); ?>
				<?php esc_html_e( 'Required abilities:', 'texttango' ); ?>
				<code class="tt-rounded tt-bg-slate-100 tt-px-1 tt-py-0.5 tt-text-xs"><?php echo esc_html( implode( ', ', (array) $args['abilities'] ) ); ?></code>
			</p>
			<?php if ( ! empty( $args['has_api_key'] ) ) : ?>
				<label class="tt-mt-2 tt-flex tt-items-center tt-gap-2 tt-text-sm tt-text-slate-600">
					<input type="checkbox" name="remove_api_key" value="1" />
					<?php esc_html_e( 'Remove the stored API token', 'texttango' ); ?>
				</label>
			<?php endif; ?>
		</div>

		<div class="tt-mb-5">
			<label class="tt-label" for="texttango-webhook-secret"><?php esc_html_e( 'Webhook signing secret', 'texttango' ); ?></label>
			<input
				class="tt-input"
				id="texttango-webhook-secret"
				name="webhook_secret"
				type="password"
				autocomplete="off"
				spellcheck="false"
				placeholder="<?php echo esc_attr( ! empty( $args['has_webhook'] ) ? __( 'Saved — leave blank to keep it', 'texttango' ) : 'whsec_…' ); ?>" />
			<p class="tt-hint">
				<?php esc_html_e( 'The whsec_… value shown once when the token was created. Used to verify inbound delivery receipts.', 'texttango' ); ?>
			</p>
			<?php if ( ! empty( $args['has_webhook'] ) ) : ?>
				<label class="tt-mt-2 tt-flex tt-items-center tt-gap-2 tt-text-sm tt-text-slate-600">
					<input type="checkbox" name="remove_webhook_secret" value="1" />
					<?php esc_html_e( 'Remove the stored webhook signing secret', 'texttango' ); ?>
				</label>
			<?php endif; ?>
		</div>

		<div class="tt-grid tt-grid-cols-1 tt-gap-5 sm:tt-grid-cols-2">
			<div>
				<label class="tt-label" for="texttango-sender-id"><?php esc_html_e( 'Default sender ID', 'texttango' ); ?></label>
				<div class="tt-flex tt-gap-2">
					<input
						class="tt-input"
						id="texttango-sender-id"
						name="sender_id"
						type="text"
						maxlength="11"
						list="texttango-sender-ids"
						value="<?php echo esc_attr( (string) $texttango_settings['sender_id'] ); ?>" />
					<datalist id="texttango-sender-ids"></datalist>
					<button type="button" class="tt-btn tt-btn-secondary tt-shrink-0" data-texttango-action="load-senders">
						<?php esc_html_e( 'Load', 'texttango' ); ?>
					</button>
				</div>
				<p class="tt-hint">
					<?php esc_html_e( 'Up to 11 characters. Load pulls the approved sender IDs from your account.', 'texttango' ); ?>
				</p>
			</div>

			<div>
				<label class="tt-label" for="texttango-country"><?php esc_html_e( 'Default country', 'texttango' ); ?></label>
				<select class="tt-input" id="texttango-country" name="default_country">
					<?php foreach ( (array) $args['countries'] as $texttango_code => $texttango_label ) : ?>
						<option
							value="<?php echo esc_attr( (string) $texttango_code ); ?>"
							<?php selected( (string) $texttango_settings['default_country'], (string) $texttango_code ); ?>>
							<?php echo esc_html( (string) $texttango_label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<p class="tt-hint">
					<?php esc_html_e( 'Used to turn local numbers into E.164 when an order has no country.', '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( 'Delivery receipts', 'texttango' ); ?>
		</h2>
		<p class="tt-mb-4 tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'TextTango posts a signed receipt here each time a message is delivered or fails, which is what moves the message log past "sent".', 'texttango' ); ?>
		</p>

		<input
			class="tt-input tt-font-mono tt-text-xs"
			type="text"
			readonly
			onfocus="this.select()"
			value="<?php echo esc_attr( (string) $args['webhook_url'] ); ?>" />

		<p class="tt-mt-3 tt-text-sm" data-texttango-webhook-state aria-live="polite">
			<?php if ( ! empty( $args['webhook_verified'] ) ) : ?>
				<span class="tt-font-medium tt-text-emerald-700">
					<?php
					printf(
						/* translators: %s: how long ago, e.g. "2 mins". */
						esc_html__( '● Verified — a signed request from TextTango arrived here %s ago.', 'texttango' ),
						esc_html( human_time_diff( (int) $args['webhook_last_arrival'] ) )
					);
					?>
				</span>
			<?php else : ?>
				<span class="tt-text-slate-500">
					<?php esc_html_e( 'Nothing has arrived here yet. Register this site, then send a test event to prove the loop closes.', 'texttango' ); ?>
				</span>
			<?php endif; ?>
		</p>

		<div class="tt-mt-4 tt-flex tt-flex-wrap tt-items-center tt-gap-3">
			<button type="button" class="tt-btn tt-btn-secondary" data-texttango-action="webhook-register"
				<?php disabled( empty( $args['has_api_key'] ) ); ?>>
				<?php esc_html_e( 'Register this site', 'texttango' ); ?>
			</button>
			<button type="button" class="tt-btn tt-btn-secondary" data-texttango-action="webhook-test"
				<?php disabled( empty( $args['has_api_key'] ) || empty( $args['has_webhook'] ) ); ?>>
				<?php esc_html_e( 'Send test event', 'texttango' ); ?>
			</button>
			<button type="button" class="tt-btn tt-btn-secondary" data-texttango-action="webhook-rotate"
				data-texttango-confirm="<?php esc_attr_e( 'Rotating issues a new signing secret and stops the current one working immediately. Receipts will not verify until the new one is saved, which happens straight away. Continue?', 'texttango' ); ?>"
				<?php disabled( empty( $args['has_api_key'] ) ); ?>>
				<?php esc_html_e( 'Rotate signing secret', 'texttango' ); ?>
			</button>
			<span class="tt-text-sm" data-texttango-webhook-result aria-live="polite"></span>
		</div>

		<p class="tt-hint tt-mt-3">
			<?php esc_html_e( 'Registering sets this URL as the token\'s single_sms_callback_url and needs a token with webhooks:write. Without that ability, set it by hand in the TextTango dashboard instead — everything else still works.', 'texttango' ); ?>
		</p>
	</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>
		<button
			type="button"
			class="tt-btn tt-btn-secondary"
			data-texttango-action="test-connection"
			<?php disabled( empty( $args['has_api_key'] ) ); ?>>
			<?php esc_html_e( 'Test connection', 'texttango' ); ?>
		</button>
		<span class="tt-text-sm" data-texttango-result aria-live="polite"></span>
	</div>

	<?php if ( empty( $args['has_api_key'] ) ) : ?>
		<p class="tt-hint tt-mt-2">
			<?php esc_html_e( 'Save your API token first, then test the connection.', 'texttango' ); ?>
		</p>
	<?php endif; ?>

	<?php if ( ! empty( $args['derived_key'] ) ) : ?>
		<p class="tt-hint tt-mt-4">
			<?php esc_html_e( 'Credentials are encrypted with a key derived from this site\'s security salts. Rotating those salts will make them unreadable and you will need to paste them again. Define TEXTTANGO_ENCRYPTION_KEY in wp-config.php to use a dedicated key instead.', 'texttango' ); ?>
		</p>
	<?php endif; ?>
</form>
