<?php
/**
 * Add / edit connection form (shown below provider picker).
 *
 * @package EmailSmtp
 * @var string               $provider_id
 * @var array<string, mixed> $connection  Existing connection or empty defaults
 * @var array<string, mixed> $settings
 * @var string               $page_url
 */

use EmailSmtp\Includes\ConnectionStore;
use EmailSmtp\Includes\OAuth\OAuthState;
use EmailSmtp\Includes\OAuth\OAuthTokenStore;
use EmailSmtp\Includes\ProviderRegistry;
use EmailSmtp\Includes\SmtpRelayProfiles;
use EmailSmtp\Module;
use EmailSmtp\Providers\AmazonSes\Provider as AmazonSesProvider;
use EmailSmtp\Providers\Smtp\Provider as SmtpProvider;

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

$provider = ProviderRegistry::get( $provider_id );
if ( ! $provider ) {
	return;
}

$conn_id = sanitize_key( (string) ( $connection['id'] ?? '' ) );
$is_edit = $conn_id !== '';
$name    = (string) ( $connection['name'] ?? $provider->get_name() );
$from    = (string) ( $connection['from_email'] ?? ( $settings['from_email'] ?? get_option( 'admin_email' ) ) );
$from_name = (string) ( $connection['from_name'] ?? ( $settings['from_name'] ?? get_option( 'blogname' ) ) );

$field = static function ( string $key ) use ( $connection, $settings ): string {
	if ( isset( $connection[ $key ] ) ) {
		return (string) $connection[ $key ];
	}
	return (string) ( $settings[ $key ] ?? '' );
};

$secret = static function ( string $key ) use ( $connection ): string {
	return ! empty( $connection[ $key ] ) ? Module::getPasswordMask() : '';
};

/**
 * Under-field dashboard link (“Follow this link to get…”).
 *
 * @param string      $url         Provider dashboard URL.
 * @param string      $link_label  Action label, e.g. “Create API Key”.
 * @param string      $what        What to retrieve, e.g. “an API Key”.
 * @param string|null $setup_help  Optional setup-modal id (data-wpps-ed-help).
 */
$credential_help = static function ( string $url, string $link_label, string $what, ?string $setup_help = null ): void {
	echo '<p class="description">';
	echo esc_html(
		sprintf(
			/* translators: %s: credential type, e.g. "an API Key" */
			__( 'Follow this link to get %s:', 'wp-powersuite' ),
			$what
		)
	);
	printf(
		' <a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>',
		esc_url( $url ),
		esc_html( $link_label )
	);
	if ( is_string( $setup_help ) && $setup_help !== '' ) {
		printf(
			' <button type="button" class="button-link wpps-ed-open-help-modal" data-wpps-ed-help="%1$s">%2$s</button>',
			esc_attr( $setup_help ),
			esc_html__( 'Setup guide', 'wp-powersuite' )
		);
	}
	echo '</p>';
};

$icon = ConnectionStore::provider_icon_url( $provider_id );
// Branded providers are API-only. SMTP relay uses Custom SMTP.
$transport         = $provider_id === 'smtp' ? 'smtp' : 'api';
$legacy_smtp_relay = $provider_id !== 'smtp'
	&& SmtpRelayProfiles::supports( $provider_id )
	&& sanitize_key( (string) ( $connection['transport'] ?? 'api' ) ) === 'smtp';
$cred_title        = $provider_id === 'smtp'
	? __( 'SMTP settings', 'wp-powersuite' )
	: __( 'Provider credentials', 'wp-powersuite' );
?>
<div class="wpps-ed-panel wpps-ed-conn-form-panel" id="wpps-ed-connection-form">
	<div class="wpps-ed-panel-title">
		<span class="wpps-ed-conn-form-head">
			<?php if ( $icon !== '' ) : ?>
				<img src="<?php echo esc_url( $icon ); ?>" alt="" class="wpps-ed-provider-icon wpps-ed-provider-icon--form" />
			<?php endif; ?>
			<span class="wpps-ed-conn-form-head__text">
				<span class="wpps-ed-conn-form-head__title">
					<?php
					echo esc_html(
						$is_edit
							? sprintf(
								/* translators: %s: provider name */
								__( 'Edit %s connection', 'wp-powersuite' ),
								$provider->get_name()
							)
							: sprintf(
								/* translators: %s: provider name */
								__( 'New %s connection', 'wp-powersuite' ),
								$provider->get_name()
							)
					);
					?>
				</span>
				<span class="wpps-ed-conn-form-head__sub"><?php echo esc_html( ProviderRegistry::transport_label_for( $provider_id ) ); ?></span>
			</span>
		</span>
	</div>
	<div class="wpps-ed-panel-body wpps-ed-conn-form-body">
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wpps-ed-connection-form">
			<?php wp_nonce_field( 'wpps_email_delivery_save_connection' ); ?>
			<input type="hidden" name="action" value="wpps_email_delivery_save_connection" />
			<input type="hidden" name="connection[id]" value="<?php echo esc_attr( $conn_id ); ?>" />
			<input type="hidden" name="connection[provider]" value="<?php echo esc_attr( $provider_id ); ?>" />
			<input type="hidden" name="connection[transport]" value="<?php echo esc_attr( $transport ); ?>" />

			<?php if ( $legacy_smtp_relay ) : ?>
				<div class="notice notice-warning inline" style="margin: 0 20px 0;">
					<p>
						<?php
						esc_html_e(
							'This connection still uses SMTP relay. Named providers now send via API only. Enter API credentials below and save, or create a Custom SMTP connection for SMTP relay.',
							'wp-powersuite'
						);
						?>
					</p>
				</div>
			<?php endif; ?>

			<section class="wpps-ed-conn-section">
				<h3 class="wpps-ed-conn-section__title"><?php esc_html_e( 'Sender', 'wp-powersuite' ); ?></h3>
				<div class="wpps-ed-conn-grid">
					<div class="wpps-ed-field wpps-ed-field--full">
						<label for="wpps_ed_conn_name"><?php esc_html_e( 'Connection name', 'wp-powersuite' ); ?></label>
						<input type="text" class="regular-text" id="wpps_ed_conn_name" name="connection[name]" value="<?php echo esc_attr( $name ); ?>" placeholder="<?php esc_attr_e( 'e.g. Shop orders', 'wp-powersuite' ); ?>" />
					</div>
					<div class="wpps-ed-field">
						<label for="wpps_ed_conn_from"><?php esc_html_e( 'From email', 'wp-powersuite' ); ?></label>
						<input type="email" class="regular-text" id="wpps_ed_conn_from" name="connection[from_email]" value="<?php echo esc_attr( $from ); ?>" required />
						<p class="description"><?php esc_html_e( 'Sender address for this connection. Verify it with your email provider first.', 'wp-powersuite' ); ?></p>
					</div>
					<div class="wpps-ed-field">
						<label for="wpps_ed_conn_from_name"><?php esc_html_e( 'From name', 'wp-powersuite' ); ?></label>
						<input type="text" class="regular-text" id="wpps_ed_conn_from_name" name="connection[from_name]" value="<?php echo esc_attr( $from_name ); ?>" />
					</div>
				</div>

				<div class="wpps-ed-conn-options">
					<label class="wpps-ed-check-card">
						<input type="checkbox" name="connection[force_from_email]" value="1" <?php checked( (string) ( $connection['force_from_email'] ?? '1' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Force From email', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'Override the From address on every send.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
					<label class="wpps-ed-check-card">
						<input type="checkbox" name="connection[force_from_name]" value="1" <?php checked( (string) ( $connection['force_from_name'] ?? '1' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Force From name', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'Override the display name on every send.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
					<label class="wpps-ed-check-card">
						<input type="checkbox" name="connection[set_return_path]" value="1" <?php checked( (string) ( $connection['set_return_path'] ?? '1' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Set Return-Path', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'Match the Return-Path to the From email so bounce notices are delivered.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
				</div>
			</section>

			<section class="wpps-ed-conn-section">
				<h3 class="wpps-ed-conn-section__title"><?php echo esc_html( $cred_title ); ?></h3>
				<table class="form-table wpps-ed-conn-fields" role="presentation">
				<?php if ( $provider_id === 'wordpress' ) : ?>
					<tr>
						<th scope="row"><?php esc_html_e( 'Credentials', 'wp-powersuite' ); ?></th>
						<td><p class="description"><?php esc_html_e( 'No credentials needed. Not recommended for production.', 'wp-powersuite' ); ?></p></td>
					</tr>

				<?php elseif ( $provider_id === 'smtp' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_smtp_host"><?php esc_html_e( 'SMTP host', 'wp-powersuite' ); ?></label></th>
						<td><input type="text" class="regular-text" id="wpps_ed_smtp_host" name="connection[fields][smtp_host]" value="<?php echo esc_attr( $field( 'smtp_host' ) ); ?>" placeholder="smtp.example.com" /></td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_smtp_port"><?php esc_html_e( 'Port', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="number" class="small-text" id="wpps_ed_smtp_port" name="connection[fields][smtp_port]" value="<?php echo esc_attr( $field( 'smtp_port' ) !== '' ? $field( 'smtp_port' ) : '587' ); ?>" min="1" max="65535" />
							<p class="description"><?php echo esc_html( sprintf( /* translators: %s: ports */ __( 'Suggested: %s', 'wp-powersuite' ), implode( ', ', SmtpProvider::suggested_ports( $field( 'smtp_encryption' ) !== '' ? $field( 'smtp_encryption' ) : 'tls' ) ) ) ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_smtp_encryption"><?php esc_html_e( 'Encryption', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_smtp_encryption" name="connection[fields][smtp_encryption]">
								<option value="tls" <?php selected( $field( 'smtp_encryption' ) !== '' ? $field( 'smtp_encryption' ) : 'tls', 'tls' ); ?>><?php esc_html_e( 'TLS', 'wp-powersuite' ); ?></option>
								<option value="ssl" <?php selected( $field( 'smtp_encryption' ), 'ssl' ); ?>><?php esc_html_e( 'SSL', 'wp-powersuite' ); ?></option>
								<option value="" <?php selected( $field( 'smtp_encryption' ), '' ); ?>><?php esc_html_e( 'None', 'wp-powersuite' ); ?></option>
							</select>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Authentication', 'wp-powersuite' ); ?></th>
						<td>
							<label>
								<input type="hidden" name="connection[fields][smtp_auth]" value="0" />
								<input type="checkbox" id="wpps_ed_smtp_auth" name="connection[fields][smtp_auth]" value="1" <?php checked( $field( 'smtp_auth' ) !== '0', true ); ?> />
								<?php esc_html_e( 'Use SMTP authentication', 'wp-powersuite' ); ?>
							</label>
						</td>
					</tr>
					<tr class="wpps-ed-auth-row">
						<th scope="row"><label for="wpps_ed_smtp_username"><?php esc_html_e( 'Username', 'wp-powersuite' ); ?></label></th>
						<td><input type="text" class="regular-text" id="wpps_ed_smtp_username" name="connection[fields][smtp_username]" value="<?php echo esc_attr( $field( 'smtp_username' ) ); ?>" autocomplete="off" /></td>
					</tr>
					<tr class="wpps-ed-auth-row">
						<th scope="row"><label for="wpps_ed_smtp_password"><?php esc_html_e( 'Password', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_smtp_password" name="connection[fields][smtp_password]" value="<?php echo esc_attr( $secret( 'smtp_password' ) ); ?>" autocomplete="new-password" />
							<p class="description"><?php esc_html_e( 'Leave masked to keep the stored password.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'google' ) : ?>
					<?php $callback_url = OAuthState::callback_url( 'google' ); ?>
					<tr>
						<th scope="row"><label for="wpps_ed_google_client_id"><?php esc_html_e( 'Client ID', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_google_client_id" name="connection[fields][google_client_id]" value="<?php echo esc_attr( $field( 'google_client_id' ) ); ?>" autocomplete="off" />
							<?php
							$credential_help(
								'https://console.cloud.google.com/apis/credentials',
								__( 'Open Credentials', 'wp-powersuite' ),
								__( 'an OAuth Client ID', 'wp-powersuite' ),
								'google'
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_google_client_secret"><?php esc_html_e( 'Client secret', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_google_client_secret" name="connection[fields][google_client_secret]" value="<?php echo esc_attr( $secret( 'google_client_secret' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://console.cloud.google.com/apis/credentials',
								__( 'Open Credentials', 'wp-powersuite' ),
								__( 'the Client secret', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Redirect URI', 'wp-powersuite' ); ?></th>
						<td>
							<code class="wpps-ed-callback-url"><?php echo esc_html( $callback_url ); ?></code>
							<button type="button" class="button button-small wpps-ed-copy" data-copy="<?php echo esc_attr( $callback_url ); ?>"><?php esc_html_e( 'Copy', 'wp-powersuite' ); ?></button>
							<p class="description"><?php esc_html_e( 'Add this URI in Google Cloud, save the connection, then click Connect.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Status', 'wp-powersuite' ); ?></th>
						<td>
							<?php if ( OAuthTokenStore::is_connected( $connection, 'google' ) ) : ?>
								<span class="wpps-ed-status wpps-ed-status--connected"><?php esc_html_e( 'Connected', 'wp-powersuite' ); ?></span>
								<?php if ( is_email( (string) ( $connection['google_connected_email'] ?? '' ) ) ) : ?>
									— <?php echo esc_html( (string) $connection['google_connected_email'] ); ?>
								<?php endif; ?>
							<?php else : ?>
								<span class="wpps-ed-status wpps-ed-status--incomplete"><?php esc_html_e( 'Not connected', 'wp-powersuite' ); ?></span>
							<?php endif; ?>
							<?php if ( $is_edit ) : ?>
								<p style="margin-top:10px;">
									<button type="submit" form="wpps-ed-oauth-google" class="button button-secondary"><?php esc_html_e( 'Connect Google', 'wp-powersuite' ); ?></button>
									<?php if ( OAuthTokenStore::is_connected( $connection, 'google' ) ) : ?>
										<button type="submit" form="wpps-ed-oauth-google-disconnect" class="button"><?php esc_html_e( 'Disconnect', 'wp-powersuite' ); ?></button>
									<?php endif; ?>
								</p>
							<?php else : ?>
								<p class="description"><?php esc_html_e( 'Save the connection first, then Connect to authorize.', 'wp-powersuite' ); ?></p>
							<?php endif; ?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'microsoft' ) : ?>
					<?php $callback_url = OAuthState::callback_url( 'microsoft' ); ?>
					<tr>
						<th scope="row"><label for="wpps_ed_ms_client_id"><?php esc_html_e( 'Application (client) ID', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_ms_client_id" name="connection[fields][microsoft_client_id]" value="<?php echo esc_attr( $field( 'microsoft_client_id' ) ); ?>" autocomplete="off" />
							<?php
							$credential_help(
								'https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade',
								__( 'Open App registrations', 'wp-powersuite' ),
								__( 'an Application (client) ID', 'wp-powersuite' ),
								'microsoft'
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_ms_client_secret"><?php esc_html_e( 'Client secret', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_ms_client_secret" name="connection[fields][microsoft_client_secret]" value="<?php echo esc_attr( $secret( 'microsoft_client_secret' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://portal.azure.com/#view/Microsoft_AAD_RegisteredApps/ApplicationsListBlade',
								__( 'Open App registrations', 'wp-powersuite' ),
								__( 'a Client secret', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_ms_tenant"><?php esc_html_e( 'Tenant', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_ms_tenant" name="connection[fields][microsoft_tenant]" value="<?php echo esc_attr( $field( 'microsoft_tenant' ) !== '' ? $field( 'microsoft_tenant' ) : 'common' ); ?>" />
							<p class="description"><?php esc_html_e( 'Usually “common”. Or use your directory tenant ID.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Redirect URI', 'wp-powersuite' ); ?></th>
						<td>
							<code class="wpps-ed-callback-url"><?php echo esc_html( $callback_url ); ?></code>
							<button type="button" class="button button-small wpps-ed-copy" data-copy="<?php echo esc_attr( $callback_url ); ?>"><?php esc_html_e( 'Copy', 'wp-powersuite' ); ?></button>
							<p class="description"><?php esc_html_e( 'Add this as a Web redirect URI, then save and Connect.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><?php esc_html_e( 'Status', 'wp-powersuite' ); ?></th>
						<td>
							<?php if ( OAuthTokenStore::is_connected( $connection, 'microsoft' ) ) : ?>
								<span class="wpps-ed-status wpps-ed-status--connected"><?php esc_html_e( 'Connected', 'wp-powersuite' ); ?></span>
								<?php if ( is_email( (string) ( $connection['microsoft_connected_email'] ?? '' ) ) ) : ?>
									— <?php echo esc_html( (string) $connection['microsoft_connected_email'] ); ?>
								<?php endif; ?>
							<?php else : ?>
								<span class="wpps-ed-status wpps-ed-status--incomplete"><?php esc_html_e( 'Not connected', 'wp-powersuite' ); ?></span>
							<?php endif; ?>
							<?php if ( $is_edit ) : ?>
								<p style="margin-top:10px;">
									<button type="submit" form="wpps-ed-oauth-microsoft" class="button button-secondary"><?php esc_html_e( 'Connect Microsoft', 'wp-powersuite' ); ?></button>
									<?php if ( OAuthTokenStore::is_connected( $connection, 'microsoft' ) ) : ?>
										<button type="submit" form="wpps-ed-oauth-microsoft-disconnect" class="button"><?php esc_html_e( 'Disconnect', 'wp-powersuite' ); ?></button>
									<?php endif; ?>
								</p>
							<?php else : ?>
								<p class="description"><?php esc_html_e( 'Save the connection first, then Connect to authorize.', 'wp-powersuite' ); ?></p>
							<?php endif; ?>
						</td>
					</tr>

				<?php else : ?>
					<tbody class="wpps-ed-api-fields">
				<?php if ( $provider_id === 'sendgrid' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_sendgrid_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_sendgrid_api_key" name="connection[fields][sendgrid_api_key]" value="<?php echo esc_attr( $secret( 'sendgrid_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.sendgrid.com/settings/api_keys',
								__( 'Create API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' ),
								'sendgrid'
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_sendgrid_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_sendgrid_region" name="connection[fields][sendgrid_region]">
								<option value="global" <?php selected( $field( 'sendgrid_region' ) !== '' ? $field( 'sendgrid_region' ) : 'global', 'global' ); ?>><?php esc_html_e( 'Global', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'sendgrid_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
							</select>
							<p class="description"><?php esc_html_e( 'Match the region of your SendGrid account.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'mailgun' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_mailgun_api_key"><?php esc_html_e( 'Private API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mailgun_api_key" name="connection[fields][mailgun_api_key]" value="<?php echo esc_attr( $secret( 'mailgun_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.mailgun.com/settings/api_security',
								__( 'Get a Private API Key', 'wp-powersuite' ),
								__( 'a Private API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_mailgun_domain"><?php esc_html_e( 'Sending domain', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_mailgun_domain" name="connection[fields][mailgun_domain]" value="<?php echo esc_attr( $field( 'mailgun_domain' ) ); ?>" placeholder="mg.example.com" />
							<?php
							$credential_help(
								'https://app.mailgun.com/mg/sending/domains',
								__( 'Get a Domain Name', 'wp-powersuite' ),
								__( 'a sending domain', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_mailgun_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_mailgun_region" name="connection[fields][mailgun_region]">
								<option value="us" <?php selected( $field( 'mailgun_region' ) !== '' ? $field( 'mailgun_region' ) : 'us', 'us' ); ?>><?php esc_html_e( 'US', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'mailgun_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
							</select>
							<p class="description"><?php esc_html_e( 'Must match where the domain was created.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'brevo' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_brevo_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_brevo_api_key" name="connection[fields][brevo_api_key]" value="<?php echo esc_attr( $secret( 'brevo_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.brevo.com/settings/keys/api',
								__( 'Get v3 API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'postmark' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_postmark_token"><?php esc_html_e( 'Server token', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_postmark_token" name="connection[fields][postmark_server_token]" value="<?php echo esc_attr( $secret( 'postmark_server_token' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://account.postmarkapp.com/api_tokens',
								__( 'Get Server API Token', 'wp-powersuite' ),
								__( 'a Server API Token', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_postmark_stream"><?php esc_html_e( 'Message stream', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_postmark_stream" name="connection[fields][postmark_message_stream]" value="<?php echo esc_attr( $field( 'postmark_message_stream' ) !== '' ? $field( 'postmark_message_stream' ) : 'outbound' ); ?>" placeholder="outbound" />
							<p class="description"><?php esc_html_e( 'Usually “outbound” for transactional mail.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'mailersend' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_mailersend_token"><?php esc_html_e( 'API token', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mailersend_token" name="connection[fields][mailersend_api_token]" value="<?php echo esc_attr( $secret( 'mailersend_api_token' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.mailersend.com/api-tokens',
								__( 'Get API Token', 'wp-powersuite' ),
								__( 'an API Token', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'resend' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_resend_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_resend_api_key" name="connection[fields][resend_api_key]" value="<?php echo esc_attr( $secret( 'resend_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://resend.com/api-keys',
								__( 'Create API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'sparkpost' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_sparkpost_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_sparkpost_api_key" name="connection[fields][sparkpost_api_key]" value="<?php echo esc_attr( $secret( 'sparkpost_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.sparkpost.com/account/api-keys',
								__( 'Get API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_sparkpost_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_sparkpost_region" name="connection[fields][sparkpost_region]">
								<option value="us" <?php selected( $field( 'sparkpost_region' ) !== '' ? $field( 'sparkpost_region' ) : 'us', 'us' ); ?>><?php esc_html_e( 'US', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'sparkpost_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
							</select>
							<p class="description"><?php esc_html_e( 'Used when the key does not include a region.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'elastic-email' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_elastic_email_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_elastic_email_api_key" name="connection[fields][elastic_email_api_key]" value="<?php echo esc_attr( $secret( 'elastic_email_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.elasticemail.com/api/settings/manage-api',
								__( 'Get API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'smtp2go' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_smtp2go_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_smtp2go_api_key" name="connection[fields][smtp2go_api_key]" value="<?php echo esc_attr( $secret( 'smtp2go_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.smtp2go.com/sending/apikeys/',
								__( 'Create API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_smtp2go_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_smtp2go_region" name="connection[fields][smtp2go_region]">
								<option value="global" <?php selected( $field( 'smtp2go_region' ) !== '' ? $field( 'smtp2go_region' ) : 'global', 'global' ); ?>><?php esc_html_e( 'Global', 'wp-powersuite' ); ?></option>
								<option value="us" <?php selected( $field( 'smtp2go_region' ), 'us' ); ?>><?php esc_html_e( 'US', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'smtp2go_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
								<option value="au" <?php selected( $field( 'smtp2go_region' ), 'au' ); ?>><?php esc_html_e( 'AU', 'wp-powersuite' ); ?></option>
							</select>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'mailjet' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_mailjet_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mailjet_api_key" name="connection[fields][mailjet_api_key]" value="<?php echo esc_attr( $secret( 'mailjet_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.mailjet.com/account/apikeys',
								__( 'API Key Management', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_mailjet_secret_key"><?php esc_html_e( 'Secret key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mailjet_secret_key" name="connection[fields][mailjet_secret_key]" value="<?php echo esc_attr( $secret( 'mailjet_secret_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.mailjet.com/account/apikeys',
								__( 'API Key Management', 'wp-powersuite' ),
								__( 'a Secret Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'netcore' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_netcore_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_netcore_api_key" name="connection[fields][netcore_api_key]" value="<?php echo esc_attr( $secret( 'netcore_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.pepipost.com/app/settings/integration',
								__( 'Get API Key', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_netcore_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_netcore_region" name="connection[fields][netcore_region]">
								<option value="us" <?php selected( $field( 'netcore_region' ) !== '' ? $field( 'netcore_region' ) : 'us', 'us' ); ?>><?php esc_html_e( 'Non-EU', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'netcore_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
							</select>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'mandrill' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_mandrill_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mandrill_api_key" name="connection[fields][mandrill_api_key]" value="<?php echo esc_attr( $secret( 'mandrill_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://mandrillapp.com/settings',
								__( 'Create API Key', 'wp-powersuite' ),
								__( 'a Mandrill API Key', 'wp-powersuite' )
							);
							?>
							<p class="description"><?php esc_html_e( 'Use a Mailchimp Transactional key — not a Marketing API key.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'emailit' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_emailit_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_emailit_api_key" name="connection[fields][emailit_api_key]" value="<?php echo esc_attr( $secret( 'emailit_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.emailit.com/',
								__( 'Open Dashboard', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'sendlayer' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_sendlayer_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_sendlayer_api_key" name="connection[fields][sendlayer_api_key]" value="<?php echo esc_attr( $secret( 'sendlayer_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://console.sendlayer.com/',
								__( 'Open Settings → API Keys', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'smtpcom' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_smtpcom_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_smtpcom_api_key" name="connection[fields][smtpcom_api_key]" value="<?php echo esc_attr( $secret( 'smtpcom_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://my.smtp.com/',
								__( 'Open Account', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_smtpcom_channel"><?php esc_html_e( 'Channel name', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_smtpcom_channel" name="connection[fields][smtpcom_channel]" value="<?php echo esc_attr( $field( 'smtpcom_channel' ) ); ?>" autocomplete="off" />
							<?php
							$credential_help(
								'https://my.smtp.com/',
								__( 'Open Channels', 'wp-powersuite' ),
								__( 'the channel name', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'maileroo' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_maileroo_api_key"><?php esc_html_e( 'Sending key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_maileroo_api_key" name="connection[fields][maileroo_api_key]" value="<?php echo esc_attr( $secret( 'maileroo_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://app.maileroo.com/domains',
								__( 'Create Sending Key', 'wp-powersuite' ),
								__( 'a Sending Key', 'wp-powersuite' )
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'turbo-smtp' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_turbo_smtp_consumer_key"><?php esc_html_e( 'Consumer key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_turbo_smtp_consumer_key" name="connection[fields][turbo_smtp_consumer_key]" value="<?php echo esc_attr( $secret( 'turbo_smtp_consumer_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://dash.turbo-smtp.com/',
								__( 'Open API Keys', 'wp-powersuite' ),
								__( 'a Consumer Key', 'wp-powersuite' )
							);
							?>
							<p class="description"><?php esc_html_e( 'The Consumer Secret is shown only once when created.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_turbo_smtp_consumer_secret"><?php esc_html_e( 'Consumer secret', 'wp-powersuite' ); ?></label></th>
						<td><input type="password" class="regular-text" id="wpps_ed_turbo_smtp_consumer_secret" name="connection[fields][turbo_smtp_consumer_secret]" value="<?php echo esc_attr( $secret( 'turbo_smtp_consumer_secret' ) ); ?>" autocomplete="new-password" /></td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_turbo_smtp_region"><?php esc_html_e( 'Region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_turbo_smtp_region" name="connection[fields][turbo_smtp_region]">
								<option value="us" <?php selected( $field( 'turbo_smtp_region' ) !== '' ? $field( 'turbo_smtp_region' ) : 'us', 'us' ); ?>><?php esc_html_e( 'Global', 'wp-powersuite' ); ?></option>
								<option value="eu" <?php selected( $field( 'turbo_smtp_region' ), 'eu' ); ?>><?php esc_html_e( 'EU', 'wp-powersuite' ); ?></option>
							</select>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'mail-baby' ) : ?>
					<tr>
						<th scope="row"><label for="wpps_ed_mail_baby_api_key"><?php esc_html_e( 'API key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_mail_baby_api_key" name="connection[fields][mail_baby_api_key]" value="<?php echo esc_attr( $secret( 'mail_baby_api_key' ) ); ?>" autocomplete="new-password" />
							<?php
							$credential_help(
								'https://my.interserver.net/',
								__( 'Open API Access', 'wp-powersuite' ),
								__( 'an API Key', 'wp-powersuite' ),
								'mail-baby'
							);
							?>
						</td>
					</tr>

				<?php elseif ( $provider_id === 'amazon-ses' ) : ?>
					<?php
					$aws_regions   = AmazonSesProvider::regions();
					$aws_region    = $field( 'aws_region' ) !== '' ? $field( 'aws_region' ) : 'us-east-1';
					$aws_cred_src  = $field( 'aws_credential_source' ) !== '' ? $field( 'aws_credential_source' ) : 'keys';
					if ( ! isset( $aws_regions[ $aws_region ] ) ) {
						$aws_regions = [ $aws_region => $aws_region ] + $aws_regions;
					}
					?>
					<tr>
						<th scope="row"><label for="wpps_ed_aws_credential_source"><?php esc_html_e( 'Authentication', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_aws_credential_source" name="connection[fields][aws_credential_source]">
								<option value="keys" <?php selected( $aws_cred_src, 'keys' ); ?>><?php esc_html_e( 'Enter access keys below', 'wp-powersuite' ); ?></option>
								<option value="env" <?php selected( $aws_cred_src, 'env' ); ?>><?php esc_html_e( 'Use wp-config.php / environment variables', 'wp-powersuite' ); ?></option>
							</select>
							<p class="description">
								<?php esc_html_e( 'Use IAM access keys — not Amazon SES SMTP credentials.', 'wp-powersuite' ); ?>
								<button type="button" class="button-link wpps-ed-open-help-modal" data-wpps-ed-help="amazon-ses"><?php esc_html_e( 'Setup guide', 'wp-powersuite' ); ?></button>
							</p>
						</td>
					</tr>
					<tr class="wpps-ed-aws-keys-row">
						<th scope="row"><label for="wpps_ed_aws_key"><?php esc_html_e( 'Access key ID', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_aws_key" name="connection[fields][aws_access_key_id]" value="<?php echo esc_attr( $field( 'aws_access_key_id' ) ); ?>" autocomplete="off" />
							<?php
							$credential_help(
								'https://console.aws.amazon.com/iam/home#/users',
								__( 'Create IAM access key', 'wp-powersuite' ),
								__( 'an Access key ID', 'wp-powersuite' ),
								'amazon-ses'
							);
							?>
						</td>
					</tr>
					<tr class="wpps-ed-aws-keys-row">
						<th scope="row"><label for="wpps_ed_aws_secret"><?php esc_html_e( 'Secret access key', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="password" class="regular-text" id="wpps_ed_aws_secret" name="connection[fields][aws_secret_access_key]" value="<?php echo esc_attr( $secret( 'aws_secret_access_key' ) ); ?>" autocomplete="new-password" />
							<p class="description"><?php esc_html_e( 'Shown only once when the IAM access key is created.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr class="wpps-ed-aws-env-row">
						<th scope="row"><?php esc_html_e( 'Where to set keys', 'wp-powersuite' ); ?></th>
						<td>
							<p class="description" style="margin-top:0;">
								<?php
								echo wp_kses(
									sprintf(
										/* translators: %s: file name */
										__( 'Add these lines to %s (above the “That’s all, stop editing!” line). Keys are not stored in the WordPress database.', 'wp-powersuite' ),
										'<code>wp-config.php</code>'
									),
									[ 'code' => [] ]
								);
								?>
							</p>
							<pre class="wpps-ed-code-sample" tabindex="0"><?php
							echo esc_html(
								"define( 'WP_POWERSUITE_AWS_ACCESS_KEY_ID', 'AKIAEXAMPLEKEYID' );\n" .
								"define( 'WP_POWERSUITE_AWS_SECRET_ACCESS_KEY', 'yourSecretAccessKey' );"
							);
							?></pre>
							<p class="description">
								<?php
								echo wp_kses(
									sprintf(
										/* translators: 1: env var, 2: env var */
										__( 'Or set %1$s and %2$s in your host’s environment variables panel (cPanel, Plesk, Cloudways, etc.).', 'wp-powersuite' ),
										'<code>AWS_ACCESS_KEY_ID</code>',
										'<code>AWS_SECRET_ACCESS_KEY</code>'
									),
									[ 'code' => [] ]
								);
								?>
							</p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_aws_region"><?php esc_html_e( 'AWS region', 'wp-powersuite' ); ?></label></th>
						<td>
							<select id="wpps_ed_aws_region" name="connection[fields][aws_region]">
								<?php foreach ( $aws_regions as $region_code => $region_label ) : ?>
									<option value="<?php echo esc_attr( $region_code ); ?>" <?php selected( $aws_region, $region_code ); ?>>
										<?php echo esc_html( $region_label . ' — ' . $region_code ); ?>
									</option>
								<?php endforeach; ?>
							</select>
							<p class="description"><?php esc_html_e( 'Must match the region where your SES identities are verified.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
					<tr>
						<th scope="row"><label for="wpps_ed_aws_configuration_set"><?php esc_html_e( 'Configuration set', 'wp-powersuite' ); ?></label></th>
						<td>
							<input type="text" class="regular-text" id="wpps_ed_aws_configuration_set" name="connection[fields][aws_configuration_set]" value="<?php echo esc_attr( $field( 'aws_configuration_set' ) ); ?>" autocomplete="off" />
							<p class="description"><?php esc_html_e( 'Optional. Required only if you use delivery webhooks under Reliability.', 'wp-powersuite' ); ?></p>
						</td>
					</tr>
				<?php endif; ?>
					</tbody>
				<?php endif; ?>
				</table>
			</section>

			<div class="wpps-ed-conn-footer">
				<div class="wpps-ed-conn-footer__actions">
					<button type="submit" class="button button-primary"><?php echo $is_edit ? esc_html__( 'Update connection', 'wp-powersuite' ) : esc_html__( 'Save connection', 'wp-powersuite' ); ?></button>
					<button type="button" class="button" id="wpps-ed-test-connection" data-label="<?php esc_attr_e( 'Test Connection', 'wp-powersuite' ); ?>"><?php esc_html_e( 'Test Connection', 'wp-powersuite' ); ?></button>
					<button type="button" class="button wpps-ed-cancel-connection"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
				</div>
				<div id="wpps-ed-test-connection-feedback" class="wpps-ed-feedback" aria-live="polite"></div>
			</div>
		</form>

		<?php if ( $is_edit && $provider_id === 'google' ) : ?>
			<form id="wpps-ed-oauth-google" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="hidden">
				<?php wp_nonce_field( 'wpps_email_delivery_oauth', 'wpps_ed_oauth_nonce' ); ?>
				<input type="hidden" name="action" value="wpps_email_delivery_oauth_connect" />
				<input type="hidden" name="oauth_provider" value="google" />
				<input type="hidden" name="oauth_connection_id" value="<?php echo esc_attr( $conn_id ); ?>" />
			</form>
			<form id="wpps-ed-oauth-google-disconnect" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="hidden">
				<?php wp_nonce_field( 'wpps_email_delivery_oauth', 'wpps_ed_oauth_nonce' ); ?>
				<input type="hidden" name="action" value="wpps_email_delivery_oauth_disconnect" />
				<input type="hidden" name="oauth_provider" value="google" />
				<input type="hidden" name="oauth_connection_id" value="<?php echo esc_attr( $conn_id ); ?>" />
			</form>
		<?php endif; ?>

		<?php if ( $is_edit && $provider_id === 'microsoft' ) : ?>
			<form id="wpps-ed-oauth-microsoft" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="hidden">
				<?php wp_nonce_field( 'wpps_email_delivery_oauth', 'wpps_ed_oauth_nonce' ); ?>
				<input type="hidden" name="action" value="wpps_email_delivery_oauth_connect" />
				<input type="hidden" name="oauth_provider" value="microsoft" />
				<input type="hidden" name="oauth_connection_id" value="<?php echo esc_attr( $conn_id ); ?>" />
			</form>
			<form id="wpps-ed-oauth-microsoft-disconnect" method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="hidden">
				<?php wp_nonce_field( 'wpps_email_delivery_oauth', 'wpps_ed_oauth_nonce' ); ?>
				<input type="hidden" name="action" value="wpps_email_delivery_oauth_disconnect" />
				<input type="hidden" name="oauth_provider" value="microsoft" />
				<input type="hidden" name="oauth_connection_id" value="<?php echo esc_attr( $conn_id ); ?>" />
			</form>
		<?php endif; ?>

		<?php if ( $provider_id === 'google' ) : ?>
			<div id="wpps-ed-help-modal-google" class="wpps-ed-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="wpps-ed-help-google-title">
				<div class="wpps-ed-modal-dialog" role="document">
					<div class="wpps-ed-modal-header">
						<h2 id="wpps-ed-help-google-title"><?php esc_html_e( 'Gmail / Google Workspace setup', 'wp-powersuite' ); ?></h2>
						<button type="button" class="button-link wpps-ed-modal-close" data-wpps-ed-close title="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							<span class="screen-reader-text"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></span>
						</button>
					</div>
					<div class="wpps-ed-modal-body">
						<p><?php esc_html_e( 'Sends via the Gmail API (not SMTP). Similar to other WordPress mailers that use Google OAuth — no app password required.', 'wp-powersuite' ); ?></p>
						<ol class="wpps-ed-help-list">
							<li><?php esc_html_e( 'In Google Cloud Console, create (or select) a project and enable the Gmail API.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Configure the OAuth consent screen. For personal Gmail, add yourself as a test user while the app is in Testing.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Create OAuth client credentials of type Web application.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Paste the Redirect URI from this screen into Authorized redirect URIs, then save Client ID and Client secret here.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Save the connection, click Connect Google, and approve gmail.send + account email.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Set the WordPress From address to the connected Google account (or an allowed Workspace alias).', 'wp-powersuite' ); ?></li>
						</ol>
						<p>
							<a href="https://developers.google.com/workspace/gmail/api/guides/sending" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Gmail API sending guide', 'wp-powersuite' ); ?></a>
							·
							<a href="https://developers.google.com/workspace/gmail/api/auth/scopes" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'OAuth scopes', 'wp-powersuite' ); ?></a>
						</p>
					</div>
					<div class="wpps-ed-modal-footer">
						<button type="button" class="button button-primary" data-wpps-ed-close><?php esc_html_e( 'Got it', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		<?php endif; ?>

		<?php if ( $provider_id === 'microsoft' ) : ?>
			<div id="wpps-ed-help-modal-microsoft" class="wpps-ed-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="wpps-ed-help-microsoft-title">
				<div class="wpps-ed-modal-dialog" role="document">
					<div class="wpps-ed-modal-header">
						<h2 id="wpps-ed-help-microsoft-title"><?php esc_html_e( 'Microsoft 365 / Outlook setup', 'wp-powersuite' ); ?></h2>
						<button type="button" class="button-link wpps-ed-modal-close" data-wpps-ed-close title="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							<span class="screen-reader-text"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></span>
						</button>
					</div>
					<div class="wpps-ed-modal-body">
						<p><?php esc_html_e( 'Uses Microsoft Graph sendMail with delegated OAuth (user signs in once). Messages are sent as that mailbox and saved to Sent Items.', 'wp-powersuite' ); ?></p>
						<ol class="wpps-ed-help-list">
							<li><?php esc_html_e( 'In Microsoft Entra ID → App registrations, create a new registration (or use an existing app).', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Add a Web platform redirect URI matching the value shown on this screen.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Under API permissions, add Microsoft Graph delegated: Mail.Send, User.Read, and offline_access. Grant admin consent if your tenant requires it.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Create a client secret, then paste Application (client) ID, secret, and tenant here.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Save the connection, click Connect Microsoft, and sign in with the mailbox that should send WordPress email.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Set From to that mailbox address (shared mailbox / send-as needs extra Graph rights and is not configured here).', 'wp-powersuite' ); ?></li>
						</ol>
						<p>
							<a href="https://learn.microsoft.com/en-us/graph/api/user-sendmail" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Graph sendMail docs', 'wp-powersuite' ); ?></a>
							·
							<a href="https://learn.microsoft.com/en-us/graph/auth-v2-user" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Delegated auth', 'wp-powersuite' ); ?></a>
						</p>
					</div>
					<div class="wpps-ed-modal-footer">
						<button type="button" class="button button-primary" data-wpps-ed-close><?php esc_html_e( 'Got it', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		<?php endif; ?>

		<?php if ( $provider_id === 'sendgrid' ) : ?>
			<div id="wpps-ed-help-modal-sendgrid" class="wpps-ed-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="wpps-ed-help-sendgrid-title">
				<div class="wpps-ed-modal-dialog" role="document">
					<div class="wpps-ed-modal-header">
						<h2 id="wpps-ed-help-sendgrid-title"><?php esc_html_e( 'SendGrid setup', 'wp-powersuite' ); ?></h2>
						<button type="button" class="button-link wpps-ed-modal-close" data-wpps-ed-close title="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							<span class="screen-reader-text"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></span>
						</button>
					</div>
					<div class="wpps-ed-modal-body">
						<ol class="wpps-ed-help-list">
							<li><?php esc_html_e( 'In SendGrid, go to Settings → API Keys and create a key with Mail Send permission.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Verify your From address or domain under Settings → Sender Authentication.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Paste the API key here and choose Global or EU to match your SendGrid account region.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Set the WordPress From address to a verified sender, then use Test Connection.', 'wp-powersuite' ); ?></li>
						</ol>
						<p>
							<a href="https://www.twilio.com/docs/sendgrid/ui/account-and-settings/api-keys" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'API keys guide', 'wp-powersuite' ); ?></a>
							·
							<a href="https://www.twilio.com/docs/sendgrid/ui/account-and-settings/how-to-set-up-domain-authentication" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Sender authentication', 'wp-powersuite' ); ?></a>
						</p>
					</div>
					<div class="wpps-ed-modal-footer">
						<button type="button" class="button button-primary" data-wpps-ed-close><?php esc_html_e( 'Got it', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		<?php endif; ?>

		<?php if ( $provider_id === 'amazon-ses' ) : ?>
			<div id="wpps-ed-help-modal-amazon-ses" class="wpps-ed-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="wpps-ed-help-amazon-ses-title">
				<div class="wpps-ed-modal-dialog" role="document">
					<div class="wpps-ed-modal-header">
						<h2 id="wpps-ed-help-amazon-ses-title"><?php esc_html_e( 'Amazon SES setup', 'wp-powersuite' ); ?></h2>
						<button type="button" class="button-link wpps-ed-modal-close" data-wpps-ed-close title="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							<span class="screen-reader-text"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></span>
						</button>
					</div>
					<div class="wpps-ed-modal-body">
						<p><?php esc_html_e( 'New Amazon SES accounts start in the sandbox and can only send to verified addresses until production access is approved.', 'wp-powersuite' ); ?></p>
						<ol class="wpps-ed-help-list">
							<li><?php esc_html_e( 'In the AWS Console, open Amazon SES in your region and verify your From domain or email address.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Create an IAM user with permission to send email through SES, then create an access key for that user.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Choose “Enter access keys below” and paste the Access key ID and Secret, or choose “Use wp-config.php / environment variables” and add the defines shown on that option.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Select the same AWS region where your SES identities are verified.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'Optional: add a configuration set if you want bounce/complaint events under Reliability.', 'wp-powersuite' ); ?></li>
							<li><?php esc_html_e( 'When ready for production, request production access in the SES console so you can email any recipient.', 'wp-powersuite' ); ?></li>
						</ol>
						<p><?php esc_html_e( 'Do not paste SES SMTP username/password here — those are different from IAM access keys.', 'wp-powersuite' ); ?></p>
						<p>
							<a href="https://docs.aws.amazon.com/ses/latest/dg/creating-identities.html" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Verify identities', 'wp-powersuite' ); ?></a>
							·
							<a href="https://docs.aws.amazon.com/ses/latest/dg/request-production-access.html" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Leave the sandbox', 'wp-powersuite' ); ?></a>
						</p>
					</div>
					<div class="wpps-ed-modal-footer">
						<button type="button" class="button button-primary" data-wpps-ed-close><?php esc_html_e( 'Got it', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		<?php endif; ?>

		<?php if ( $provider_id === 'mail-baby' ) : ?>
			<div id="wpps-ed-help-modal-mail-baby" class="wpps-ed-modal" aria-hidden="true" role="dialog" aria-modal="true" aria-labelledby="wpps-ed-help-mail-baby-title">
				<div class="wpps-ed-modal-dialog" role="document">
					<div class="wpps-ed-modal-header">
						<h2 id="wpps-ed-help-mail-baby-title"><?php esc_html_e( 'Mail.baby setup instructions', 'wp-powersuite' ); ?></h2>
						<button type="button" class="button-link wpps-ed-modal-close" data-wpps-ed-close title="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">
							<span class="dashicons dashicons-no-alt" aria-hidden="true"></span>
							<span class="screen-reader-text"><?php esc_html_e( 'Close', 'wp-powersuite' ); ?></span>
						</button>
					</div>
					<div class="wpps-ed-modal-body">
						<p>
							<strong><?php esc_html_e( '1. API key', 'wp-powersuite' ); ?></strong><br />
							<?php esc_html_e( 'Create or copy it from my.interserver.net → Settings → API Access.', 'wp-powersuite' ); ?>
						</p>
						<p>
							<strong><?php esc_html_e( '2. From email', 'wp-powersuite' ); ?></strong><br />
							<?php esc_html_e( 'Use an address on a domain you control (set in Sender above). Test Connection only checks the key; sending also requires DNS on that domain.', 'wp-powersuite' ); ?>
						</p>
						<p>
							<strong><?php esc_html_e( '3. Authorize the domain for API (required)', 'wp-powersuite' ); ?></strong><br />
							<?php esc_html_e( 'Without this, sends fail with “domain is not configured for API”.', 'wp-powersuite' ); ?>
						</p>
						<ol class="wpps-ed-help-list">
							<li>
								<?php
								printf(
									/* translators: 1: DNS host example, 2: DNS type, 3: short host */
									esc_html__( 'Add a %2$s record named %1$s (or just %3$s in your DNS panel).', 'wp-powersuite' ),
									'<code>_mb.yourdomain.com</code>',
									'<code>TXT</code>',
									'<code>_mb</code>'
								);
								?>
							</li>
							<li><?php esc_html_e( 'Set the value to your Mail.baby username (shown in your Mail.baby account, usually starts with “mb”).', 'wp-powersuite' ); ?></li>
							<li>
								<?php
								printf(
									/* translators: %s: SPF include fragment */
									esc_html__( 'Ensure SPF includes %s.', 'wp-powersuite' ),
									'<code>include:spf-c.mailbaby.net</code>'
								);
								?>
							</li>
							<li><?php esc_html_e( 'Wait for DNS to propagate, then send a test email.', 'wp-powersuite' ); ?></li>
						</ol>
						<p>
							<a href="https://www.mail.baby/apiauth" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Full guide: mail.baby/apiauth', 'wp-powersuite' ); ?></a>
							·
							<a href="https://www.mail.baby/apidoc.html" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'API docs', 'wp-powersuite' ); ?></a>
						</p>
					</div>
					<div class="wpps-ed-modal-footer">
						<button type="button" class="button button-primary" data-wpps-ed-close><?php esc_html_e( 'Got it', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		<?php endif; ?>
	</div>
</div>
