<?php
/**
 * Settings Page Template.
 *
 * Tabs: General, Events, Pro (stub), Debug Console, System Health.
 * All option keys use capiflow_ prefix.
 *
 * @package CapiFlow
 */

defined( 'ABSPATH' ) || exit;

$capiflow_active_tab = isset( $capiflow_active_tab ) ? $capiflow_active_tab : 'general';
$capiflow_is_pro     = \CapiFlow\Core\License_Manager::is_pro_active();
?>

<div class="wrap capiflow-settings-wrap">
	<h1 class="capiflow-settings-title">
		<span class="capiflow-logo-icon">⚡</span>
		<?php esc_html_e( 'CapiFlow Settings', 'capiflow' ); ?>
		<?php if ( $capiflow_is_pro ) : ?>
		<span class="capiflow-pro-chip">Pro</span>
		<?php endif; ?>
	</h1>

	<nav class="nav-tab-wrapper capiflow-tabs">
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=capiflow-settings&tab=general' ) ); ?>"
		   class="nav-tab <?php echo 'general' === $capiflow_active_tab ? 'nav-tab-active' : ''; ?>">
			⚙️ <?php esc_html_e( 'General', 'capiflow' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=capiflow-settings&tab=events' ) ); ?>"
		   class="nav-tab <?php echo 'events' === $capiflow_active_tab ? 'nav-tab-active' : ''; ?>">
			📡 <?php esc_html_e( 'Events', 'capiflow' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=capiflow-settings&tab=debug' ) ); ?>"
		   class="nav-tab <?php echo 'debug' === $capiflow_active_tab ? 'nav-tab-active' : ''; ?>">
			🐛 <?php esc_html_e( 'Debug Console', 'capiflow' ); ?>
		</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=capiflow-settings&tab=health' ) ); ?>"
		   class="nav-tab <?php echo 'health' === $capiflow_active_tab ? 'nav-tab-active' : ''; ?>">
			🏥 <?php esc_html_e( 'System Health', 'capiflow' ); ?>
		</a>
		<?php
		/**
		 * Pro plugin adds its own tab here.
		 * add_action( 'capiflow_settings_tabs', function() { ... } );
		 */
		do_action( 'capiflow_settings_tabs', $capiflow_active_tab );
		?>
	</nav>

	<div class="capiflow-tab-content">

		<?php if ( 'general' === $capiflow_active_tab ) : ?>
		<!-- ══════════════ GENERAL TAB ══════════════ -->
		<form method="post" action="options.php">
			<?php settings_fields( 'capiflow_general' ); ?>

			<h3><?php esc_html_e( '🔌 Pixel & API Configuration', 'capiflow' ); ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><label for="capiflow_pixel_id"><?php esc_html_e( 'Pixel ID', 'capiflow' ); ?></label></th>
					<td>
						<input type="text" name="capiflow_pixel_id" id="capiflow_pixel_id"
						       value="<?php echo esc_attr( get_option( 'capiflow_pixel_id', '' ) ); ?>"
						       class="regular-text" placeholder="e.g. 123456789012345">
						<p class="description"><?php esc_html_e( 'Your Meta (Facebook) Pixel ID from Events Manager.', 'capiflow' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="capiflow_access_token"><?php esc_html_e( 'Access Token', 'capiflow' ); ?></label></th>
					<td>
						<?php
						// Validate stored token via decryption (not just option existence).
						$capiflow_stored_enc    = get_option( 'capiflow_access_token', '' );
						$capiflow_has_token     = false;
						$capiflow_token_preview = '';
						if ( ! empty( $capiflow_stored_enc ) ) {
							$capiflow_dec = \CapiFlow\Core\Encryption::decrypt( $capiflow_stored_enc );
							if ( ! empty( $capiflow_dec ) && strlen( $capiflow_dec ) > 10 ) {
								$capiflow_has_token     = true;
								$capiflow_token_preview = substr( $capiflow_dec, 0, 6 ) . '••••••' . substr( $capiflow_dec, -4 );
							}
						}
						?>
						<div style="position:relative;display:inline-block;width:100%;max-width:400px;">
							<input type="text" name="capiflow_access_token" id="capiflow_access_token"
							       value="" class="regular-text" style="width:100%;padding-right:60px;font-family:monospace;font-size:13px;"
							       placeholder="<?php echo $capiflow_has_token ? esc_attr( $capiflow_token_preview ) : 'EAAxxxxxxx...'; ?>"
							       autocomplete="off" spellcheck="false">
						</div>
						<?php if ( $capiflow_has_token ) : ?>
						<p style="margin:4px 0 0;font-size:12px;color:#16a34a;">✓ <?php esc_html_e( 'Token saved and encrypted. Leave blank to keep existing.', 'capiflow' ); ?></p>
						<?php endif; ?>
						<p class="description"><?php esc_html_e( 'CAPI System User Access Token. Stored AES-256 encrypted.', 'capiflow' ); ?></p>
						<?php if ( $capiflow_is_pro ) : ?>
						<p class="description" style="margin-top:4px;color:#6b7280;font-style:italic;">
							💡 <?php esc_html_e( 'For EMQ Analytics: token needs ads_read + ads_management permissions. Generate from Meta Events Manager → Settings → Dataset Quality API.', 'capiflow' ); ?>
						</p>
						<?php endif; ?>
						<button type="button" id="capiflow-test-btn" class="button capiflow-test-connection" style="margin-top:6px;">
							🔗 <?php esc_html_e( 'Test Connection', 'capiflow' ); ?>
						</button>
						<span id="capiflow-test-result" class="capiflow-test-result"></span>
					</td>
				</tr>
				<tr>
					<th scope="row"><label for="capiflow_graph_api_version"><?php esc_html_e( 'Graph API Version', 'capiflow' ); ?></label></th>
					<td>
						<select name="capiflow_graph_api_version" id="capiflow_graph_api_version">
							<?php
							$capiflow_versions = array( 'v25.0', 'v24.0', 'v23.0', 'v22.0', 'v21.0', 'v20.0', 'v19.0' );
							$capiflow_current  = get_option( 'capiflow_graph_api_version', 'v25.0' );
							foreach ( $capiflow_versions as $capiflow_v ) {
								$capiflow_label = $capiflow_v . ( 'v25.0' === $capiflow_v ? ' (' . __( 'Recommended', 'capiflow' ) . ')' : '' );
								echo '<option value="' . esc_attr( $capiflow_v ) . '" ' . selected( $capiflow_current, $capiflow_v, false ) . '>' . esc_html( $capiflow_label ) . '</option>';
							}
							?>
						</select>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Environment Mode', 'capiflow' ); ?></th>
					<td>
						<?php $capiflow_env_mode = get_option( 'capiflow_environment_mode', 'production' ); ?>
						<label style="margin-right:18px;">
							<input type="radio" name="capiflow_environment_mode" value="production"
								<?php checked( $capiflow_env_mode, 'production' ); ?>
								id="capiflow_env_production">
							🟢 <?php esc_html_e( 'Production (Live)', 'capiflow' ); ?>
						</label>
						<label>
							<input type="radio" name="capiflow_environment_mode" value="test"
								<?php checked( $capiflow_env_mode, 'test' ); ?>
								id="capiflow_env_test">
							🟡 <?php esc_html_e( 'Test Mode', 'capiflow' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'Production: events count in ads & reporting. Test: events appear in Test Events tab only (not counted in ads).', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
				<tr id="capiflow-test-code-row" style="<?php echo 'test' === $capiflow_env_mode ? '' : 'display:none;'; ?>">
					<th scope="row"><label for="capiflow_test_event_code"><?php esc_html_e( 'Test Event Code', 'capiflow' ); ?></label></th>
					<td>
						<input type="text" name="capiflow_test_event_code" id="capiflow_test_event_code"
						       value="<?php echo esc_attr( get_option( 'capiflow_test_event_code', '' ) ); ?>"
						       class="regular-text" placeholder="TEST12345">
						<p class="description" style="color:#e65100;">
							⚠️ <?php esc_html_e( 'From Meta Events Manager → Test Events tab. Switch to Production when testing is complete.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Debug Mode', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_debug_mode" value="1"
							       <?php checked( get_option( 'capiflow_debug_mode', false ) ); ?>>
							<?php esc_html_e( 'Enable detailed logging (full payloads + Meta responses)', 'capiflow' ); ?>
						</label>
						<p class="description" style="color:#e65100;">
							⚠️ <?php esc_html_e( 'Disable in production — increases log file size ~50x.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Keep Data on Uninstall', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_keep_data_on_uninstall" value="1"
							       <?php checked( get_option( 'capiflow_keep_data_on_uninstall', true ) ); ?>>
							<?php esc_html_e( 'Preserve all settings and event data when plugin is deleted (recommended)', 'capiflow' ); ?>
						</label>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '🛡️ Privacy & Consent', 'capiflow' ); ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Consent Enforcement', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_consent_enforcement" value="1"
							       <?php checked( get_option( 'capiflow_consent_enforcement', false ) ); ?>>
							<?php esc_html_e( 'Enable Consent Enforcement', 'capiflow' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'When enabled, CapiFlow will respect tracking consent signals provided by a supported Consent Management Platform (CMP) or custom consent hook for browser pixel and server-side CAPI events.', 'capiflow' ); ?>
						</p>
						<p class="description" style="margin-top:4px;">
							<?php esc_html_e( 'Supported CMPs are auto-detected when available. If no supported CMP or custom consent hook is present, tracking events will continue to fire normally. Install and configure a compatible consent solution to enforce consent-based blocking.', 'capiflow' ); ?>
						</p>
						<?php if ( ! get_option( 'capiflow_consent_enforcement', false ) ) : ?>
						<p class="description" style="margin-top:6px;color:#e65100;">
							⚠️ <?php esc_html_e( 'Disabling consent enforcement may not comply with privacy regulations in certain jurisdictions (e.g., EU GDPR, UK GDPR, California CCPA). Store owners are responsible for configuring a lawful consent mechanism where required.', 'capiflow' ); ?>
						</p>
						<?php endif; ?>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '⚡ Performance', 'capiflow' ); ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Queue Interval', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_queue_interval" min="1" max="10"
						       value="<?php echo esc_attr( get_option( 'capiflow_queue_interval', 1 ) ); ?>" class="small-text">
						<?php esc_html_e( 'minutes', 'capiflow' ); ?>
						<p class="description"><?php esc_html_e( 'How often the queue processes server-side events.', 'capiflow' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Batch Size', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_batch_size" min="1" max="200"
						       value="<?php echo esc_attr( get_option( 'capiflow_batch_size', 50 ) ); ?>" class="small-text">
						<?php esc_html_e( 'events per batch (Meta allows up to 1000)', 'capiflow' ); ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'API Timeout', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_api_timeout" min="5" max="30"
						       value="<?php echo esc_attr( get_option( 'capiflow_api_timeout', 15 ) ); ?>" class="small-text">
						<?php esc_html_e( 'seconds', 'capiflow' ); ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Per-Pixel Rate Limit', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_hourly_rate_limit" min="100" max="50000"
						       value="<?php echo esc_attr( get_option( 'capiflow_hourly_rate_limit', 2000 ) ); ?>" class="small-text">
						<?php esc_html_e( 'events/hour', 'capiflow' ); ?>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Global Rate Limit', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_global_hourly_limit" min="500" max="100000"
						       value="<?php echo esc_attr( get_option( 'capiflow_global_hourly_limit', 10000 ) ); ?>" class="small-text">
						<?php esc_html_e( 'events/hour (all pixels combined)', 'capiflow' ); ?>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '🛡️ Inbound Security (IP Rate Limiting)', 'capiflow' ); ?></h3>
			<p class="description" style="margin-bottom:12px;">
				<?php esc_html_e( 'Limits how many tracking requests a single IP can send per minute. Prevents bots from exhausting your Meta API quota.', 'capiflow' ); ?>
			</p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Trusted IP Limit', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_ip_trusted_limit" min="50" max="1000"
						       value="<?php echo esc_attr( get_option( 'capiflow_ip_trusted_limit', 300 ) ); ?>" class="small-text">
						<?php esc_html_e( 'requests/min/IP', 'capiflow' ); ?>
						<p class="description">
							<?php esc_html_e( 'For verified browser requests (has Origin/Referer header). Higher limit to support CGNAT, corporate WiFi, and mobile networks where many users share one IP.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Suspicious IP Limit', 'capiflow' ); ?></th>
					<td>
						<input type="number" name="capiflow_ip_suspicious_limit" min="5" max="200"
						       value="<?php echo esc_attr( get_option( 'capiflow_ip_suspicious_limit', 30 ) ); ?>" class="small-text">
						<?php esc_html_e( 'requests/min/IP', 'capiflow' ); ?>
						<p class="description" style="color:#e65100;">
							⚠️ <?php esc_html_e( 'For requests without Origin/Referer headers (likely bots/scripts). Keep this low to block API exhaustion attacks.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '📦 Catalog & Content ID Strategy', 'capiflow' ); ?></h3>
			<p class="description" style="margin-bottom:12px;">
				<?php esc_html_e( 'Controls which product identifier (content_id) is sent in Meta Pixel & CAPI tracking events. To enable Meta Dynamic Product Ads (DPA), this must match the "id" column in your Meta Product Catalog feed.', 'capiflow' ); ?>
			</p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Content ID Strategy', 'capiflow' ); ?></th>
					<td>
						<?php $capiflow_cid_strategy = get_option( 'capiflow_content_id_strategy', 'sku' ); ?>

						<!-- Option 1: SKU -->
						<div style="margin-bottom:12px; padding:10px 14px; background:#fff; border:1px solid #dcdcde; border-radius:5px;">
							<label style="display:block; font-weight:600; margin-bottom:4px; cursor:pointer;">
								<input type="radio" name="capiflow_content_id_strategy" value="sku"
								       <?php checked( $capiflow_cid_strategy, 'sku' ); ?>>
								<?php esc_html_e( 'Product SKU (Default & Recommended)', 'capiflow' ); ?>
							</label>
							<div style="margin-left:24px; font-size:13px; color:#50575e; line-height:1.5;">
								<?php esc_html_e( 'Uses the unique SKU of each product or variation (e.g.', 'capiflow' ); ?> <code>TSHIRT-RED-XL</code><?php esc_html_e( '). If SKU is missing, automatically falls back to Product ID.', 'capiflow' ); ?><br>
								<span style="color:#0073aa; font-size:12px; font-weight:500;">
									💡 <?php esc_html_e( 'Best for: Stores where each variation (size/color) has its own SKU in the catalog feed.', 'capiflow' ); ?>
								</span>
							</div>
						</div>

						<!-- Option 2: Parent SKU -->
						<div style="margin-bottom:12px; padding:10px 14px; background:#fff; border:1px solid #dcdcde; border-radius:5px;">
							<label style="display:block; font-weight:600; margin-bottom:4px; cursor:pointer;">
								<input type="radio" name="capiflow_content_id_strategy" value="parent_sku"
								       <?php checked( $capiflow_cid_strategy, 'parent_sku' ); ?>>
								<?php esc_html_e( 'Parent Product SKU (Grouped Variations)', 'capiflow' ); ?>
							</label>
							<div style="margin-left:24px; font-size:13px; color:#50575e; line-height:1.5;">
								<?php esc_html_e( 'For variations, always sends the main parent product SKU (e.g.', 'capiflow' ); ?> <code>TSHIRT</code> <?php esc_html_e( 'instead of variation SKU).', 'capiflow' ); ?><br>
								<span style="color:#0073aa; font-size:12px; font-weight:500;">
									💡 <?php esc_html_e( 'Best for: Catalog ads that group all sizes/colors under one parent catalog item.', 'capiflow' ); ?>
								</span>
							</div>
						</div>

						<!-- Option 3: Product ID -->
						<div style="margin-bottom:12px; padding:10px 14px; background:#fff; border:1px solid #dcdcde; border-radius:5px;">
							<label style="display:block; font-weight:600; margin-bottom:4px; cursor:pointer;">
								<input type="radio" name="capiflow_content_id_strategy" value="product_id"
								       <?php checked( $capiflow_cid_strategy, 'product_id' ); ?>>
								<?php esc_html_e( 'WooCommerce Product ID (Numeric Post ID)', 'capiflow' ); ?>
							</label>
							<div style="margin-left:24px; font-size:13px; color:#50575e; line-height:1.5;">
								<?php esc_html_e( 'Always sends the numeric WooCommerce post ID (e.g.', 'capiflow' ); ?> <code>4582</code><?php esc_html_e( '). Ignores SKUs entirely.', 'capiflow' ); ?><br>
								<span style="color:#0073aa; font-size:12px; font-weight:500;">
									💡 <?php esc_html_e( 'Best for: Feed plugins (e.g. CTX Feed, Product Feed PRO) configured to export Post IDs as the catalog id.', 'capiflow' ); ?>
								</span>
							</div>
						</div>

						<!-- Warning Notice Box -->
						<div style="margin-top:14px; padding:10px 14px; background:#fff8e5; border-left:4px solid #f59e0b; border-radius:4px; font-size:13px; line-height:1.5;">
							<strong style="color:#92400e;">⚠️ <?php esc_html_e( 'Important for Active Campaigns:', 'capiflow' ); ?></strong>
							<?php esc_html_e( 'Changing this setting alters the ID sent to Meta. Ensure your Catalog feed ID matches this selection to avoid Dynamic Ad retargeting errors. Always verify with Meta Events Manager → Test Events.', 'capiflow' ); ?>
						</div>
					</td>
				</tr>
			</table>

			<?php submit_button( __( 'Save Settings', 'capiflow' ) ); ?>
		</form>

		<?php elseif ( 'events' === $capiflow_active_tab ) : ?>
		<!-- ══════════════ EVENTS TAB ══════════════ -->
		<form method="post" action="options.php">
			<?php settings_fields( 'capiflow_events' ); ?>

			<h3><?php esc_html_e( '🔥 Browser Events (Fire & Forget)', 'capiflow' ); ?></h3>
			<table class="form-table">
				<?php
				$capiflow_browser_events = array(
					'pageview'    => array( 'PageView', '🏠' ),
					'viewcontent' => array( 'ViewContent', '👁' ),
					'search'      => array( 'Search', '🔍' ),
					'addtocart'   => array( 'AddToCart', '🛒' ),
				);
				foreach ( $capiflow_browser_events as $capiflow_key => $capiflow_info ) :
				?>
				<tr>
					<th scope="row"><?php echo esc_html( $capiflow_info[1] . ' ' . $capiflow_info[0] ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_send_<?php echo esc_attr( $capiflow_key ); ?>" value="1"
							       <?php checked( get_option( 'capiflow_send_' . $capiflow_key, true ) ); ?>>
							<?php esc_html_e( 'Enable', 'capiflow' ); ?>
						</label>
					</td>
				</tr>
				<?php endforeach; ?>
			</table>

			<h3><?php esc_html_e( '💰 Queue Events (Server-Side)', 'capiflow' ); ?></h3>
			<table class="form-table">
				<?php
				$capiflow_queue_events = array(
					'initiatecheckout' => array( 'InitiateCheckout', '📋' ),
					'addpaymentinfo'   => array( 'AddPaymentInfo', '💳' ),
					'purchase'         => array( 'Purchase', '💰' ),
				);
				foreach ( $capiflow_queue_events as $capiflow_key => $capiflow_info ) :
				?>
				<tr>
					<th scope="row"><?php echo esc_html( $capiflow_info[1] . ' ' . $capiflow_info[0] ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_send_<?php echo esc_attr( $capiflow_key ); ?>" value="1"
							       <?php checked( get_option( 'capiflow_send_' . $capiflow_key, true ) ); ?>>
							<?php esc_html_e( 'Enable', 'capiflow' ); ?>
						</label>
					</td>
				</tr>
				<?php endforeach; ?>
			</table>

			<h3>
				<?php esc_html_e( '🔄 Negative Events', 'capiflow' ); ?>
				<?php if ( ! $capiflow_is_pro ) : ?>
				<span class="capiflow-pro-badge">🔒 <?php esc_html_e( 'Pro Feature', 'capiflow' ); ?></span>
				<?php endif; ?>
			</h3>
			<?php if ( ! $capiflow_is_pro ) : ?>
			<div class="capiflow-pro-teaser">
				<p>
					<?php esc_html_e( 'Send cancellation and refund signals to Meta to improve ad targeting accuracy. Available in CapiFlow Pro.', 'capiflow' ); ?>
				</p>
				<a href="https://capiflowpro.com" target="_blank" rel="noopener" class="button button-primary">
					⬆ <?php esc_html_e( 'Upgrade to Pro', 'capiflow' ); ?>
				</a>
			</div>
			<?php else : ?>
			<?php do_action( 'capiflow_settings_negative_events' ); ?>
			<?php endif; ?>

			<h3><?php esc_html_e( '⚙️ Purchase Configuration', 'capiflow' ); ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Purchase Trigger', 'capiflow' ); ?></th>
					<td>
						<p class="description">
							<?php esc_html_e( 'Purchase event fires automatically on: Processing (online payments) and Confirmed (COD orders).', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '🛒 Order Source Filter', 'capiflow' ); ?></h3>
			<p class="description" style="margin-bottom:12px;">
				<?php esc_html_e( 'Control which WooCommerce order sources trigger a CAPI Purchase event. Website checkout is always tracked. Admin-created and marketplace orders are opt-in — enabling these for off-platform sales may reduce Meta ad attribution accuracy.', 'capiflow' ); ?>
			</p>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Website Checkout', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" disabled checked>
							<strong><?php esc_html_e( 'Always tracked', 'capiflow' ); ?></strong>
						</label>
						<p class="description"><?php esc_html_e( 'Browser checkout and Blocks-based checkout (created_via: checkout, store-api) are always included.', 'capiflow' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Admin Orders', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_track_admin_orders" value="1"
							       <?php checked( get_option( 'capiflow_track_admin_orders', false ) ); ?>>
							<?php esc_html_e( 'Track manually-created orders (created via WP Admin)', 'capiflow' ); ?>
						</label>
						<p class="description"><?php esc_html_e( 'Enable if your team regularly creates orders on behalf of customers and you want these counted in Meta ad attribution.', 'capiflow' ); ?></p>
					</td>
				</tr>
				<tr>
					<th scope="row"><?php esc_html_e( 'Marketplace & External Orders', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_track_marketplace_orders" value="1"
							       <?php checked( get_option( 'capiflow_track_marketplace_orders', false ) ); ?>>
							<?php esc_html_e( 'Track orders from external channels (eBay, Amazon, REST API, import, POS)', 'capiflow' ); ?>
						</label>
						<p class="description" style="color:#e65100;">
							⚠️ <?php esc_html_e( 'Not recommended. Marketplace orders were not originated by Meta ad clicks. Including them artificially inflates ROAS and degrades Meta campaign optimization.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '🔗 OpenGraph Product Tags', 'capiflow' ); ?></h3>
			<table class="form-table">
				<tr>
					<th scope="row"><?php esc_html_e( 'Inject OG Product Tags', 'capiflow' ); ?></th>
					<td>
						<label>
							<input type="checkbox" name="capiflow_enable_og_product_tags" value="1"
							       <?php checked( get_option( 'capiflow_enable_og_product_tags', false ) ); ?>>
							<?php esc_html_e( 'Add product:retailer_item_id, product:price:amount, and product:availability Open Graph tags on product pages', 'capiflow' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'Improves Facebook/Instagram catalog matching by embedding the same content_id used in CAPI events into the page\'s OG tags. Uses your configured Content ID Strategy. Yoast WooCommerce SEO conflict is auto-detected.', 'capiflow' ); ?>
						</p>
					</td>
				</tr>
			</table>

			<h3><?php esc_html_e( '🔧 Developer Hooks', 'capiflow' ); ?></h3>
			<div style="background:#f6f7f7;border:1px solid #ddd;border-radius:4px;padding:14px 18px;font-size:13px;line-height:1.8;">
				<p style="margin:0 0 8px;font-weight:600;"><?php esc_html_e( 'Available filters for custom integrations:', 'capiflow' ); ?></p>
				<code>capiflow_content_id</code> — <?php esc_html_e( 'Override the resolved content_id for any product (e.g., custom catalog ID).', 'capiflow' ); ?><br>
				<code>capiflow_allowed_order_sources</code> — <?php esc_html_e( 'Extend the list of whitelisted order sources beyond the settings above.', 'capiflow' ); ?><br>
				<code>capiflow_should_track_purchase</code> — <?php esc_html_e( 'Return false to suppress CAPI for a specific order (no dedup lock set).', 'capiflow' ); ?>
			</div>

			<?php submit_button( __( 'Save Events', 'capiflow' ) ); ?>
		</form>

		<?php elseif ( 'debug' === $capiflow_active_tab ) : ?>
		<!-- ══════════════ DEBUG CONSOLE ══════════════ -->
		<?php include CAPIFLOW_PLUGIN_DIR . 'admin/views/debug-console.php'; ?>

		<?php elseif ( 'health' === $capiflow_active_tab ) : ?>
		<!-- ══════════════ SYSTEM HEALTH ══════════════ -->
		<?php include CAPIFLOW_PLUGIN_DIR . 'admin/views/system-health.php'; ?>

		<?php else : ?>
		<!-- Pro plugin custom tabs rendered here -->
		<?php do_action( 'capiflow_settings_tab_content_' . sanitize_key( $capiflow_active_tab ) ); ?>

		<?php endif; ?>

	</div><!-- .capiflow-tab-content -->
</div><!-- .wrap -->
