<?php
/**
 * Integrations Page
 *
 * @package Giraforms
 */

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

/**
 * Class GiraForms_Integrations
 */
class GiraForms_Integrations {

	/**
	 * Instance of this class.
	 *
	 * @var GiraForms_Integrations
	 */
	private static $instance = null;

	/**
	 * Settings option name.
	 *
	 * @var string
	 */
	private $option_name = 'giraforms_settings';

	/**
	 * Get instance.
	 *
	 * @return GiraForms_Integrations
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor.
	 */
	private function __construct() {
		add_action( 'admin_menu', array( $this, 'add_integrations_page' ) );
		add_action( 'admin_post_giraforms_save_integrations', array( $this, 'handle_save' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
	}

	/**
	 * Add integrations page to admin menu.
	 */
	public function add_integrations_page() {
		add_submenu_page(
			'giraforms',
			__( 'Integrations', 'giraforms' ),
			__( 'Integrations', 'giraforms' ),
			'manage_options',
			'giraforms-integrations',
			array( $this, 'render_integrations_page' )
		);
	}

	/**
	 * Handle saving integration keys.
	 */
	public function handle_save() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have the necessary permissions to access this page.', 'giraforms' ) );
		}

		check_admin_referer( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' );

		$existing = get_option( $this->option_name, array() );

		// Only update keys that are present in the submitted form.
		if ( array_key_exists( 'turnstile_site_key', $_POST ) ) {
			$existing['turnstile_site_key'] = sanitize_text_field( wp_unslash( $_POST['turnstile_site_key'] ) );
		}
		if ( array_key_exists( 'turnstile_secret_key', $_POST ) ) {
			$existing['turnstile_secret_key'] = sanitize_text_field( wp_unslash( $_POST['turnstile_secret_key'] ) );
		}
		// reCAPTCHA v2 keys.
		if ( array_key_exists( 'recaptcha_v2_site_key', $_POST ) ) {
			$existing['recaptcha_v2_site_key'] = sanitize_text_field( wp_unslash( $_POST['recaptcha_v2_site_key'] ) );
		}
		if ( array_key_exists( 'recaptcha_v2_secret_key', $_POST ) ) {
			$existing['recaptcha_v2_secret_key'] = sanitize_text_field( wp_unslash( $_POST['recaptcha_v2_secret_key'] ) );
		}
		// reCAPTCHA v3 keys.
		if ( array_key_exists( 'recaptcha_v3_site_key', $_POST ) ) {
			$existing['recaptcha_v3_site_key'] = sanitize_text_field( wp_unslash( $_POST['recaptcha_v3_site_key'] ) );
		}
		if ( array_key_exists( 'recaptcha_v3_secret_key', $_POST ) ) {
			$existing['recaptcha_v3_secret_key'] = sanitize_text_field( wp_unslash( $_POST['recaptcha_v3_secret_key'] ) );
		}
		// hCaptcha keys.
		if ( array_key_exists( 'hcaptcha_site_key', $_POST ) ) {
			$existing['hcaptcha_site_key'] = sanitize_text_field( wp_unslash( $_POST['hcaptcha_site_key'] ) );
		}
		if ( array_key_exists( 'hcaptcha_secret_key', $_POST ) ) {
			$existing['hcaptcha_secret_key'] = sanitize_text_field( wp_unslash( $_POST['hcaptcha_secret_key'] ) );
		}
		if ( array_key_exists( 'brevo_api_key', $_POST ) ) {
			$existing['brevo_api_key'] = sanitize_text_field( wp_unslash( $_POST['brevo_api_key'] ) );
			delete_transient( 'giraforms_brevo_lists' );
			delete_transient( 'giraforms_brevo_detected_ip' );
		}
		if ( array_key_exists( 'stripe_publishable_key', $_POST ) ) {
			$existing['stripe_publishable_key'] = sanitize_text_field( wp_unslash( $_POST['stripe_publishable_key'] ) );
		}
		if ( array_key_exists( 'stripe_secret_key', $_POST ) ) {
			$existing['stripe_secret_key'] = sanitize_text_field( wp_unslash( $_POST['stripe_secret_key'] ) );
		}
		// Mode-aware Stripe keys.
		if ( array_key_exists( 'stripe_mode', $_POST ) ) {
			$mode = sanitize_text_field( wp_unslash( $_POST['stripe_mode'] ) );
			$existing['stripe_mode'] = in_array( $mode, array( 'test', 'live' ), true ) ? $mode : 'test';
		}
		$stripe_mode_keys = array(
			'stripe_test_publishable_key',
			'stripe_test_secret_key',
			'stripe_live_publishable_key',
			'stripe_live_secret_key',
			'stripe_test_webhook_secret',
			'stripe_live_webhook_secret',
		);
		foreach ( $stripe_mode_keys as $stripe_key ) {
			if ( array_key_exists( $stripe_key, $_POST ) ) {
				$existing[ $stripe_key ] = sanitize_text_field( wp_unslash( $_POST[ $stripe_key ] ) );
			}
		}

		update_option( $this->option_name, $existing );

		wp_safe_redirect(
			add_query_arg(
				array(
					'page'    => 'giraforms-integrations',
					'updated' => 'true',
				),
				admin_url( 'admin.php' )
			),
			302,
			'Giraforms'
		);
		exit;
	}

	/**
	 * Enqueue styles for integrations page.
	 */
	public function enqueue_styles() {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking page parameter for style enqueue.
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

		if ( 'giraforms-integrations' !== $page ) {
			return;
		}

		wp_add_inline_style( 'wp-admin', $this->get_inline_styles() );

		wp_enqueue_script( 'jquery' );
		wp_add_inline_script( 'jquery', "
			jQuery(document).ready(function($) {
				$('.secret-key-toggle').on('click', function() {
					var btn = $(this);
					var input = btn.siblings('input');
					var icon = btn.find('.dashicons');
					if (input.attr('type') === 'password') {
						input.attr('type', 'text');
						icon.removeClass('dashicons-visibility').addClass('dashicons-hidden');
					} else {
						input.attr('type', 'password');
						icon.removeClass('dashicons-hidden').addClass('dashicons-visibility');
					}
				});
			});
		" );
	}

	/**
	 * Get inline styles for integrations page.
	 *
	 * @return string
	 */
	private function get_inline_styles() {
		return '
			.giraforms-integrations-section {
				margin-top: 28px;
			}
			.giraforms-integrations-section:first-of-type {
				margin-top: 20px;
			}
			.giraforms-integrations-section-header {
				display: flex;
				align-items: center;
				gap: 8px;
				margin-bottom: 6px;
			}
			.giraforms-integrations-section-title {
				margin: 0;
				font-size: 18px;
				font-weight: 600;
			}
			.giraforms-integrations-section-description {
				margin: 0 0 14px;
				color: #50575e;
			}
			.giraforms-integrations-grid {
				display: grid;
				grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
				gap: 20px;
				margin-top: 0;
			}
			.giraforms-integration-card {
				background: #fff;
				border: 1px solid #c3c4c7;
				border-radius: 8px;
				padding: 24px;
				position: relative;
				transition: border-color 0.2s ease, box-shadow 0.2s ease;
			}
			.giraforms-integration-card:hover {
				border-color: #a7aaad;
				box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
			}
			.giraforms-integration-card .card-header {
				display: flex;
				align-items: center;
				gap: 12px;
				margin-bottom: 12px;
			}
			.giraforms-integration-card .card-icon {
				width: 48px;
				height: 48px;
				border-radius: 10px;
				display: flex;
				align-items: center;
				justify-content: center;
				flex-shrink: 0;
				font-size: 17px;
				font-weight: 700;
				line-height: 1;
			}
			.giraforms-integration-card .card-icon.turnstile {
				background: #fff3e8;
				color: #b45309;
			}
			.giraforms-integration-card .card-icon.webhooks {
				background: #edf2ff;
				color: #1d4ed8;
			}
			.giraforms-integration-card .card-icon.brevo {
				background: #e9eeff;
				color: #1e3a8a;
			}
			.giraforms-integration-card .card-icon.recaptcha {
				background: #edf3ff;
				color: #1d4ed8;
			}
			.giraforms-integration-card .card-icon.hcaptcha {
				background: #e9f2ff;
				color: #1e40af;
			}
			.giraforms-integration-card .card-icon.stripe {
				background: #f0f5ff;
				color: #635bff;
			}
			.giraforms-integration-card .card-title {
				margin: 0;
				font-size: 16px;
				font-weight: 600;
			}
			.giraforms-integration-card .card-title .badge-pro {
				display: inline-block;
				background: linear-gradient(135deg, #3858e9, #6366f1);
				color: #fff;
				font-size: 10px;
				font-weight: 700;
				padding: 2px 8px;
				border-radius: 10px;
				margin-left: 8px;
				vertical-align: middle;
				text-transform: uppercase;
				letter-spacing: 0.5px;
			}
			.giraforms-integration-card .card-description {
				color: #50575e;
				margin: 0 0 16px 0;
				font-size: 13px;
				line-height: 1.5;
			}
			.giraforms-integration-card .card-status {
				display: inline-flex;
				align-items: center;
				gap: 6px;
				font-size: 12px;
				font-weight: 500;
				padding: 4px 10px;
				border-radius: 12px;
				margin-bottom: 16px;
			}
			.giraforms-integration-card .card-status.configured {
				background: rgba(0, 163, 42, 0.08);
				color: #00a32a;
			}
			.giraforms-integration-card .card-status.not-configured {
				background: rgba(219, 166, 23, 0.08);
				color: #dba617;
			}
			.giraforms-integration-card .card-fields {
				margin-top: 16px;
				padding-top: 16px;
				border-top: 1px solid #e2e4e7;
			}
			.giraforms-integration-card .card-fields label {
				display: block;
				font-weight: 500;
				margin-bottom: 4px;
				font-size: 13px;
			}
			.giraforms-integration-card .card-fields input[type="text"],
			.giraforms-integration-card .card-fields input[type="password"] {
				width: 100%;
				margin-bottom: 12px;
			}
			.giraforms-integration-card .card-fields .description {
				font-size: 12px;
				color: #757575;
				margin: -8px 0 12px 0;
			}
			.giraforms-integration-card .card-fields .card-fields-subsection {
				margin-top: 16px;
				padding-top: 14px;
				border-top: 1px dashed #e2e4e7;
			}
			.giraforms-integration-card .card-fields .card-fields-subsection:first-child {
				margin-top: 0;
				padding-top: 0;
				border-top: none;
			}
			.giraforms-integration-card .card-fields .card-fields-subsection-title {
				margin: 0 0 10px 0;
				font-size: 13px;
				font-weight: 600;
				color: #1e1e1e;
			}
			.giraforms-integration-card .card-action {
				margin-top: 16px;
				padding-top: 16px;
				border-top: 1px solid #e2e4e7;
			}
			.giraforms-integration-card .card-fields .secret-key-wrapper {
				position: relative;
				display: flex;
				align-items: stretch;
				margin-bottom: 12px;
			}
			.giraforms-integration-card .card-fields .secret-key-wrapper input {
				margin-bottom: 0;
				border-top-right-radius: 0;
				border-bottom-right-radius: 0;
				flex: 1;
			}
			.giraforms-integration-card .card-fields .secret-key-toggle {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 36px;
				border: 1px solid #8c8f94;
				border-left: none;
				border-radius: 0 4px 4px 0;
				background: #f0f0f1;
				cursor: pointer;
				color: #50575e;
				transition: color 0.15s ease;
				padding: 0;
			}
			.giraforms-integration-card .card-fields .secret-key-toggle:hover {
				color: #1d2327;
				background: #e8e8e9;
			}
		';
	}

	/**
	 * Get letter marker for a service.
	 *
	 * @param string $service Service slug.
	 * @return string
	 */
	private function get_service_icon_letter( $service ) {
		$letters = array(
			'turnstile' => 'T',
			'recaptcha' => 'R',
			'hcaptcha'  => 'H',
			'webhooks'  => 'W',
			'brevo'     => 'B',
			'stripe'    => 'S',
		);

		return isset( $letters[ $service ] ) ? $letters[ $service ] : '?';
	}

	/**
	 * Render integrations page.
	 */
	public function render_integrations_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( esc_html__( 'You do not have the necessary permissions to access this page.', 'giraforms' ) );
		}

		$settings  = get_option( $this->option_name, array() );
		$site_key  = isset( $settings['turnstile_site_key'] ) ? $settings['turnstile_site_key'] : '';
		$secret_key = isset( $settings['turnstile_secret_key'] ) ? $settings['turnstile_secret_key'] : '';
		$turnstile_configured = ! empty( $site_key ) && ! empty( $secret_key );

		// Check if webhooks are configured
		$webhooks = get_option( 'giraforms_webhooks', array() );
		$webhooks_count = is_array( $webhooks ) ? count( $webhooks ) : 0;
		$has_premium = giraforms__fs()->can_use_premium_code__premium_only();

		?>
		<div class="wrap">
			<h1><?php esc_html_e( 'Integrations', 'giraforms' ); ?></h1>
			<p><?php esc_html_e( 'Connect Giraforms with external services to extend your forms capabilities.', 'giraforms' ); ?></p>

			<?php
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only checking 'updated' flag for success message display.
			if ( isset( $_GET['updated'] ) && 'true' === $_GET['updated'] ) :
				?>
				<div class="notice notice-success is-dismissible">
					<p><?php esc_html_e( 'Settings saved.', 'giraforms' ); ?></p>
				</div>
			<?php endif; ?>

			<div class="giraforms-integrations-section">
				<div class="giraforms-integrations-section-header">
					<span class="dashicons dashicons-shield"></span>
					<h2 class="giraforms-integrations-section-title"><?php esc_html_e( 'Captcha Services', 'giraforms' ); ?></h2>
				</div>
				<p class="giraforms-integrations-section-description"><?php esc_html_e( 'Configure anti-spam providers used by your forms.', 'giraforms' ); ?></p>

				<div class="giraforms-integrations-grid">
				<!-- Turnstile Card -->
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon turnstile"><?php echo esc_html( $this->get_service_icon_letter( 'turnstile' ) ); ?></div>
						<div>
							<h3 class="card-title"><?php esc_html_e( 'Cloudflare Turnstile', 'giraforms' ); ?></h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Protect your forms from spam with Cloudflare Turnstile. Privacy-friendly CAPTCHA alternative that verifies visitors without showing puzzles.', 'giraforms' ); ?>
					</p>

					<?php if ( $turnstile_configured ) : ?>
						<span class="card-status configured">● <?php esc_html_e( 'Configured', 'giraforms' ); ?></span>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Not configured', 'giraforms' ); ?></span>
					<?php endif; ?>

					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<input type="hidden" name="action" value="giraforms_save_integrations" />
						<?php wp_nonce_field( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' ); ?>

						<div class="card-fields">
							<label for="turnstile_site_key"><?php esc_html_e( 'Site Key', 'giraforms' ); ?></label>
							<input
								type="text"
								id="turnstile_site_key"
								name="turnstile_site_key"
								value="<?php echo esc_attr( $site_key ); ?>"
								class="regular-text"
								placeholder="0x..."
							/>

							<label for="turnstile_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
							<div class="secret-key-wrapper">
								<input
									type="password"
									id="turnstile_secret_key"
									name="turnstile_secret_key"
									value="<?php echo esc_attr( $secret_key ); ?>"
									class="regular-text"
								/>
								<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
									<span class="dashicons dashicons-visibility"></span>
								</button>
							</div>

							<p class="description">
								<?php
								echo wp_kses_post(
									sprintf(
										/* translators: %s: Cloudflare dashboard URL */
										__( 'Get your keys from the <a href="%s" target="_blank">Cloudflare Turnstile Dashboard</a>.', 'giraforms' ),
										'https://dash.cloudflare.com/?to=/:account/turnstile'
									)
								);
								?>
							</p>
						</div>

						<?php submit_button( __( 'Save Keys', 'giraforms' ), 'primary', 'submit', true ); ?>
					</form>
				</div>

				<!-- Google reCAPTCHA Card -->
				<?php
				$recaptcha_v2_site_key   = isset( $settings['recaptcha_v2_site_key'] ) ? $settings['recaptcha_v2_site_key'] : '';
				$recaptcha_v2_secret_key = isset( $settings['recaptcha_v2_secret_key'] ) ? $settings['recaptcha_v2_secret_key'] : '';
				$recaptcha_v3_site_key   = isset( $settings['recaptcha_v3_site_key'] ) ? $settings['recaptcha_v3_site_key'] : '';
				$recaptcha_v3_secret_key = isset( $settings['recaptcha_v3_secret_key'] ) ? $settings['recaptcha_v3_secret_key'] : '';
				$recaptcha_configured    = ( ! empty( $recaptcha_v2_site_key ) && ! empty( $recaptcha_v2_secret_key ) )
					|| ( ! empty( $recaptcha_v3_site_key ) && ! empty( $recaptcha_v3_secret_key ) );
				?>
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon recaptcha"><?php echo esc_html( $this->get_service_icon_letter( 'recaptcha' ) ); ?></div>
						<div>
							<h3 class="card-title"><?php esc_html_e( 'Google reCAPTCHA', 'giraforms' ); ?></h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Protect your forms with Google reCAPTCHA. Supports v2 (checkbox) and v3 (invisible score-based). Each version requires its own pair of keys.', 'giraforms' ); ?>
					</p>

					<?php if ( $recaptcha_configured ) : ?>
						<span class="card-status configured">● <?php esc_html_e( 'Configured', 'giraforms' ); ?></span>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Not configured', 'giraforms' ); ?></span>
					<?php endif; ?>

					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<input type="hidden" name="action" value="giraforms_save_integrations" />
						<?php wp_nonce_field( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' ); ?>

						<div class="card-fields">
							<div class="card-fields-subsection">
								<h4 class="card-fields-subsection-title"><?php esc_html_e( 'reCAPTCHA v2 (Checkbox)', 'giraforms' ); ?></h4>

								<label for="recaptcha_v2_site_key"><?php esc_html_e( 'Site Key', 'giraforms' ); ?></label>
								<input
									type="text"
									id="recaptcha_v2_site_key"
									name="recaptcha_v2_site_key"
									value="<?php echo esc_attr( $recaptcha_v2_site_key ); ?>"
									class="regular-text"
								/>

								<label for="recaptcha_v2_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
								<div class="secret-key-wrapper">
									<input
										type="password"
										id="recaptcha_v2_secret_key"
										name="recaptcha_v2_secret_key"
										value="<?php echo esc_attr( $recaptcha_v2_secret_key ); ?>"
										class="regular-text"
									/>
									<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
										<span class="dashicons dashicons-visibility"></span>
									</button>
								</div>
							</div>

							<div class="card-fields-subsection">
								<h4 class="card-fields-subsection-title"><?php esc_html_e( 'reCAPTCHA v3 (Invisible)', 'giraforms' ); ?></h4>

								<label for="recaptcha_v3_site_key"><?php esc_html_e( 'Site Key', 'giraforms' ); ?></label>
								<input
									type="text"
									id="recaptcha_v3_site_key"
									name="recaptcha_v3_site_key"
									value="<?php echo esc_attr( $recaptcha_v3_site_key ); ?>"
									class="regular-text"
								/>

								<label for="recaptcha_v3_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
								<div class="secret-key-wrapper">
									<input
										type="password"
										id="recaptcha_v3_secret_key"
										name="recaptcha_v3_secret_key"
										value="<?php echo esc_attr( $recaptcha_v3_secret_key ); ?>"
										class="regular-text"
									/>
									<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
										<span class="dashicons dashicons-visibility"></span>
									</button>
								</div>
							</div>

							<p class="description">
								<?php
								echo wp_kses_post(
									sprintf(
										/* translators: %s: Google reCAPTCHA console URL */
										__( 'Get your keys from the <a href="%s" target="_blank">Google reCAPTCHA Console</a>. v2 and v3 require separate key pairs.', 'giraforms' ),
										'https://www.google.com/recaptcha/admin'
									)
								);
								?>
							</p>
						</div>

						<?php submit_button( __( 'Save Keys', 'giraforms' ), 'primary', 'submit', true ); ?>
					</form>
				</div>

				<!-- hCaptcha Card -->
				<?php
				$hcaptcha_site_key   = isset( $settings['hcaptcha_site_key'] ) ? $settings['hcaptcha_site_key'] : '';
				$hcaptcha_secret_key = isset( $settings['hcaptcha_secret_key'] ) ? $settings['hcaptcha_secret_key'] : '';
				$hcaptcha_configured = ! empty( $hcaptcha_site_key ) && ! empty( $hcaptcha_secret_key );
				?>
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon hcaptcha"><?php echo esc_html( $this->get_service_icon_letter( 'hcaptcha' ) ); ?></div>
						<div>
							<h3 class="card-title"><?php esc_html_e( 'hCaptcha', 'giraforms' ); ?></h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Privacy-focused bot protection with hCaptcha. Rewards website owners for serving its demand while protecting forms from abuse.', 'giraforms' ); ?>
					</p>

					<?php if ( $hcaptcha_configured ) : ?>
						<span class="card-status configured">● <?php esc_html_e( 'Configured', 'giraforms' ); ?></span>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Not configured', 'giraforms' ); ?></span>
					<?php endif; ?>

					<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
						<input type="hidden" name="action" value="giraforms_save_integrations" />
						<?php wp_nonce_field( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' ); ?>

						<div class="card-fields">
							<label for="hcaptcha_site_key"><?php esc_html_e( 'Site Key', 'giraforms' ); ?></label>
							<input
								type="text"
								id="hcaptcha_site_key"
								name="hcaptcha_site_key"
								value="<?php echo esc_attr( $hcaptcha_site_key ); ?>"
								class="regular-text"
							/>

							<label for="hcaptcha_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
							<div class="secret-key-wrapper">
								<input
									type="password"
									id="hcaptcha_secret_key"
									name="hcaptcha_secret_key"
									value="<?php echo esc_attr( $hcaptcha_secret_key ); ?>"
									class="regular-text"
								/>
								<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
									<span class="dashicons dashicons-visibility"></span>
								</button>
							</div>

							<p class="description">
								<?php
								echo wp_kses_post(
									sprintf(
										/* translators: %s: hCaptcha dashboard URL */
										__( 'Get your keys from the <a href="%s" target="_blank">hCaptcha Dashboard</a>.', 'giraforms' ),
										'https://dashboard.hcaptcha.com/'
									)
								);
								?>
							</p>
						</div>

						<?php submit_button( __( 'Save Keys', 'giraforms' ), 'primary', 'submit', true ); ?>
					</form>
				</div>
				</div>
			</div>

			<div class="giraforms-integrations-section">
				<div class="giraforms-integrations-section-header">
					<span class="dashicons dashicons-megaphone"></span>
					<h2 class="giraforms-integrations-section-title"><?php esc_html_e( 'Marketing Services', 'giraforms' ); ?></h2>
				</div>
				<p class="giraforms-integrations-section-description"><?php esc_html_e( 'Connect your forms with email marketing platforms.', 'giraforms' ); ?></p>

				<div class="giraforms-integrations-grid">
				<!-- Brevo Card -->
				<?php
				$brevo_api_key    = isset( $settings['brevo_api_key'] ) ? $settings['brevo_api_key'] : '';
				$brevo_configured = ! empty( $brevo_api_key );
				$brevo_detected_ip = get_transient( 'giraforms_brevo_detected_ip' );
				?>
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon brevo"><?php echo esc_html( $this->get_service_icon_letter( 'brevo' ) ); ?></div>
						<div>
							<h3 class="card-title">
								<?php esc_html_e( 'Brevo', 'giraforms' ); ?>
								<?php if ( ! $has_premium ) : ?>
									<span class="badge-pro">Pro</span>
								<?php endif; ?>
							</h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Automatically add form submitters to a Brevo contact list and sync their name attributes. Configure per form in the block settings.', 'giraforms' ); ?>
					</p>

					<?php if ( $has_premium ) : ?>
						<?php if ( $brevo_configured ) : ?>
							<span class="card-status configured">● <?php esc_html_e( 'Configured', 'giraforms' ); ?></span>
						<?php else : ?>
							<span class="card-status not-configured">● <?php esc_html_e( 'Not configured', 'giraforms' ); ?></span>
						<?php endif; ?>

						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
							<input type="hidden" name="action" value="giraforms_save_integrations" />
							<?php wp_nonce_field( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' ); ?>

							<div class="card-fields">
								<label for="brevo_api_key"><?php esc_html_e( 'API Key', 'giraforms' ); ?></label>
								<div class="secret-key-wrapper">
									<input
										type="password"
										id="brevo_api_key"
										name="brevo_api_key"
										value="<?php echo esc_attr( $brevo_api_key ); ?>"
										class="regular-text"
										placeholder="xkeysib-..."
									/>
									<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
										<span class="dashicons dashicons-visibility"></span>
									</button>
								</div>
								<p class="description">
									<?php
									echo wp_kses_post(
										sprintf(
											/* translators: %s: Brevo API keys URL */
											__( 'Find your API key in the <a href="%s" target="_blank">Brevo API Keys settings</a> (Account → SMTP & API → API Keys).', 'giraforms' ),
											'https://app.brevo.com/settings/keys/api'
										)
									);
									?>
								</p>
								<?php if ( ! empty( $brevo_detected_ip ) ) : ?>
									<p class="description" style="color:#d63638;">
										<?php
										echo wp_kses_post(
											sprintf(
												/* translators: 1: IP address, 2: Brevo authorized IPs URL */
												__( 'Detected outgoing IP: <code>%1$s</code>. Add it in <a href="%2$s" target="_blank">Brevo Authorized IPs</a>.', 'giraforms' ),
												esc_html( $brevo_detected_ip ),
												'https://app.brevo.com/security/authorised_ips'
											)
										);
										?>
									</p>
								<?php endif; ?>
							</div>

							<?php submit_button( __( 'Save Key', 'giraforms' ), 'primary', 'submit', true ); ?>
						</form>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Requires Pro license', 'giraforms' ); ?></span>

						<div class="card-action">
							<a href="<?php echo esc_url( giraforms__fs()->get_upgrade_url() ); ?>" class="button button-primary">
								<?php esc_html_e( 'Upgrade to Pro', 'giraforms' ); ?>
							</a>
						</div>
					<?php endif; ?>
				</div>
				</div>
			</div>

			<div class="giraforms-integrations-section">
				<div class="giraforms-integrations-section-header">
					<span class="dashicons dashicons-money-alt"></span>
					<h2 class="giraforms-integrations-section-title"><?php esc_html_e( 'Payment', 'giraforms' ); ?></h2>
				</div>
				<p class="giraforms-integrations-section-description"><?php esc_html_e( 'Collect payments directly through your forms.', 'giraforms' ); ?></p>

				<div class="giraforms-integrations-grid">
				<!-- Stripe Card -->
				<?php
				$stripe_mode               = isset( $settings['stripe_mode'] ) ? $settings['stripe_mode'] : 'test';
				$stripe_test_pub_key       = isset( $settings['stripe_test_publishable_key'] ) ? $settings['stripe_test_publishable_key'] : '';
				$stripe_test_sec_key       = isset( $settings['stripe_test_secret_key'] ) ? $settings['stripe_test_secret_key'] : '';
				$stripe_live_pub_key       = isset( $settings['stripe_live_publishable_key'] ) ? $settings['stripe_live_publishable_key'] : '';
				$stripe_live_sec_key       = isset( $settings['stripe_live_secret_key'] ) ? $settings['stripe_live_secret_key'] : '';
				$stripe_test_webhook_sec   = isset( $settings['stripe_test_webhook_secret'] ) ? $settings['stripe_test_webhook_secret'] : '';
				$stripe_live_webhook_sec   = isset( $settings['stripe_live_webhook_secret'] ) ? $settings['stripe_live_webhook_secret'] : '';
				$stripe_legacy_pub         = isset( $settings['stripe_publishable_key'] ) ? $settings['stripe_publishable_key'] : '';
				$stripe_legacy_sec         = isset( $settings['stripe_secret_key'] ) ? $settings['stripe_secret_key'] : '';

				$stripe_configured = ( ! empty( $stripe_test_pub_key ) && ! empty( $stripe_test_sec_key ) )
					|| ( ! empty( $stripe_live_pub_key ) && ! empty( $stripe_live_sec_key ) )
					|| ( ! empty( $stripe_legacy_pub ) && ! empty( $stripe_legacy_sec ) );

				$stripe_key_warnings = array();
				if ( ! empty( $stripe_test_pub_key ) && strpos( $stripe_test_pub_key, 'pk_test_' ) !== 0 ) {
					$stripe_key_warnings[] = __( 'Test publishable key does not start with pk_test_', 'giraforms' );
				}
				if ( ! empty( $stripe_test_sec_key ) && strpos( $stripe_test_sec_key, 'sk_test_' ) !== 0 && strpos( $stripe_test_sec_key, 'rk_test_' ) !== 0 ) {
					$stripe_key_warnings[] = __( 'Test secret key does not start with sk_test_', 'giraforms' );
				}
				if ( ! empty( $stripe_live_pub_key ) && strpos( $stripe_live_pub_key, 'pk_live_' ) !== 0 ) {
					$stripe_key_warnings[] = __( 'Live publishable key does not start with pk_live_', 'giraforms' );
				}
				if ( ! empty( $stripe_live_sec_key ) && strpos( $stripe_live_sec_key, 'sk_live_' ) !== 0 && strpos( $stripe_live_sec_key, 'rk_live_' ) !== 0 ) {
					$stripe_key_warnings[] = __( 'Live secret key does not start with sk_live_', 'giraforms' );
				}

				$webhook_url = rest_url( 'giraforms/v1/stripe/webhook' );
				?>
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon stripe"><?php echo esc_html( $this->get_service_icon_letter( 'stripe' ) ); ?></div>
						<div>
							<h3 class="card-title">
								<?php esc_html_e( 'Stripe', 'giraforms' ); ?>
								<?php if ( ! $has_premium ) : ?>
									<span class="badge-pro">Pro</span>
								<?php endif; ?>
							</h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Accept credit card payments directly in your forms using Stripe. Supports fixed amounts, booking prices, and dynamic field values.', 'giraforms' ); ?>
					</p>

					<?php if ( $has_premium ) : ?>
						<?php if ( $stripe_configured ) : ?>
							<span class="card-status configured">● <?php esc_html_e( 'Configured', 'giraforms' ); ?>
								(<?php echo 'live' === $stripe_mode ? esc_html__( 'Live', 'giraforms' ) : esc_html__( 'Test', 'giraforms' ); ?>)
							</span>
						<?php else : ?>
							<span class="card-status not-configured">● <?php esc_html_e( 'Not configured', 'giraforms' ); ?></span>
						<?php endif; ?>

						<?php foreach ( $stripe_key_warnings as $warning ) : ?>
							<div style="background: #fef3cd; color: #856404; padding: 6px 10px; border-radius: 4px; font-size: 12px; margin-bottom: 8px;">
								<?php echo esc_html( $warning ); ?>
							</div>
						<?php endforeach; ?>

						<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
							<input type="hidden" name="action" value="giraforms_save_integrations" />
							<?php wp_nonce_field( 'giraforms_integrations_nonce', '_giraforms_integrations_nonce' ); ?>

							<div class="card-fields">
								<!-- Mode Toggle -->
								<label style="font-weight: 600; margin-bottom: 8px; display: block;"><?php esc_html_e( 'Mode', 'giraforms' ); ?></label>
								<div style="display: flex; gap: 16px; margin-bottom: 16px;">
									<label style="display: flex; align-items: center; gap: 4px; font-weight: normal; cursor: pointer;">
										<input type="radio" name="stripe_mode" value="test" <?php checked( $stripe_mode, 'test' ); ?> />
										<?php esc_html_e( 'Test', 'giraforms' ); ?>
									</label>
									<label style="display: flex; align-items: center; gap: 4px; font-weight: normal; cursor: pointer;">
										<input type="radio" name="stripe_mode" value="live" <?php checked( $stripe_mode, 'live' ); ?> />
										<?php esc_html_e( 'Live', 'giraforms' ); ?>
									</label>
								</div>

								<!-- Test Keys -->
								<div class="card-fields-subsection">
									<h4 class="card-fields-subsection-title"><?php esc_html_e( 'Test Keys', 'giraforms' ); ?></h4>

									<label for="stripe_test_publishable_key"><?php esc_html_e( 'Publishable Key', 'giraforms' ); ?></label>
									<input type="text" id="stripe_test_publishable_key" name="stripe_test_publishable_key" value="<?php echo esc_attr( $stripe_test_pub_key ); ?>" class="regular-text" placeholder="pk_test_..." />

									<label for="stripe_test_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
									<div class="secret-key-wrapper">
										<input type="password" id="stripe_test_secret_key" name="stripe_test_secret_key" value="<?php echo esc_attr( $stripe_test_sec_key ); ?>" class="regular-text" placeholder="sk_test_..." />
										<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
											<span class="dashicons dashicons-visibility"></span>
										</button>
									</div>

									<label for="stripe_test_webhook_secret"><?php esc_html_e( 'Webhook Secret', 'giraforms' ); ?></label>
									<div class="secret-key-wrapper">
										<input type="password" id="stripe_test_webhook_secret" name="stripe_test_webhook_secret" value="<?php echo esc_attr( $stripe_test_webhook_sec ); ?>" class="regular-text" placeholder="whsec_..." />
										<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
											<span class="dashicons dashicons-visibility"></span>
										</button>
									</div>
								</div>

								<!-- Live Keys -->
								<div class="card-fields-subsection">
									<h4 class="card-fields-subsection-title"><?php esc_html_e( 'Live Keys', 'giraforms' ); ?></h4>

									<label for="stripe_live_publishable_key"><?php esc_html_e( 'Publishable Key', 'giraforms' ); ?></label>
									<input type="text" id="stripe_live_publishable_key" name="stripe_live_publishable_key" value="<?php echo esc_attr( $stripe_live_pub_key ); ?>" class="regular-text" placeholder="pk_live_..." />

									<label for="stripe_live_secret_key"><?php esc_html_e( 'Secret Key', 'giraforms' ); ?></label>
									<div class="secret-key-wrapper">
										<input type="password" id="stripe_live_secret_key" name="stripe_live_secret_key" value="<?php echo esc_attr( $stripe_live_sec_key ); ?>" class="regular-text" placeholder="sk_live_..." />
										<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
											<span class="dashicons dashicons-visibility"></span>
										</button>
									</div>

									<label for="stripe_live_webhook_secret"><?php esc_html_e( 'Webhook Secret', 'giraforms' ); ?></label>
									<div class="secret-key-wrapper">
										<input type="password" id="stripe_live_webhook_secret" name="stripe_live_webhook_secret" value="<?php echo esc_attr( $stripe_live_webhook_sec ); ?>" class="regular-text" placeholder="whsec_..." />
										<button type="button" class="secret-key-toggle" aria-label="<?php esc_attr_e( 'Toggle key visibility', 'giraforms' ); ?>">
											<span class="dashicons dashicons-visibility"></span>
										</button>
									</div>
								</div>

								<!-- Webhook URL -->
								<div class="card-fields-subsection">
									<label><?php esc_html_e( 'Webhook URL', 'giraforms' ); ?></label>
									<input type="text" value="<?php echo esc_attr( $webhook_url ); ?>" class="regular-text" readonly onclick="this.select();" style="background: #f6f7f7; cursor: text;" />
									<p class="description">
										<?php esc_html_e( 'Add this URL in your Stripe Dashboard under Developers > Webhooks.', 'giraforms' ); ?>
									</p>
								</div>

								<p class="description">
									<?php
									echo wp_kses_post(
										sprintf(
											/* translators: %s: Stripe dashboard URL */
											__( 'Get your keys from the <a href="%s" target="_blank">Stripe Dashboard</a> (Developers > API keys).', 'giraforms' ),
											'https://dashboard.stripe.com/apikeys'
										)
									);
									?>
								</p>
							</div>

							<?php submit_button( __( 'Save Keys', 'giraforms' ), 'primary', 'submit', true ); ?>
						</form>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Requires Pro license', 'giraforms' ); ?></span>

						<div class="card-action">
							<a href="<?php echo esc_url( giraforms__fs()->get_upgrade_url() ); ?>" class="button button-primary">
								<?php esc_html_e( 'Upgrade to Pro', 'giraforms' ); ?>
							</a>
						</div>
					<?php endif; ?>
				</div>
				</div>
			</div>

			<div class="giraforms-integrations-section">
				<div class="giraforms-integrations-section-header">
					<span class="dashicons dashicons-randomize"></span>
					<h2 class="giraforms-integrations-section-title"><?php esc_html_e( 'Webhooks', 'giraforms' ); ?></h2>
				</div>
				<p class="giraforms-integrations-section-description"><?php esc_html_e( 'Send submissions to external automation tools and APIs.', 'giraforms' ); ?></p>

				<div class="giraforms-integrations-grid">
				<!-- Webhooks Card -->
				<div class="giraforms-integration-card">
					<div class="card-header">
						<div class="card-icon webhooks"><?php echo esc_html( $this->get_service_icon_letter( 'webhooks' ) ); ?></div>
						<div>
							<h3 class="card-title">
								<?php esc_html_e( 'Webhooks', 'giraforms' ); ?>
								<?php if ( ! $has_premium ) : ?>
									<span class="badge-pro">Pro</span>
								<?php endif; ?>
							</h3>
						</div>
					</div>
					<p class="card-description">
						<?php esc_html_e( 'Send form submissions to external services via HTTP POST requests. Connect with Zapier, Make, n8n, or any URL.', 'giraforms' ); ?>
					</p>

					<?php if ( $has_premium ) : ?>
						<?php if ( $webhooks_count > 0 ) : ?>
							<span class="card-status configured">●
								<?php
								echo esc_html(
									sprintf(
										/* translators: %d: number of active webhooks */
										_n( '%d webhook configured', '%d webhooks configured', $webhooks_count, 'giraforms' ),
										$webhooks_count
									)
								);
								?>
							</span>
						<?php else : ?>
							<span class="card-status not-configured">● <?php esc_html_e( 'No webhooks configured', 'giraforms' ); ?></span>
						<?php endif; ?>

						<div class="card-action">
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=giraforms-webhooks' ) ); ?>" class="button button-secondary">
								<?php esc_html_e( 'Manage Webhooks', 'giraforms' ); ?>
							</a>
						</div>
					<?php else : ?>
						<span class="card-status not-configured">● <?php esc_html_e( 'Requires Pro license', 'giraforms' ); ?></span>

						<div class="card-action">
							<a href="<?php echo esc_url( giraforms__fs()->get_upgrade_url() ); ?>" class="button button-primary">
								<?php esc_html_e( 'Upgrade to Pro', 'giraforms' ); ?>
							</a>
						</div>
					<?php endif; ?>
				</div>
				</div>
			</div>
		</div>
		<?php
	}
}
