<?php
/**
 * Webhook setup view template.
 *
 * Variables in scope (passed by Admin_Settings::render_webhook_view()):
 *
 * @var \DonationRouter\PayPal_Account $account
 * @var string                                           $webhook_url
 * @var string                                           $back_url
 * @var string[]                                         $event_types
 *
 * @since   1.0.0
 * @package DonationRouter
 */

declare( strict_types=1 );

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

?>
<div class="ador-webhook-setup wrap">
	<h2>
		<?php
		printf(
			/* translators: %s: account label */
			esc_html__( 'Webhook Setup — %s', 'alamin-donation-router' ),
			esc_html( $account->account_label )
		);
		?>
	</h2>

	<p>
		<?php esc_html_e( 'Register this URL as a webhook in your PayPal developer app. This plugin will only handle webhooks for the account listed above; your primary GiveWP account continues to use its own webhook endpoint.', 'alamin-donation-router' ); ?>
	</p>

	<table class="form-table" role="presentation">
		<tr>
			<th scope="row"><?php esc_html_e( 'Webhook URL', 'alamin-donation-router' ); ?></th>
			<td>
				<div class="ador-webhook-url-row">
					<code id="ador-webhook-url"><?php echo esc_url( $webhook_url ); ?></code>
					<button type="button" class="ador-copy-btn" data-target="#ador-webhook-url">
						<?php echo ador_icon( 'copy' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<?php esc_html_e( 'Copy', 'alamin-donation-router' ); ?>
					</button>
				</div>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Webhook ID', 'alamin-donation-router' ); ?></th>
			<td>
				<?php if ( $account->webhook_id ) : ?>
					<code><?php echo esc_html( $account->webhook_id ); ?></code>
				<?php else : ?>
					<em><?php esc_html_e( 'Not yet registered. Use the button below to register this account’s webhook with PayPal.', 'alamin-donation-router' ); ?></em>
				<?php endif; ?>
			</td>
		</tr>
		<tr>
			<th scope="row"><?php esc_html_e( 'Subscribe to these events', 'alamin-donation-router' ); ?></th>
			<td>
				<ul class="ador-event-list">
					<?php foreach ( $event_types as $event ) : // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound ?>
						<li><?php echo esc_html( $event ); ?></li>
					<?php endforeach; ?>
				</ul>
			</td>
		</tr>
	</table>

	<?php
	$ador_register_url = wp_nonce_url(
		add_query_arg(
			array(
				'action'     => \DonationRouter\Onboarding_Handler::ACTION_REGISTER_WEBHOOK,
				'account_id' => $account->id,
			),
			admin_url( 'admin-post.php' )
		),
		\DonationRouter\Onboarding_Handler::ACTION_REGISTER_WEBHOOK . '_' . $account->id
	);
	?>
	<p>
		<a href="<?php echo esc_url( $ador_register_url ); ?>" class="button button-primary">
			<?php
			echo $account->webhook_id
				? esc_html__( 'Re-register webhook with PayPal', 'alamin-donation-router' )
				: esc_html__( 'Register webhook with PayPal', 'alamin-donation-router' );
			?>
		</a>
		<span class="description">
			<?php esc_html_e( 'Registers this URL and the required events on PayPal using the account’s own credentials.', 'alamin-donation-router' ); ?>
		</span>
	</p>

	<p><a href="<?php echo esc_url( $back_url ); ?>" class="button">&larr; <?php esc_html_e( 'Back to Accounts', 'alamin-donation-router' ); ?></a></p>
</div>
