<?php
/**
 * Stripe Payouts Settings Panel Template
 *
 * @package     AffiliateWP
 * @subpackage  Payouts/Methods/StripePayouts
 * @copyright   Copyright (c) 2025, Sandhills Development, LLC
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       2.29.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Get current settings.
$test_mode = affiliate_wp()->settings->get( 'stripe_test_mode', false );

// API Secret keys.
$live_secret_key = affiliate_wp()->settings->get( 'stripe_live_secret_key', '' );
$test_secret_key = affiliate_wp()->settings->get( 'stripe_test_secret_key', '' );

// Webhook secret.
$webhook_secret = affiliate_wp()->settings->get( 'stripe_webhook_secret', '' );

// Check which credentials are configured.
$has_live_creds = ! empty( $live_secret_key );
$has_test_creds = ! empty( $test_secret_key );

// Determine connection status.
$is_connected = $test_mode ? $has_test_creds : $has_live_creds;

// Get Stripe Connect status.
$admin_connected = false;
if ( function_exists( 'affwp_stripe_payouts_is_admin_connected' ) ) {
	$admin_connected = affwp_stripe_payouts_is_admin_connected();
}

// Cross-border (international affiliate) payouts toggle state.
$cross_border_enabled = (bool) affiliate_wp()->settings->get( 'stripe_cross_border_payouts', false );

// Cross-border requires a US/UK Stripe platform. Only probe the platform country when connected
// (the helper makes an Account::retrieve call; it's cached per-request).
$cross_border_platform_ok = $admin_connected
	&& function_exists( 'affwp_stripe_payouts_platform_supports_cross_border' )
	&& affwp_stripe_payouts_platform_supports_cross_border();
?>

<div class="max-w-3xl" x-data="{
	testMode: <?php echo $test_mode ? 'true' : 'false'; ?>,

	intlPayoutsEnabled: <?php echo ( $cross_border_enabled && $cross_border_platform_ok ) ? 'true' : 'false'; ?>,
	intlPayoutsWasEnabled: <?php echo ( $cross_border_enabled && $cross_border_platform_ok ) ? 'true' : 'false'; ?>,

	init() {
		// Set global state for badge reactivity
		window.affwpStripeTestMode = this.testMode;

		// Watch for testMode changes
		this.$watch('testMode', (value) => {
			window.affwpStripeTestMode = value;
			// Dispatch custom event for other components
			window.dispatchEvent(new CustomEvent('stripe-mode-changed', {
				detail: { testMode: value }
			}));
			// Load balance when switching to test mode
			if (value) {
				this.loadCachedBalance();
			}
		});

		// Load balance on init if in test mode
		if (this.testMode) {
			this.loadCachedBalance();
		}

	},

	// Balance management
	currentBalance: '<?php echo esc_js( affwp_stripe_payouts_format_balance_amount( affwp_stripe_payouts_get_cached_balance() ? affwp_stripe_payouts_get_cached_balance() : [] ) ); ?>',
	balanceData: null,
	lastBalanceCheck: null,
	checkingBalance: false,
	balanceResult: null,
	balanceError: null,

	// Test charge state
	creatingCharge: false,
	chargeResult: null,
	chargeError: null,
	modalAmount: 0,
	modalInput: null,


	loadCachedBalance() {
		// Attempt to load balance from cache first
		const cachedBalance = '<?php echo esc_js( affwp_stripe_payouts_format_balance_amount( affwp_stripe_payouts_get_cached_balance() ? affwp_stripe_payouts_get_cached_balance() : [] ) ); ?>';
		// Always update the balance, even if it's $0.00 (which could be a valid balance).
		this.currentBalance = cachedBalance;
	},

	checkBalance() {
		this.checkingBalance = true;
		this.balanceResult = null;
		this.balanceError = null;

		const formData = new FormData();
		formData.append('action', 'affwp_stripe_check_balance');
		formData.append('nonce', '<?php echo esc_attr( wp_create_nonce( 'affwp_stripe_check_balance' ) ); ?>');

		fetch(ajaxurl, {
			method: 'POST',
			body: formData,
			credentials: 'same-origin'
		})
		.then(response => response.json())
		.then(data => {
			this.checkingBalance = false;
			if (data.success) {
				// Update current balance
				if (data.data.formatted) {
					this.currentBalance = data.data.formatted;
				}
				if (data.data.balance) {
					this.balanceData = data.data.balance;
				}
				this.lastBalanceCheck = new Date();
				// Show success briefly
				this.balanceResult = '<?php esc_html_e( 'Balance updated', 'affiliate-wp' ); ?>';
				setTimeout(() => {
					this.balanceResult = null;
				}, 3000);
			} else {
				this.balanceError = data.data.message || 'Failed to retrieve balance';
				setTimeout(() => {
					this.balanceError = null;
				}, 5000);
			}
		})
		.catch(error => {
			this.checkingBalance = false;
			this.balanceError = 'Network error occurred';
			console.error('Balance check error:', error);
			setTimeout(() => {
				this.balanceError = null;
			}, 5000);
		});
	},

	createTestCharge() {
		const amountInput = document.querySelector('#stripe-test-charge-amount');
		const amount = amountInput ? parseFloat(amountInput.value) : 10;

		if (!amount || isNaN(amount) || amount <= 0) {
			this.chargeError = 'Please enter a valid amount';
			setTimeout(() => {
				this.chargeError = null;
			}, 5000);
			return;
		}

		// Store amount for modal display
		this.modalAmount = amount;
		this.modalInput = amountInput;

		// Store process function in window for modal access
		window.stripeProcessTestCharge = () => {
			this.processTestCharge(this.modalAmount, this.modalInput);
		};

		// Open the modal with data passed through the store
		this.$store.modals.open('stripe-test-charge-modal', {
			chargeAmount: amount
		});
	},

	processTestCharge(amount, amountInput) {
		this.creatingCharge = true;
		this.chargeResult = null;
		this.chargeError = null;

		const balanceBeforeCharge = this.currentBalance;

		const formData = new FormData();
		formData.append('action', 'affwp_stripe_create_test_charge');
		formData.append('nonce', '<?php echo esc_attr( wp_create_nonce( 'affwp_stripe_create_test_charge' ) ); ?>');
		formData.append('amount', amount);

		fetch(ajaxurl, {
			method: 'POST',
			body: formData,
			credentials: 'same-origin'
		})
		.then(response => response.json())
		.then(data => {
			this.creatingCharge = false;
			if (data.success) {
				this.chargeResult = data.data.message;
				// Update the balance if provided
				if (data.data.new_balance) {
					this.currentBalance = data.data.new_balance;
				}
				if (data.data.balance_data) {
					this.balanceData = data.data.balance_data;
				}
				// Reset amount to default
				if (amountInput) {
					amountInput.value = '500';
				}
				// Stripe's Balance API can lag behind PaymentIntent confirmation, so re-check until the balance moves.
				if (this.currentBalance === balanceBeforeCharge) {
					this.pollBalanceUntilChanged(balanceBeforeCharge);
				}
			} else {
				this.chargeError = data.data.message || 'Failed to create test charge';
			}

			// Clear after 10 seconds
			setTimeout(() => {
				this.chargeResult = null;
				this.chargeError = null;
			}, 10000);
		})
		.catch(error => {
			this.creatingCharge = false;
			this.chargeError = 'Network error occurred';
			console.error('Test charge error:', error);
		});
	},

	pollBalanceUntilChanged(previousBalance) {
		const delays = [1000, 2000, 3000, 4000];
		let attempt = 0;

		const tick = () => {
			if (attempt >= delays.length) return;
			const delay = delays[attempt++];
			setTimeout(() => {
				this.refreshBalanceSilently().then(() => {
					if (this.currentBalance === previousBalance) {
						tick();
					}
				});
			}, delay);
		};
		tick();
	},

	refreshBalanceSilently() {
		const formData = new FormData();
		formData.append('action', 'affwp_stripe_check_balance');
		formData.append('nonce', '<?php echo esc_attr( wp_create_nonce( 'affwp_stripe_check_balance' ) ); ?>');

		return fetch(ajaxurl, {
			method: 'POST',
			body: formData,
			credentials: 'same-origin'
		})
		.then(response => response.json())
		.then(data => {
			if (data.success && data.data.formatted) {
				this.currentBalance = data.data.formatted;
				if (data.data.balance) {
					this.balanceData = data.data.balance;
				}
			}
		})
		.catch(() => {});
	}
}">

	<!-- Header Section -->
	<div class="mb-6">
		<h3 class="mb-2 text-xl font-semibold text-gray-900"><?php esc_html_e( 'Stripe Payouts Configuration', 'affiliate-wp' ); ?></h3>
		<p class="text-sm text-gray-600">
			<?php esc_html_e( 'Configure Stripe Connect to send instant payments to affiliates via bank transfer.', 'affiliate-wp' ); ?>
		</p>
	</div>

	<?php AffiliateWP_Admin_Payouts_Tab::render_affiliate_selectable_field( 'stripe' ); ?>

	<!-- Main Settings Container -->
	<div class="space-y-6">

		<!-- International Affiliate (Cross-Border) Payouts -->
		<div id="stripe-international-payouts" class="bg-white rounded-lg border border-gray-200">
			<div class="p-6">
				<div class="flex justify-between items-start<?php echo $cross_border_platform_ok ? ' cursor-pointer' : ''; ?>"<?php
				if ( $cross_border_platform_ok ) {
					// Row-level click-to-toggle; guarded so selecting text doesn't flip it.
					echo ' @click="if ( ! window.getSelection().isCollapsed ) return; intlPayoutsEnabled = !intlPayoutsEnabled; $refs.toggle_intl_payouts.querySelector(\'input[type=checkbox]\').click()"'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static Alpine handler; escaping would corrupt the directive.
				}
				?>>
					<div>
						<div class="flex flex-wrap gap-2 items-center mb-1">
							<h4 class="text-base font-medium text-gray-900"><?php esc_html_e( 'International Affiliate Payouts', 'affiliate-wp' ); ?></h4>
							<?php
							affwp_badge(
								[
									'text'    => esc_html__( 'US & UK', 'affiliate-wp' ),
									'variant' => 'info',
									'size'    => 'sm',
								]
							);

							if ( function_exists( 'affwp_feature_discovery' ) && affwp_feature_discovery()->should_show_badge( 'intl_affiliate_payouts' ) ) {
								printf(
									'<span class="text-[11px] font-semibold tracking-wider uppercase text-affwp-brand-600">%s</span>',
									esc_html__( 'New', 'affiliate-wp' )
								);
							}
							?>
						</div>
						<p class="mt-1 text-sm text-gray-600">
							<?php esc_html_e( 'Pay affiliates in other countries in their local currency, straight from your Stripe account.', 'affiliate-wp' ); ?>
						</p>
						<p class="mt-1 text-sm">
							<?php
							if ( function_exists( 'affwp_link' ) && function_exists( 'affwp_utm_link' ) ) {
								affwp_link(
									[
										'text'       => esc_html__( 'Learn more about international payouts', 'affiliate-wp' ),
										'href'       => affwp_utm_link( 'https://affiliatewp.com/docs/international-affiliate-payouts/', 'payouts-settings', 'intl-affiliate-payouts' ),
										'icon'       => 'share-up-right',
										'external'   => true,
										'attributes' => array( '@click.stop' => '' ),
									]
								);
							}
							?>
						</p>
						<?php if ( ! $admin_connected ) : ?>
							<p class="flex gap-1.5 items-start mt-3 text-sm text-gray-600">
								<?php \AffiliateWP\Utils\Icons::render( 'circle-info', '', [ 'class' => 'size-4 mt-0.5 shrink-0 text-gray-400' ] ); ?>
								<span><?php esc_html_e( 'Connect your Stripe account below to enable this. Available for US- and UK-based Stripe accounts.', 'affiliate-wp' ); ?></span>
							</p>
						<?php elseif ( ! $cross_border_platform_ok ) : ?>
							<p class="flex gap-1.5 items-start mt-3 text-sm text-gray-600">
								<?php \AffiliateWP\Utils\Icons::render( 'circle-info', '', [ 'class' => 'size-4 mt-0.5 shrink-0 text-gray-400' ] ); ?>
								<span><?php esc_html_e( 'International payouts are available for Stripe accounts based in the US or UK.', 'affiliate-wp' ); ?></span>
							</p>
						<?php endif; ?>
						<p class="mt-2 text-sm text-gray-500"
							x-show="intlPayoutsWasEnabled && ! intlPayoutsEnabled"
							x-transition.opacity
							x-cloak>
							<?php esc_html_e( 'Affiliates already connected keep getting paid. This only stops new international affiliates from connecting.', 'affiliate-wp' ); ?>
						</p>
					</div>
					<div class="flex items-center ml-4" @click.stop x-ref="toggle_intl_payouts"<?php
					if ( ! $cross_border_platform_ok ) {
						$intl_toggle_tooltip = $admin_connected
							? __( 'International payouts require a US- or UK-based Stripe account.', 'affiliate-wp' )
							: __( 'Connect your Stripe account to enable international payouts.', 'affiliate-wp' );
						printf(
							' data-tooltip-html="%s"',
							esc_attr( affwp_tooltip( $intl_toggle_tooltip ) )
						);
					}
					?>>
						<input type="hidden" name="affwp_settings[stripe_cross_border_payouts]" value="0" />
						<?php
						affwp_toggle(
							[
								'name'         => 'affwp_settings[stripe_cross_border_payouts]',
								'label'        => __( 'Enable international affiliate payouts', 'affiliate-wp' ),
								'checked'      => $cross_border_enabled && $cross_border_platform_ok,
								'disabled'     => ! $cross_border_platform_ok,
								'size'         => 'md',
								'color'        => 'blue',
								'alpine_model' => 'intlPayoutsEnabled',
							]
						);
						?>
					</div>
				</div>
			</div>
		</div>

		<!-- Live Credentials Section -->
		<div class="overflow-hidden bg-white rounded-lg border border-gray-200">
			<div class="relative p-6 transition-colors" :class="!testMode ? 'bg-green-50/20 border-l-4 border-green-500' : 'border-l-4 border-transparent'">
				<div class="mb-4">
					<h4 class="flex items-center text-base font-medium text-gray-900">
						<?php esc_html_e( 'Live Mode', 'affiliate-wp' ); ?>
						<?php
						affwp_badge(
							[
								'text'    => __( 'Active', 'affiliate-wp' ),
								'variant' => 'success',
								'size'    => 'sm',
								'class'   => 'ml-2',
								'alpine'  => [
									'show'       => '!testMode && ' . ( $has_live_creds ? 'true' : 'false' ),
									'cloak'      => true,
									'transition' => [
										'enter'       => 'transition ease-out duration-200',
										'enter-start' => 'opacity-0 scale-95 translate-x-2',
										'enter-end'   => 'opacity-100 scale-100 translate-x-0',
										'leave'       => 'transition ease-in duration-150',
										'leave-start' => 'opacity-100 scale-100 translate-x-0',
										'leave-end'   => 'opacity-0 scale-95 translate-x-2',
									],
								],
							]
						);
						?>
					</h4>
					<p class="mt-1 text-sm text-gray-600"><?php esc_html_e( 'Process payments using your live Stripe account', 'affiliate-wp' ); ?></p>
				</div>

				<div class="space-y-5">
					<div>
						<label for="stripe-live-secret" class="block mb-1 text-sm font-medium text-gray-700"><?php esc_html_e( 'Secret Key', 'affiliate-wp' ); ?></label>
						<div class="relative">
							<?php
							affwp_input(
								array(
									'id'          => 'stripe-live-secret',
									'name'        => 'affwp_settings[stripe_live_secret_key]',
									'type'        => 'password',
									'value'       => $live_secret_key,
									'placeholder' => __( 'sk_live_...', 'affiliate-wp' ),
									'secret'      => true,
								)
							);
							?>

						</div>
						<p class="mt-2 text-sm text-gray-500">
							<?php
							// affwp_render_link() returns pre-escaped HTML (esc_url for href, esc_attr for attributes).
							/* translators: %s: Link to Stripe Dashboard */
							printf(
								esc_html__( 'Find this in your %s under Developers → API keys.', 'affiliate-wp' ),
								affwp_render_link(
									[
										'text'     => esc_html__( 'Stripe Dashboard', 'affiliate-wp' ),
										'href'     => 'https://dashboard.stripe.com/apikeys',
										'external' => true,
										'variant'  => 'default',
									]
								)
							);
							?>
						</p>
					</div>
				</div>

			</div>
		</div>

		<!-- Sandbox Mode Section -->
		<div class="overflow-hidden bg-white rounded-lg border border-gray-200 transition-all duration-300"
			:class="testMode ? 'border-l-4 border-l-orange-500 bg-orange-50/20' : 'bg-white'">
			<!-- Sandbox Mode Header - Always visible -->
			<div class="p-6 transition-colors cursor-pointer"
				@click="testMode = !testMode">
				<div class="flex justify-between items-center">
					<div class="flex-1">
						<h4 class="flex items-center text-base font-medium text-gray-900">
							<?php esc_html_e( 'Sandbox Mode', 'affiliate-wp' ); ?>
							<?php
							affwp_badge(
								[
									'text'    => __( 'Active', 'affiliate-wp' ),
									'variant' => 'warning',
									'size'    => 'sm',
									'class'   => 'ml-2',
									'alpine'  => [
										'show'       => 'testMode && ' . ( $has_test_creds ? 'true' : 'false' ),
										'cloak'      => true,
										'transition' => [
											'enter'       => 'transition ease-out duration-200',
											'enter-start' => 'opacity-0 scale-95 translate-x-2',
											'enter-end'   => 'opacity-100 scale-100 translate-x-0',
											'leave'       => 'transition ease-in duration-150',
											'leave-start' => 'opacity-100 scale-100 translate-x-0',
											'leave-end'   => 'opacity-0 scale-95 translate-x-2',
										],
									],
								]
							);
							?>
						</h4>
						<p class="mt-1 text-sm text-gray-600"
							x-show="!testMode">
							<?php esc_html_e( 'Enable to test payments without real transactions', 'affiliate-wp' ); ?>
						</p>
						<p class="mt-1 text-sm text-gray-600"
							x-show="testMode">
							<?php esc_html_e( 'Process payments using Stripe Test Mode for testing', 'affiliate-wp' ); ?>
						</p>
					</div>
					<div class="flex items-center ml-4" @click.stop>
						<input type="hidden" name="affwp_settings[stripe_test_mode]" value="0" />
						<?php
						// Use the new toggle component.
						affwp_toggle(
							[
								'name'         => 'affwp_settings[stripe_test_mode]',
								'label'        => __( 'Enable test mode', 'affiliate-wp' ),
								'checked'      => $test_mode,
								'size'         => 'md',
								'color'        => 'blue',
								'alpine_model' => 'testMode',
							]
						);
						?>
					</div>
				</div>
			</div>

			<!-- Collapsible Content - Only visible when testMode is true -->
			<div x-show="testMode"
				x-collapse>
				<div class="px-6 pb-6 space-y-5">
					<div>
						<label for="stripe-test-secret" class="block mb-1 text-sm font-medium text-gray-700"><?php esc_html_e( 'Test Secret Key', 'affiliate-wp' ); ?></label>
						<div class="relative">
							<?php
							affwp_input(
								array(
									'id'          => 'stripe-test-secret',
									'name'        => 'affwp_settings[stripe_test_secret_key]',
									'type'        => 'password',
									'value'       => $test_secret_key,
									'placeholder' => __( 'sk_test_...', 'affiliate-wp' ),
									'secret'      => true,
								)
							);
							?>

						</div>
						<p class="mt-2 text-sm text-gray-500">
							<?php
							/* translators: %s: Link to Stripe Test Dashboard */
							printf(
								esc_html__( 'Find this in your %s under Developers → API keys.', 'affiliate-wp' ),
								affwp_render_link(
									[
										'text'     => esc_html__( 'Stripe Test Dashboard', 'affiliate-wp' ),
										'href'     => 'https://dashboard.stripe.com/test/apikeys',
										'external' => true,
										'variant'  => 'default',
									]
								)
							);
							?>
						</p>
					</div>


					<!-- Test Balance Management Section -->
					<div class="mt-6">
						<label class="block mb-1 text-sm font-medium text-gray-700"><?php esc_html_e( 'Available Test Balance', 'affiliate-wp' ); ?></label>

						<!-- Current Balance Display -->
						<div class="mb-4">
							<div class="flex items-center justify-between p-3 rounded-lg border border-gray-200">
								<div>

									<span class="ml-2 text-lg font-bold text-gray-900" x-html="currentBalance">$0.00</span>
								</div>
								<?php
								affwp_button(
									[
										'text'       => __( 'Refresh', 'affiliate-wp' ),
										'variant'    => 'secondary',
										'size'       => 'sm',
										'type'       => 'button',
										'attributes' => [
											'@click'    => 'checkBalance()',
											':disabled' => 'checkingBalance',
											'x-text'    => "checkingBalance ? '" . esc_html__( 'Refreshing...', 'affiliate-wp' ) . "' : '" . esc_html__( 'Refresh', 'affiliate-wp' ) . "'",
										],
									]
								);
								?>
							</div>

							<!-- Balance notifications -->

							<div x-show="balanceResult" x-transition class="mt-2 p-2 bg-green-50 rounded-md border border-green-200">
								<p class="text-sm text-green-700" x-text="balanceResult"></p>
							</div>
							<div x-show="balanceError" x-transition class="mt-2 p-2 bg-red-50 rounded-md border border-red-200">
								<p class="text-sm text-red-700" x-text="balanceError"></p>
							</div>
						</div>

						<!-- Add Test Funds -->
						<div class="mt-4">
							<label for="stripe-test-charge-amount" class="block mb-1 text-sm font-medium text-gray-700"><?php esc_html_e( 'Add Test Funds', 'affiliate-wp' ); ?></label>
							<div class="flex items-center space-x-2">
								<div class="focus:outline-hidden focus:ring-2 focus:ring-affwp-brand-500 hover:border-gray-400 flex items-center rounded-md bg-white pl-3 border-1 border-gray-300">
									<div class="shrink-0 text-base text-gray-500 select-none"><?php echo affwp_get_currency(); ?></div>
									<input
										id="stripe-test-charge-amount"
										type="number"
										name="test-charge-amount"
										value="500"
										min="1"
										max="10000"
										step="1"
										placeholder="500"
										class="block min-w-0 w-20 py-2 pr-3 pl-2 text-base text-gray-900 placeholder:text-gray-400 focus:outline-none" />
								</div>

								<?php
								affwp_button(
									[
										'text'       => esc_html__( 'Add Funds', 'affiliate-wp' ),
										'variant'    => 'secondary',
										'type'       => 'button',
										'size'       => 'md',
										'attributes' => [
											'@click'    => 'createTestCharge()',
											':disabled' => 'creatingCharge',
											'x-text'    => "creatingCharge ? '" . esc_html__( 'Adding...', 'affiliate-wp' ) . "' : '" . esc_html__( 'Add Funds', 'affiliate-wp' ) . "'",
										],
									]
								);
								?>
							</div>

							<!-- Charge Result Messages -->
							<div x-show="chargeResult" x-transition class="mt-3 p-3 bg-green-50 rounded-lg border border-green-200">
								<p class="text-sm text-green-700" x-text="chargeResult"></p>
							</div>
							<div x-show="chargeError" x-transition class="mt-3 p-3 bg-red-50 rounded-lg border border-red-200">
								<p class="text-sm text-red-700" x-text="chargeError"></p>
							</div>

							<p class="mt-3 text-sm text-gray-500">
								<?php esc_html_e( 'Add funds to your test balance to pay referrals in Stripe Test Mode.', 'affiliate-wp' ); ?>
							</p>
						</div>

					</div>


			</div>
		</div>
	</div>

		<!-- Webhook Configuration -->
		<div id="stripe-webhooks" class="bg-white rounded-lg border border-gray-200">
			<div class="p-6">
				<div class="mb-4">
					<div class="flex justify-between items-start">
						<div>
							<div class="flex flex-wrap gap-2 items-center mb-1">
								<h4 class="text-base font-medium text-gray-900"><?php esc_html_e( 'Webhook Configuration', 'affiliate-wp' ); ?></h4>
								<?php
								affwp_badge(
									[
										'text'    => esc_html__( 'Recommended', 'affiliate-wp' ),
										'variant' => 'success',
										'size'    => 'sm',
									]
								);
								?>
							</div>
							<p class="mt-1 text-sm text-gray-600">
								<?php esc_html_e( 'Enable real-time updates for affiliate account requirements and payout tracking', 'affiliate-wp' ); ?>
							</p>
						</div>

					</div>
				</div>


				<div class="space-y-5">
					<!-- Webhook URL with Copy Button -->
					<div>
						<label class="block mb-1 text-sm font-medium text-gray-700">
							<?php esc_html_e( 'Webhook Endpoint URL', 'affiliate-wp' ); ?>
						</label>
						<div class="flex items-center space-x-2">
							<input
								type="text"
								readonly
								value="<?php echo esc_attr( home_url( '/wp-json/affwp/v1/stripe/webhook' ) ); ?>"
								class="flex-1 px-4 py-2 font-mono text-sm bg-gray-50 rounded-lg border-1 border-gray-300"
								id="webhook-url-field" />
							<?php
							echo affwp_render_copy_button(
								[
									'content'      => home_url( '/wp-json/affwp/v1/stripe/webhook' ),
									'button_text'  => esc_html__( 'Copy', 'affiliate-wp' ),
									'success_text' => esc_html__( 'Copied!', 'affiliate-wp' ),
									'variant'      => 'secondary',
									'size'         => 'md',
								]
							);
							?>
						</div>
						<p class="mt-2 text-sm text-gray-500">
							<?php
							$webhook_url = $test_mode
								? 'https://dashboard.stripe.com/test/webhooks'
								: 'https://dashboard.stripe.com/webhooks';

							/* translators: %s: Link to Stripe account */
							printf(
								esc_html__( 'Copy this URL and add it as a Webhook endpoint in your %s.', 'affiliate-wp' ),
								affwp_render_link(
									[
										'text'     => esc_html__( 'Stripe account', 'affiliate-wp' ),
										'href'     => 'https://dashboard.stripe.com/webhooks',
										'external' => true,
										'variant'  => 'default',
									]
								),
							);
							?>
						</p>
					</div>

					<!-- Webhook Secret -->
					<div>
						<label for="stripe-webhook-secret" class="block mb-1 text-sm font-medium text-gray-700">
							<?php esc_html_e( 'Webhook Signing Secret', 'affiliate-wp' ); ?>
						</label>
						<?php
						affwp_input(
							array(
								'id'          => 'stripe-webhook-secret',
								'name'        => 'affwp_settings[stripe_webhook_secret]',
								'type'        => 'password',
								'value'       => $webhook_secret,
								'placeholder' => __( 'whsec_...', 'affiliate-wp' ),
							)
						);
						?>
							<p class="mt-2 text-sm text-gray-500">
							<?php
							esc_html_e( 'Copy the signing secret from Stripe and add it here.', 'affiliate-wp' );
							?>
						</p>
					</div>



				</div>
			</div>
		</div>

	</div> <!-- End of Main Settings Container -->

	<!-- Documentation Link -->
	<div class="mt-6 text-center">
		<?php
		affwp_link(
			[
				'text'     => esc_html__( 'View Stripe Payouts Documentation', 'affiliate-wp' ),
				'href'     => 'https://affiliatewp.com/docs/stripe-payouts/',
				'external' => true,
				'variant'  => 'primary',
				'icon'     => [
					'name'     => 'book',
					'position' => 'left',
				],
			]
		);
		?>
	</div>


</div> <!-- End of max-w-4xl container -->

<?php
// Prepare the test charge confirmation modal content.
//
// Build currency-aware pieces for the Alpine x-text expressions so the modal
// reflects the store currency instead of a hardcoded "$". affwp_currency_filter()
// returns HTML entities (e.g. &yen;), so decode to raw UTF-8 for x-text
// (textContent), and split around a sentinel to get the symbol prefix/suffix.
$affwp_currency_sample   = html_entity_decode( affwp_currency_filter( '@@AMOUNT@@' ), ENT_COMPAT, 'UTF-8' );
$affwp_currency_parts    = explode( '@@AMOUNT@@', $affwp_currency_sample, 2 );
$affwp_currency_prefix   = isset( $affwp_currency_parts[0] ) ? $affwp_currency_parts[0] : '';
$affwp_currency_suffix   = isset( $affwp_currency_parts[1] ) ? $affwp_currency_parts[1] : '';
$affwp_currency_decimals = (int) affwp_currency_decimal_filter();
ob_start();
?>
<div x-data="{
	get modalData() {
		return Alpine.store('modals').registry['stripe-test-charge-modal']?.data || {};
	}
}">
	<div class="space-y-5 flex flex-col">
		<p class="text-base text-center  text-gray-700">
			<?php esc_html_e( 'Add', 'affiliate-wp' ); ?>
			<strong class="text-gray-900 font-semibold" x-text="'<?php echo esc_js( $affwp_currency_prefix ); ?>' + (modalData.chargeAmount || 0).toFixed(<?php echo $affwp_currency_decimals; ?>) + '<?php echo esc_js( $affwp_currency_suffix ); ?>'"></strong>
			<?php esc_html_e( 'to your Stripe test balance to process test payouts.', 'affiliate-wp' ); ?>
		</p>

		<ul class="space-y-3 text-base max-w-[330px] self-center">
			<li class="flex items-center text-gray-700">
				<span class="mr-2.5 text-green-600 flex-shrink-0">
					<?php \AffiliateWP\Utils\Icons::render( 'check-circle', '', [ 'class' => 'size-6' ] ); ?>
				</span>
				<span>
					<?php esc_html_e( 'Test funds for', 'affiliate-wp' ); ?>
					<strong class="text-gray-900 font-semibold" x-text="'<?php echo esc_js( $affwp_currency_prefix ); ?>' + (modalData.chargeAmount || 0).toFixed(<?php echo $affwp_currency_decimals; ?>) + '<?php echo esc_js( $affwp_currency_suffix ); ?>' + ' <?php echo esc_js( affwp_get_currency() ); ?>'"></strong>
				</span>
			</li>
			<li class="flex items-center text-gray-700">
				<span class="mr-2.5 text-green-600 flex-shrink-0">
					<?php \AffiliateWP\Utils\Icons::render( 'check-circle', '', [ 'class' => 'size-6' ] ); ?>
				</span>
				<span><?php esc_html_e( 'Available immediately for test payouts', 'affiliate-wp' ); ?></span>
			</li>
			<li class="flex items-center text-gray-700">
				<span class="mr-2.5 text-green-600 flex-shrink-0">
					<?php \AffiliateWP\Utils\Icons::render( 'check-circle', '', [ 'class' => 'size-6' ] ); ?>
				</span>
				<span><?php esc_html_e( 'No real money involved (test mode only)', 'affiliate-wp' ); ?></span>
			</li>
		</ul>
	</div>
</div>
<?php
$test_charge_content = ob_get_clean();

// Render the test charge confirmation modal.
affwp_modal(
	[
		'id'             => 'stripe-test-charge-modal',
		'icon'           => 'beaker',
		'title'          => esc_html__( 'Add Test Funds to your Stripe Balance', 'affiliate-wp' ),
		'content'        => $test_charge_content,
		'size'           => 'sm',
		'variant'        => 'info',
		'footer_actions' => [
			[
				'text'    => esc_html__( 'Cancel', 'affiliate-wp' ),
				'variant' => 'secondary',
				'action'  => 'close',
			],
			[
				'text'       => esc_html__( 'Add Test Funds', 'affiliate-wp' ),
				'variant'    => 'primary',
				'attributes' => [
					'@click'    => 'window.stripeProcessTestCharge(); close();',
					'autofocus' => true,
				],
			],
		],
	]
);
?>
