<?php
/**
 * Settings tab — connections list + add-connection provider picker.
 *
 * @package EmailSmtp
 * @var array<string, mixed> $settings
 * @var array<string, array<string, mixed>> $connections
 * @var list<array{id:string,name:string,description:string,transport_label:string,recommended_use:string}> $providers
 * @var string $page_url
 * @var string $edit_connection_id
 * @var string $pick_provider
 */

use EmailSmtp\Includes\ConnectionStore;
use EmailSmtp\Includes\ProviderRegistry;
use EmailSmtp\Includes\SmtpRelayProfiles;
use EmailSmtp\Includes\Webhooks\WebhookSecurity;
use EmailSmtp\Module;

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

$webhook_secret_display      = ! empty( $settings['webhook_secret'] ) ? Module::getPasswordMask() : '';
$webhook_bird_secret_display = ! empty( $settings['webhook_bird_signing_secret'] ) ? Module::getPasswordMask() : '';
$default_id                  = sanitize_key( (string) ( $settings['default_connection_id'] ?? '' ) );
$fallback_id            = sanitize_key( (string) ( $settings['fallback_connection_id'] ?? '' ) );

$form_provider   = $pick_provider;
$form_connection = [];
if ( $edit_connection_id !== '' && isset( $connections[ $edit_connection_id ] ) ) {
	$form_connection = $connections[ $edit_connection_id ];
	$form_provider   = sanitize_key( (string) ( $form_connection['provider'] ?? '' ) );
}

$show_form       = $form_provider !== '' && in_array( $form_provider, ProviderRegistry::ids(), true );
$has_connections = $connections !== [];
// Show add-connection UI when there are no connections yet, or when picking/editing a provider.
$show_add = ! $has_connections || $show_form || $pick_provider !== '';
?>

<div class="wpps-ed-settings-layout">
<div class="wpps-ed-settings-main">

<?php if ( $has_connections ) : ?>
	<div class="wpps-ed-panel wpps-ed-connections-panel" id="wpps-ed-connections-panel" <?php echo $show_add && ! $edit_connection_id ? 'hidden' : ''; ?>>
		<div class="wpps-ed-panel-title wpps-ed-panel-title--row">
			<span><?php esc_html_e( 'Your connections', 'wp-powersuite' ); ?></span>
			<button type="button" class="button button-primary wpps-ed-show-add-connection">
				<?php esc_html_e( 'Add connection', 'wp-powersuite' ); ?>
			</button>
		</div>
		<div class="wpps-ed-panel-body" style="padding:0;">
			<p class="description" style="padding:12px 16px;margin:0;border-bottom:1px solid #dcdcde;">
				<?php esc_html_e( 'Outgoing mail is routed automatically to the connection whose From email matches the message. Primary is used when no From matches. Fallback is tried once if the primary send fails before accept.', 'wp-powersuite' ); ?>
			</p>
			<table class="widefat striped wpps-ed-connections-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Name', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'Provider', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'From email', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'Role', 'wp-powersuite' ); ?></th>
						<th><?php esc_html_e( 'Actions', 'wp-powersuite' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $connections as $cid => $conn ) : ?>
						<?php
						$pid  = sanitize_key( (string) ( $conn['provider'] ?? '' ) );
						$prov = ProviderRegistry::get( $pid );
						$icon = ConnectionStore::provider_icon_url( $pid );
						$is_default  = ( $cid === $default_id );
						$is_fallback = ( $cid === $fallback_id );
						$delete_url = wp_nonce_url(
							add_query_arg(
								[
									'action'     => 'wpps_email_delivery_delete_connection',
									'connection' => $cid,
								],
								admin_url( 'admin-post.php' )
							),
							'wpps_email_delivery_delete_connection'
						);
						$primary_url = wp_nonce_url(
							add_query_arg(
								[
									'action'     => 'wpps_email_delivery_set_connection_role',
									'connection' => $cid,
									'role'       => 'primary',
								],
								admin_url( 'admin-post.php' )
							),
							'wpps_email_delivery_set_connection_role'
						);
						$fallback_url = wp_nonce_url(
							add_query_arg(
								[
									'action'     => 'wpps_email_delivery_set_connection_role',
									'connection' => $cid,
									'role'       => 'fallback',
								],
								admin_url( 'admin-post.php' )
							),
							'wpps_email_delivery_set_connection_role'
						);
						$clear_fallback_url = wp_nonce_url(
							add_query_arg(
								[
									'action'     => 'wpps_email_delivery_set_connection_role',
									'connection' => $cid,
									'role'       => 'clear_fallback',
								],
								admin_url( 'admin-post.php' )
							),
							'wpps_email_delivery_set_connection_role'
						);
						?>
						<tr class="<?php echo $cid === $edit_connection_id ? 'is-editing' : ''; ?>">
							<td><strong><?php echo esc_html( (string) ( $conn['name'] ?? $cid ) ); ?></strong></td>
							<td>
								<span class="wpps-ed-conn-provider">
									<?php if ( $icon !== '' ) : ?>
										<img src="<?php echo esc_url( $icon ); ?>" alt="" class="wpps-ed-provider-icon wpps-ed-provider-icon--sm" />
									<?php endif; ?>
									<span>
										<?php echo esc_html( $prov ? $prov->get_name() : $pid ); ?>
										<?php
										$conn_transport = sanitize_key( (string) ( $conn['transport'] ?? 'api' ) );
										if ( $pid === 'smtp' || ( SmtpRelayProfiles::supports( $pid ) && $conn_transport === 'smtp' ) ) {
											echo ' <span class="wpps-ed-muted">(' . esc_html__( 'SMTP', 'wp-powersuite' ) . ')</span>';
										} elseif ( $prov && $prov->uses_api_transport() ) {
											echo ' <span class="wpps-ed-muted">(' . esc_html__( 'API', 'wp-powersuite' ) . ')</span>';
										}
										?>
									</span>
								</span>
							</td>
							<td><?php echo esc_html( (string) ( $conn['from_email'] ?? '' ) ); ?></td>
							<td>
								<?php if ( $is_default ) : ?>
									<span class="wpps-ed-role-badge wpps-ed-role-badge--primary"><?php esc_html_e( 'Primary', 'wp-powersuite' ); ?></span>
								<?php endif; ?>
								<?php if ( $is_fallback ) : ?>
									<span class="wpps-ed-role-badge wpps-ed-role-badge--fallback"><?php esc_html_e( 'Fallback', 'wp-powersuite' ); ?></span>
								<?php endif; ?>
								<?php if ( ! $is_default && ! $is_fallback ) : ?>
									<span class="wpps-ed-muted">—</span>
								<?php endif; ?>
							</td>
							<td class="wpps-ed-conn-actions-cell">
								<div class="wpps-ed-conn-actions">
									<button
										type="button"
										class="wpps-ed-conn-action wpps-ed-edit-connection"
										data-connection="<?php echo esc_attr( $cid ); ?>"
										data-provider="<?php echo esc_attr( $pid ); ?>"
										title="<?php esc_attr_e( 'Edit', 'wp-powersuite' ); ?>"
										aria-label="<?php esc_attr_e( 'Edit', 'wp-powersuite' ); ?>"
									>
										<span class="dashicons dashicons-edit" aria-hidden="true"></span>
									</button>
									<?php if ( ! $is_default ) : ?>
										<a
											href="<?php echo esc_url( $primary_url ); ?>"
											class="wpps-ed-conn-action wpps-ed-conn-action--primary"
											title="<?php esc_attr_e( 'Make primary', 'wp-powersuite' ); ?>"
											aria-label="<?php esc_attr_e( 'Make primary', 'wp-powersuite' ); ?>"
										>
											<span class="dashicons dashicons-star-filled" aria-hidden="true"></span>
										</a>
									<?php endif; ?>
									<?php if ( ! $is_fallback && ! $is_default ) : ?>
										<a
											href="<?php echo esc_url( $fallback_url ); ?>"
											class="wpps-ed-conn-action wpps-ed-conn-action--fallback"
											title="<?php esc_attr_e( 'Use as fallback', 'wp-powersuite' ); ?>"
											aria-label="<?php esc_attr_e( 'Use as fallback', 'wp-powersuite' ); ?>"
										>
											<span class="dashicons dashicons-backup" aria-hidden="true"></span>
										</a>
									<?php elseif ( $is_fallback ) : ?>
										<a
											href="<?php echo esc_url( $clear_fallback_url ); ?>"
											class="wpps-ed-conn-action wpps-ed-conn-action--clear-fallback"
											title="<?php esc_attr_e( 'Clear fallback', 'wp-powersuite' ); ?>"
											aria-label="<?php esc_attr_e( 'Clear fallback', 'wp-powersuite' ); ?>"
										>
											<span class="dashicons dashicons-undo" aria-hidden="true"></span>
										</a>
									<?php endif; ?>
									<a
										href="<?php echo esc_url( $delete_url ); ?>"
										class="wpps-ed-conn-action wpps-ed-conn-action--delete"
										title="<?php esc_attr_e( 'Delete', 'wp-powersuite' ); ?>"
										aria-label="<?php esc_attr_e( 'Delete', 'wp-powersuite' ); ?>"
										onclick="return confirm('<?php echo esc_js( __( 'Delete this connection?', 'wp-powersuite' ) ); ?>');"
									>
										<span class="dashicons dashicons-trash" aria-hidden="true"></span>
									</a>
								</div>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>
	</div>
<?php endif; ?>

<div
	class="wpps-ed-panel wpps-ed-add-panel"
	id="wpps-ed-add-panel"
	<?php echo $show_add ? '' : 'hidden'; ?>
	data-has-connections="<?php echo $has_connections ? '1' : '0'; ?>"
>
	<div class="wpps-ed-panel-title wpps-ed-panel-title--row">
		<span><?php esc_html_e( 'Add connection', 'wp-powersuite' ); ?></span>
		<?php if ( $has_connections ) : ?>
			<button type="button" class="button wpps-ed-hide-add-connection"><?php esc_html_e( 'Back to connections', 'wp-powersuite' ); ?></button>
		<?php endif; ?>
	</div>
	<div class="wpps-ed-panel-body">
		<?php if ( ! $has_connections ) : ?>
			<p class="description"><?php esc_html_e( 'Add your first connection. Each connection needs a unique From email — mail is routed automatically by that address.', 'wp-powersuite' ); ?></p>
		<?php else : ?>
			<p class="description"><?php esc_html_e( 'Click a provider to configure it below. From emails must be unique across connections.', 'wp-powersuite' ); ?></p>
		<?php endif; ?>
		<div class="wpps-ed-provider-grid" role="list">
			<?php foreach ( $providers as $provider ) : ?>
				<?php
				$pid    = sanitize_key( (string) ( $provider['id'] ?? '' ) );
				$name   = (string) ( $provider['name'] ?? $pid );
				$icon   = ConnectionStore::provider_icon_url( $pid );
				$active = $show_form && ! $edit_connection_id && $form_provider === $pid;
				?>
				<button
					type="button"
					class="wpps-ed-provider-tile <?php echo $active ? 'is-active' : ''; ?> <?php echo $pid === 'wordpress' ? 'is-caution' : ''; ?>"
					data-provider="<?php echo esc_attr( $pid ); ?>"
					role="listitem"
				>
					<span class="wpps-ed-provider-tile__logo">
						<?php if ( $icon !== '' ) : ?>
							<img src="<?php echo esc_url( $icon ); ?>" alt="<?php echo esc_attr( $name ); ?>" class="wpps-ed-provider-icon" />
						<?php endif; ?>
					</span>
					<span class="wpps-ed-provider-tile__name"><?php echo esc_html( $name ); ?></span>
					<span class="wpps-ed-provider-tile__meta"><?php echo esc_html( (string) ( $provider['transport_label'] ?? '' ) ); ?></span>
				</button>
			<?php endforeach; ?>
		</div>
	</div>
</div>

<div id="wpps-ed-connection-form-mount" class="wpps-ed-connection-form-mount" aria-live="polite">
	<?php if ( $show_form ) : ?>
		<?php
		$provider_id = $form_provider;
		$connection  = $form_connection;
		include __DIR__ . '/connection-form.php';
		?>
	<?php endif; ?>
</div>

<?php
$email_logs_on = ! empty( $email_logs_enabled );
$fallback_hint = $fallback_id !== ''
	? __( 'A fallback connection is already selected in Your connections.', 'wp-powersuite' )
	: __( 'Tip: in Your connections, use “Use as fallback” on a second connection so mail can try a backup if the primary fails before accept.', 'wp-powersuite' );
?>
<details class="wpps-ed-panel wpps-ed-details-panel wpps-ed-reliability-panel" <?php echo ! empty( $settings['simulation_mode'] ) && (string) $settings['simulation_mode'] !== '0' ? 'open' : ''; ?>>
	<summary class="wpps-ed-panel-title wpps-ed-reliability-summary">
		<span class="wpps-ed-reliability-summary__text">
			<span class="wpps-ed-reliability-summary__title"><?php esc_html_e( 'Reliability & webhooks', 'wp-powersuite' ); ?></span>
			<span class="wpps-ed-reliability-summary__sub"><?php esc_html_e( 'Optional — simulation, retries, alerts, and delivery status updates', 'wp-powersuite' ); ?></span>
		</span>
	</summary>
	<div class="wpps-ed-panel-body wpps-ed-reliability-body">
		<p class="wpps-ed-reliability-intro">
			<?php esc_html_e( 'These options help when mail fails, when you want to be notified, and when you want bounce/delivered status from your provider. Leave them off if you only need basic sending.', 'wp-powersuite' ); ?>
		</p>
		<p class="description wpps-ed-reliability-fallback-note"><?php echo esc_html( $fallback_hint ); ?></p>

		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="wpps-ed-reliability-form">
			<?php wp_nonce_field( 'wpps_email_delivery_save' ); ?>
			<input type="hidden" name="action" value="wpps_email_delivery_save" />

			<section class="wpps-ed-feature-card">
				<div class="wpps-ed-feature-card__head">
					<h3 class="wpps-ed-feature-card__title"><?php esc_html_e( 'Email Simulation', 'wp-powersuite' ); ?></h3>
					<p class="wpps-ed-feature-card__why">
						<?php esc_html_e( 'Disable sending all emails. If you enable this, no email will be sent but the email logs will be recorded.', 'wp-powersuite' ); ?>
					</p>
				</div>
				<div class="wpps-ed-feature-card__body">
					<label class="wpps-ed-check-card">
						<input type="hidden" name="settings[simulation_mode]" value="0" />
						<input type="checkbox" name="settings[simulation_mode]" value="1" <?php checked( (string) ( $settings['simulation_mode'] ?? '0' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Enable Email Simulation', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'All outbound mail is blocked site-wide. Use for staging or test environments. Turn off on production.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
					<?php if ( ! empty( $settings['simulation_mode'] ) && (string) $settings['simulation_mode'] !== '0' ) : ?>
						<p class="wpps-ed-feature-card__warn">
							<?php esc_html_e( 'Simulation is currently on — visitors and admins will not receive real email from this site.', 'wp-powersuite' ); ?>
						</p>
					<?php endif; ?>
					<?php if ( ! $email_logs_on ) : ?>
						<p class="description">
							<?php esc_html_e( 'Tip: enable Email Logs so simulated attempts appear in the log.', 'wp-powersuite' ); ?>
							<?php if ( ! empty( $email_logs_url ) ) : ?>
								<a href="<?php echo esc_url( $email_logs_url ); ?>"><?php esc_html_e( 'Open Email Logs', 'wp-powersuite' ); ?></a>
							<?php endif; ?>
						</p>
					<?php endif; ?>
				</div>
			</section>

			<section class="wpps-ed-feature-card">
				<div class="wpps-ed-feature-card__head">
					<h3 class="wpps-ed-feature-card__title"><?php esc_html_e( 'Immediate retries', 'wp-powersuite' ); ?></h3>
					<p class="wpps-ed-feature-card__why">
						<?php esc_html_e( 'If the provider times out or rate-limits for a moment, try again right away before giving up. Only runs before the provider accepts the message — never after a successful send.', 'wp-powersuite' ); ?>
					</p>
				</div>
				<div class="wpps-ed-feature-card__body">
					<label class="wpps-ed-field-label" for="wpps_ed_retry_max"><?php esc_html_e( 'Extra attempts', 'wp-powersuite' ); ?></label>
					<select id="wpps_ed_retry_max" name="settings[retry_max_attempts]">
						<?php foreach ( [ 0, 1, 2 ] as $n ) : ?>
							<option value="<?php echo esc_attr( (string) $n ); ?>" <?php selected( (string) ( $settings['retry_max_attempts'] ?? '0' ), (string) $n ); ?>>
								<?php
								echo esc_html(
									$n === 0
										? __( 'None — fail on first error', 'wp-powersuite' )
										: sprintf(
											/* translators: %d: count */
											_n( '%d extra attempt', '%d extra attempts', $n, 'wp-powersuite' ),
											$n
										)
								);
								?>
							</option>
						<?php endforeach; ?>
					</select>
					<p class="description"><?php esc_html_e( 'Works for both API and SMTP connections. Keep this low (1–2) so page loads are not delayed.', 'wp-powersuite' ); ?></p>
				</div>
			</section>

			<section class="wpps-ed-feature-card">
				<div class="wpps-ed-feature-card__head">
					<h3 class="wpps-ed-feature-card__title"><?php esc_html_e( 'Auto-resend failed mail', 'wp-powersuite' ); ?></h3>
					<p class="wpps-ed-feature-card__why">
						<?php esc_html_e( 'Later, an hourly background job can resend messages that already failed and are still stored in Email Logs (with a body). Useful after a short outage.', 'wp-powersuite' ); ?>
					</p>
				</div>
				<div class="wpps-ed-feature-card__body">
					<label class="wpps-ed-check-card">
						<input type="hidden" name="settings[auto_retry_failed]" value="0" />
						<input type="checkbox" name="settings[auto_retry_failed]" value="1" <?php checked( (string) ( $settings['auto_retry_failed'] ?? '0' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Enable hourly auto-resend', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'Up to 2 attempts per failed log that still has a stored body. Requires Email Logs with body logging on.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
					<?php if ( ! $email_logs_on ) : ?>
						<p class="wpps-ed-feature-card__warn">
							<?php esc_html_e( 'Email Logs is not enabled — turn it on (and store message bodies) for auto-resend to work.', 'wp-powersuite' ); ?>
						</p>
					<?php elseif ( ! empty( $email_logs_url ) ) : ?>
						<p class="description">
							<a href="<?php echo esc_url( $email_logs_url ); ?>"><?php esc_html_e( 'Open Email Logs settings', 'wp-powersuite' ); ?></a>
						</p>
					<?php endif; ?>
				</div>
			</section>

			<section class="wpps-ed-feature-card">
				<div class="wpps-ed-feature-card__head">
					<h3 class="wpps-ed-feature-card__title"><?php esc_html_e( 'Failure alerts', 'wp-powersuite' ); ?></h3>
					<p class="wpps-ed-feature-card__why">
						<?php esc_html_e( 'Get notified when a send hard-fails after retries/fallback. Alerts are rate-limited (about once every 15 minutes) so a burst of failures does not flood you.', 'wp-powersuite' ); ?>
					</p>
				</div>
				<div class="wpps-ed-feature-card__body">
					<label class="wpps-ed-check-card">
						<input type="hidden" name="settings[failure_alert_enabled]" value="0" />
						<input type="checkbox" name="settings[failure_alert_enabled]" value="1" <?php checked( (string) ( $settings['failure_alert_enabled'] ?? '0' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Send failure notifications', 'wp-powersuite' ); ?></strong>
							<em><?php esc_html_e( 'Email and/or chat webhook when delivery fails.', 'wp-powersuite' ); ?></em>
						</span>
					</label>
					<div class="wpps-ed-conn-grid wpps-ed-reliability-fields">
						<div class="wpps-ed-field">
							<label for="wpps_ed_failure_alert_email"><?php esc_html_e( 'Alert email', 'wp-powersuite' ); ?></label>
							<input type="email" class="regular-text" id="wpps_ed_failure_alert_email" name="settings[failure_alert_email]" value="<?php echo esc_attr( (string) ( $settings['failure_alert_email'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( (string) get_option( 'admin_email' ) ); ?>" />
							<p class="description"><?php esc_html_e( 'Defaults to the site admin email if left blank.', 'wp-powersuite' ); ?></p>
						</div>
						<div class="wpps-ed-field">
							<label for="wpps_ed_failure_alert_webhook"><?php esc_html_e( 'Slack / Discord webhook', 'wp-powersuite' ); ?></label>
							<input type="url" class="regular-text" id="wpps_ed_failure_alert_webhook" name="settings[failure_alert_webhook]" value="<?php echo esc_attr( (string) ( $settings['failure_alert_webhook'] ?? '' ) ); ?>" placeholder="https://hooks.slack.com/services/… or Discord webhook" autocomplete="off" />
							<p class="description"><?php esc_html_e( 'Optional. Paste an incoming webhook URL to post the same alert to chat.', 'wp-powersuite' ); ?></p>
						</div>
					</div>
				</div>
			</section>

			<section class="wpps-ed-feature-card">
				<div class="wpps-ed-feature-card__head">
					<h3 class="wpps-ed-feature-card__title"><?php esc_html_e( 'Delivery webhooks', 'wp-powersuite' ); ?></h3>
					<p class="wpps-ed-feature-card__why">
						<?php esc_html_e( 'Your mail provider can call WordPress after a message is delivered, bounced, or marked as spam. Those events update Email Logs so you see real inbox outcomes — not only “sent from WordPress”.', 'wp-powersuite' ); ?>
					</p>
				</div>
				<div class="wpps-ed-feature-card__body">
					<label class="wpps-ed-check-card">
						<input type="hidden" name="settings[webhook_enabled]" value="0" />
						<input type="checkbox" name="settings[webhook_enabled]" value="1" <?php checked( (string) ( $settings['webhook_enabled'] ?? '0' ), '1' ); ?> />
						<span>
							<strong><?php esc_html_e( 'Accept provider event webhooks', 'wp-powersuite' ); ?></strong>
							<em>
								<?php
								echo $email_logs_on
									? esc_html__( 'Statuses are written to Email Logs (delivered, bounced, complained, etc.).', 'wp-powersuite' )
									: esc_html__( 'Enable the Email Logs module to store delivery statuses from these events.', 'wp-powersuite' );
								?>
							</em>
						</span>
					</label>

					<div class="wpps-ed-conn-grid wpps-ed-reliability-fields">
						<div class="wpps-ed-field">
							<label for="wpps_ed_webhook_secret"><?php esc_html_e( 'Shared secret', 'wp-powersuite' ); ?></label>
							<input type="password" class="regular-text" id="wpps_ed_webhook_secret" name="settings[webhook_secret]" value="<?php echo esc_attr( $webhook_secret_display ); ?>" autocomplete="new-password" />
							<p class="description"><?php echo esc_html__( 'Create any long random string. Send it as Authorization: Bearer <secret> or X-WPPS-Webhook-Secret. Mailgun can use its signing key.', 'wp-powersuite' ); ?></p>
						</div>
						<div class="wpps-ed-field">
							<label for="wpps_ed_bird_whsec"><?php esc_html_e( 'Bird webhook signing secret', 'wp-powersuite' ); ?></label>
							<input type="password" class="regular-text" id="wpps_ed_bird_whsec" name="settings[webhook_bird_signing_secret]" value="<?php echo esc_attr( $webhook_bird_secret_display ); ?>" autocomplete="new-password" placeholder="whsec_…" />
							<p class="description"><?php esc_html_e( 'Shown once when you create a Bird webhook (whsec_…). Used for Standard Webhooks signature verification. Constant: WP_POWERSUITE_BIRD_WEBHOOK_SECRET.', 'wp-powersuite' ); ?></p>
						</div>
						<div class="wpps-ed-field">
							<label for="wpps_ed_sendgrid_vkey"><?php esc_html_e( 'SendGrid verification key', 'wp-powersuite' ); ?></label>
							<textarea class="large-text code" rows="3" id="wpps_ed_sendgrid_vkey" name="settings[webhook_sendgrid_verification_key]" placeholder="<?php esc_attr_e( 'Optional ECDSA public key for Signed Event Webhook', 'wp-powersuite' ); ?>"><?php echo esc_textarea( (string) ( $settings['webhook_sendgrid_verification_key'] ?? '' ) ); ?></textarea>
							<p class="description"><?php esc_html_e( 'Only needed if you use SendGrid’s signed event webhooks. Leave blank otherwise.', 'wp-powersuite' ); ?></p>
						</div>
					</div>

					<div class="wpps-ed-webhook-endpoints">
						<h4 class="wpps-ed-webhook-endpoints__title"><?php esc_html_e( 'Endpoint URLs', 'wp-powersuite' ); ?></h4>
						<p class="description">
							<?php esc_html_e( 'In your provider’s dashboard, add an event / webhook notification and paste the matching URL below. Use the shared secret (or provider signature) so only that provider can update your logs.', 'wp-powersuite' ); ?>
						</p>
						<ul class="wpps-ed-code-list">
							<?php
							$webhook_endpoints = [
								'sendgrid'   => __( 'SendGrid', 'wp-powersuite' ),
								'mailgun'    => __( 'Mailgun', 'wp-powersuite' ),
								'postmark'   => __( 'Postmark', 'wp-powersuite' ),
								'amazon-ses' => __( 'Amazon SES', 'wp-powersuite' ),
								'resend'     => __( 'Resend', 'wp-powersuite' ),
								'brevo'      => __( 'Brevo', 'wp-powersuite' ),
								'sparkpost'  => __( 'SparkPost (classic msys)', 'wp-powersuite' ),
								'bird'       => __( 'Bird Email', 'wp-powersuite' ),
							];
							foreach ( $webhook_endpoints as $wh_provider => $wh_label ) :
								$wh_url = WebhookSecurity::endpoint_url( $wh_provider );
								?>
								<li class="wpps-ed-copy-row">
									<strong><?php echo esc_html( $wh_label ); ?></strong>
									<code class="wpps-ed-callback-url" title="<?php echo esc_attr( $wh_url ); ?>"><?php echo esc_html( $wh_url ); ?></code>
									<button type="button" class="button button-small wpps-ed-copy" data-copy="<?php echo esc_attr( $wh_url ); ?>"><?php esc_html_e( 'Copy', 'wp-powersuite' ); ?></button>
								</li>
							<?php endforeach; ?>
						</ul>
					</div>
				</div>
			</section>

			<div class="wpps-ed-reliability-footer">
				<button type="submit" class="button button-primary"><?php esc_html_e( 'Save reliability settings', 'wp-powersuite' ); ?></button>
			</div>
		</form>
	</div>
</details>

</div><!-- .wpps-ed-settings-main -->
<?php include __DIR__ . '/sidebar-status.php'; ?>
</div><!-- .wpps-ed-settings-layout -->
