<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals
// phpcs:disable WordPress.Security.NonceVerification
// phpcs:disable WordPress.Security.ValidatedSanitizedInput
// phpcs:disable WordPress.WP.EnqueuedResourceParameters
// phpcs:disable WordPress.DB.PreparedSQL
// phpcs:disable WordPress.DB.PreparedSQLPlaceholders
// phpcs:disable WordPress.DB.DirectDatabaseQuery
// phpcs:disable WordPress.DB.SlowDBQuery
// phpcs:disable PluginCheck.Security.DirectDB
// phpcs:disable PluginCheck.CodeAnalysis
// phpcs:disable WordPressVIPMinimum

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
if ( ! current_user_can( 'manage_options' ) ) {
	return;
}

$license_system = class_exists( 'GT_BOGO_License_System' ) ? GT_BOGO_License_System::instance() : null;
$license_active = get_option( 'gtbogo_license_status' ) === 'active';
$wc_active      = class_exists( 'WooCommerce' );
$php_version    = PHP_VERSION;
$wp_version     = get_bloginfo( 'version' );
$wc_version     = defined( 'WC_VERSION' ) ? WC_VERSION : 'Not installed';
$memory_limit   = ini_get( 'memory_limit' );
$max_execution  = ini_get( 'max_execution_time' );
$upload_max     = ini_get( 'upload_max_filesize' );
$server_sw      = isset( $_SERVER['SERVER_SOFTWARE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['SERVER_SOFTWARE'] ) ) : 'Unknown';
global $wpdb;
$mysql_version   = $wpdb->db_version();
$settings        = get_option( 'gt_bogo_engine_settings', array() );
$free_rule_on    = ! empty( $settings['enabled'] );
$rules_table     = $wpdb->prefix . 'gtbogo_rules';
$licenses_table  = $wpdb->prefix . 'gtbogo_licenses';
$rules_exists    = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $rules_table ) ) === $rules_table; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
$licenses_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $licenses_table ) ) === $licenses_table; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
$php_ok          = version_compare( $php_version, '7.4', '>=' );
$wp_ok           = version_compare( $wp_version, '5.8', '>=' );
$wc_ok           = $wc_active && version_compare( $wc_version, '6.0', '>=' );
$memory_ok       = (int) $memory_limit >= 128;
$all_green       = $php_ok && $wp_ok && $wc_ok && $memory_ok && $rules_exists && $licenses_exists;
$total_rules     = $rules_exists ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}gtbogo_rules" ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
$active_rules    = $rules_exists ? (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}gtbogo_rules WHERE status='active'" ) : 0; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
$active_plugins  = (array) get_option( 'active_plugins', array() );
$plugin_version  = defined( 'GT_BOGO_VERSION' ) ? GT_BOGO_VERSION : GT_BOGO_VERSION;
require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>
<div class="gt-page-header gt-animate-in">
	<div>
		<h1 class="gt-page-title">SYSTEM STATUS</h1>
		<p class="gt-page-subtitle">Environment health, plugin diagnostics, and server configuration at a glance</p>
	</div>
	<span class="gt-badge <?php echo esc_html( $all_green ) ? 'gt-badge-active' : 'gt-badge-warn'; ?>">
		<?php echo esc_html( $all_green ) ? '✓ All Systems Operational' : '⚠️ Issues Detected'; ?>
	</span>
</div>

<div class="gt-metrics gt-animate-in" style="margin-bottom:24px">
	<div class="gt-metric" style="--gt-metric-c:<?php echo esc_html( $license_active ) ? '#22c55e' : '#64748b'; ?>">
		<span class="gt-metric-icon"><?php echo esc_html( $license_active ) ? '🔑' : '🔒'; ?></span>
		<div class="gt-metric-label">License</div>
		<div class="gt-metric-value"><?php echo esc_html( $license_active ) ? 'PRO' : 'Free'; ?></div>
	</div>
	<div class="gt-metric" style="--gt-metric-c:var(--gt-red)">
		<span class="gt-metric-icon">⚡</span>
		<div class="gt-metric-label">Active Rules</div>
		<div class="gt-metric-value"><?php echo esc_html( $active_rules ); ?>/15</div>
	</div>
	<div class="gt-metric" style="--gt-metric-c:<?php echo esc_html( $all_green ) ? '#22c55e' : '#f59e0b'; ?>">
		<span class="gt-metric-icon"><?php echo esc_html( $all_green ) ? '✅' : '⚠️'; ?></span>
		<div class="gt-metric-label">Requirements</div>
		<div class="gt-metric-value"><?php echo esc_html( $all_green ) ? 'Pass' : 'Issues'; ?></div>
	</div>
	<div class="gt-metric" style="--gt-metric-c:#3b82f6">
		<span class="gt-metric-icon">🐘</span>
		<div class="gt-metric-label">PHP</div>
		<div class="gt-metric-value"><?php echo esc_html( $php_version ); ?></div>
	</div>
</div>

<?php if ( ! $all_green ) : ?>
<div class="gt-notice gt-notice-warn gt-animate-in" style="margin-bottom:20px">
	⚠️ One or more requirements are not met. Review the Requirements card below and contact your host if needed.
</div>
<?php endif; ?>

<div class="gt-feature-grid gt-feature-grid--2col gt-animate-in" style="margin-bottom:20px">
	<div class="gt-card">
		<div class="gt-card-header"><h2 class="gt-card-title">🎯 Core Status</h2></div>
		<div style="display:flex;flex-direction:column">
		<?php
		$core_rows = array(
			array( 'Plugin Version', $plugin_version, 'gt-badge-active' ),
			array( 'License', $license_active ? 'PRO Active' : 'Free Version', $license_active ? 'gt-badge-active' : 'gt-badge-free' ),
			array( 'Free Rule', $free_rule_on ? 'Enabled' : 'Disabled', $free_rule_on ? 'gt-badge-active' : 'gt-badge-free' ),
			array( 'Rules Table', $rules_exists ? 'Installed' : 'Missing', $rules_exists ? 'gt-badge-active' : 'gt-badge-warn' ),
			array( 'Licenses Table', $licenses_exists ? 'Installed' : 'Missing', $licenses_exists ? 'gt-badge-active' : 'gt-badge-warn' ),
			array( 'Total Rules', $total_rules . ' created · ' . esc_html( $active_rules ) . ' active', 'gt-badge-active' ),
		);
		foreach ( $core_rows as $i => $row ) :
			?>
		<div style="display:flex;align-items:center;justify-content:space-between;padding:13px 0;<?php echo $i < count( $core_rows ) - 1 ? 'border-bottom:1px solid rgba(255,255,255,0.05)' : ''; ?>">
			<span style="font-size:13px;color:var(--gt-soft)"><?php echo esc_html( $row[0] ); ?></span>
			<span class="gt-badge <?php echo esc_attr( $row[2] ); ?>"><?php echo esc_html( $row[1] ); ?></span>
		</div>
		<?php endforeach; ?>
		</div>
	</div>
	<div class="gt-card">
		<div class="gt-card-header"><h2 class="gt-card-title">⚙️ Requirements</h2></div>
		<div style="display:flex;flex-direction:column">
		<?php
		$req_rows = array(
			array( 'PHP 7.4+', $php_version . ( $php_ok ? '' : ' — requires 7.4+' ), $php_ok ),
			array( 'WordPress 5.8+', $wp_version . ( $wp_ok ? '' : ' — requires 5.8+' ), $wp_ok ),
			array( 'WooCommerce 6.0+', $wc_active ? ( $wc_version . ( $wc_ok ? '' : ' — update recommended' ) ) : 'Not installed', $wc_ok ),
			array( 'Memory ≥ 128M', $memory_limit . ( $memory_ok ? '' : ' — 128M+ recommended' ), $memory_ok ),
		);
		foreach ( $req_rows as $i => $row ) :
			?>
		<div style="display:flex;align-items:center;justify-content:space-between;padding:13px 0;<?php echo $i < count( $req_rows ) - 1 ? 'border-bottom:1px solid rgba(255,255,255,0.05)' : ''; ?>">
			<span style="font-size:13px;color:var(--gt-soft)"><?php echo esc_html( $row[0] ); ?></span>
			<span class="gt-badge <?php echo esc_html( $row[2] ) ? 'gt-badge-active' : 'gt-badge-warn'; ?>">
			<?php
			echo esc_html( $row[2] ) ? '✓ ' : '⚠ ';
			echo esc_html( $row[1] );
			?>
			</span>
		</div>
		<?php endforeach; ?>
		</div>
	</div>
</div>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
	<div class="gt-card-header">
		<h2 class="gt-card-title">🖥️ Server Information</h2>
		<span class="gt-text-soft" style="font-size:13px">Read-only environment details</span>
	</div>
	<div class="gt-feature-grid gt-feature-grid--2col" style="gap:12px">
	<?php
	foreach ( array(
		array( 'Server Software', $server_sw ),
		array( 'MySQL Version', $mysql_version ),
		array( 'Max Execution', $max_execution . 's' ),
		array( 'Upload Max Size', $upload_max ),
		array( 'WP Site URL', get_site_url() ),
		array( 'Admin Email', get_option( 'admin_email' ) ),
	) as $row ) :
		?>
	<div style="background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:14px 16px">
		<div style="font-size:13px;text-transform:uppercase;letter-spacing:0.6px;color:var(--gt-subtle);font-weight:600;margin-bottom:6px"><?php echo esc_html( $row[0] ); ?></div>
		<div style="font-size:13px;font-weight:600;color:var(--gt-white);word-break:break-all"><?php echo esc_html( $row[1] ); ?></div>
	</div>
	<?php endforeach; ?>
	</div>
</div>


<div class="gt-card gt-animate-in" style="margin-bottom:20px;border-color:rgba(99,102,241,0.2)">
	<div class="gt-card-header">
		<h2 class="gt-card-title">&#129504; Intelligence Systems Status</h2>
		<span class="gt-badge gt-badge-active">20 Systems</span>
	</div>
	<div class="gt-feature-grid gt-feature-grid--2col" style="gap:8px">
	<?php
	$intel_systems = array(
		// [name, class, enabled_option, always_on]
		array( 'Cart Nonce Stability', 'GT_BOGO_Cart_Protection', '', true ),
		array( 'BOGO Visibility (Blocks)', 'GT_BOGO_Blocks_Compat', '', true ),
		array( 'Cart Value Intelligence', 'GT_BOGO_Cart_Value_Intelligence', 'gtbogo_cart_value_enabled', false ),
		array( 'Inventory Pressure', 'GT_BOGO_Inventory_Intelligence', '', true ),
		array( 'Conversion Timing', 'GT_BOGO_Timing_Intelligence', 'gtbogo_timing_enabled', false ),
		array( 'Price Sensitivity', 'GT_BOGO_Price_Sensitivity', 'gtbogo_price_sensitivity_enabled', false ),
		array( 'Typography Intelligence', 'GT_BOGO_Theme_Compat', '', true ),
		array( 'Spacing Intelligence', 'GT_BOGO_Theme_Compat', '', true ),
		array( 'Animation Intelligence', 'GT_BOGO_Animation_Intelligence', '', true ),
		array( 'Page Builder Detection', 'GT_BOGO_Page_Builder_Compat', '', true ),
		array( 'Extended Cache Detection', 'GT_BOGO_Cache_Compat', '', true ),
		array( 'Repeat Purchase Prediction', 'GT_BOGO_Repeat_Prediction', 'gtbogo_rfm_enabled', false ),
		array( 'Bundle Intelligence', 'GT_BOGO_Bundle_Intelligence', 'gtbogo_bundle_intel_enabled', false ),
		array( 'Subscription Intelligence', 'GT_BOGO_Subscription_Intelligence', '', true ),
		array( 'Multi-Currency Intelligence', 'GT_BOGO_Multicurrency_Intelligence', '', true ),
		array( 'Rule Performance Analytics', 'GT_BOGO_Rule_Analytics', '', true ),
		array( 'Revenue Attribution', 'GT_BOGO_Revenue_Attribution', '', true ),
		array( 'Payment Gateway Compat', 'GT_BOGO_Payment_Compat', '', true ),
		array( 'Theme Framework Detection', 'GT_BOGO_Theme_Framework', '', true ),
		array( 'Cohort Intelligence', 'GT_BOGO_Cohort_Intelligence', 'gtbogo_cohort_enabled', false ),
	);
	foreach ( $intel_systems as $sys ) :
		$class_exists = class_exists( $sys[1] );
		$always_on    = $sys[3];
		$opt          = $sys[2];
		$enabled      = $always_on ? true : (bool) get_option( $opt, 0 );
		$active       = $class_exists && $enabled;
		$status_color = $active ? '#22c55e' : ( $class_exists ? '#64748b' : '#ef4444' );
		$status_label = $active ? 'Active' : ( $class_exists ? 'Disabled' : 'Missing' );
		?>
	<div style="display:flex;align-items:center;justify-content:space-between;background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:8px;padding:10px 14px">
		<div style="display:flex;align-items:center;gap:8px">
			<span style="width:8px;height:8px;border-radius:50%;background:<?php echo esc_attr( $status_color ); ?>;flex-shrink:0;display:inline-block"></span>
			<span style="font-size:13px;color:var(--gt-white)"><?php echo esc_html( $sys[0] ); ?></span>
		</div>
		<span style="font-size:11px;font-weight:700;color:<?php echo esc_attr( $status_color ); ?>"><?php echo esc_html( $status_label ); ?></span>
	</div>
	<?php endforeach; ?>
	</div>
	<?php
	$disabled_count = count(
		array_filter(
			$intel_systems,
			function ( $s ) {
				return ! $s[3] && ! get_option( $s[2], 0 );
			}
		)
	);
	if ( $disabled_count > 0 ) :
		?>
	<div style="margin-top:16px;padding:12px 16px;background:rgba(99,102,241,0.06);border:1px solid rgba(99,102,241,0.2);border-radius:8px;font-size:13px;color:var(--gt-subtle)">
		&#129504; <?php echo esc_html( $disabled_count ); ?> intelligence system<?php echo $disabled_count !== 1 ? 's are' : ' is'; ?> disabled. Enable them in <a href="<?php echo esc_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=settings' ) ); ?>" style="color:var(--gt-red)">Settings → Intelligence Systems</a>.
	</div>
	<?php endif; ?>
</div>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
	<div class="gt-card-header">
		<h2 class="gt-card-title">🔌 Active Plugins</h2>
		<span class="gt-badge gt-badge-active"><?php echo count( $active_plugins ); ?> active</span>
	</div>
	<div style="display:flex;flex-wrap:wrap;gap:8px">
	<?php
	foreach ( $active_plugins as $plugin ) :
		$slug  = dirname( $plugin );
		$is_gt = strpos( $plugin, 'gt-bogo' ) !== false;
		$is_wc = strpos( $plugin, 'woocommerce' ) !== false;
		?>
	<span style="font-size:13px;font-weight:600;padding:4px 12px;border-radius:50px;
		background:<?php echo esc_html( $is_gt ) ? 'rgba(225,0,0,0.1)' : ( $is_wc ? 'rgba(102,0,255,0.1)' : 'rgba(255,255,255,0.04)' ); ?>;
		border:1px solid <?php echo esc_html( $is_gt ) ? 'rgba(225,0,0,0.3)' : ( $is_wc ? 'rgba(102,0,255,0.2)' : 'var(--gt-border)' ); ?>;
		color:<?php echo esc_html( $is_gt ) ? 'var(--gt-red)' : ( $is_wc ? '#a78bfa' : 'var(--gt-soft)' ); ?>">
		<?php echo esc_html( $slug === '.' ? $plugin : $slug ); ?>
	</span>
	<?php endforeach; ?>
	</div>
</div>

<div class="gt-card gt-animate-in" style="margin-bottom:20px;background:linear-gradient(135deg,rgba(59,130,246,0.06),rgba(7,12,28,0));border-color:rgba(59,130,246,0.15)">
	<div class="gt-card-header">
		<h2 class="gt-card-title">💡 Production Readiness Checklist</h2>
		<span class="gt-text-soft" style="font-size:13px">Recommended configuration for live stores</span>
	</div>
	<div class="gt-feature-grid gt-feature-grid--2col" style="gap:12px">
	<?php
	foreach ( array(
		array( 'PHP 8.0+', version_compare( $php_version, '8.0', '>=' ), 'Improves performance and future compatibility. Ask your host to upgrade.' ),
		array( 'WooCommerce 8.0+', version_compare( $wc_version, '8.0', '>=' ), 'Ensures latest cart hooks and HPOS support. Keep WooCommerce current.' ),
		array( 'Memory Limit ≥ 256M', (int) $memory_limit >= 256, 'Prevents exhaustion during complex multi-rule cart evaluations.' ),
		array( 'PRO License Active', $license_active, 'Unlocks all 200 PRO rule packs across 19 industries (2,000 expert-built rules) and every enterprise feature.' ),
		array( 'Free Rule Configured', $free_rule_on, 'Your first BOGO deal is live. Go to Free Rule to configure it.' ),
		array( 'Both DB Tables Present', $rules_exists && $licenses_exists, 'Required for rule storage and license validation. Reinstall if missing.' ),
	) as $item ) :
		?>
	<div style="display:flex;align-items:flex-start;gap:12px;background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:14px">
		<span style="font-size:18px;flex-shrink:0;margin-top:1px"><?php echo esc_html( $item[1] ) ? '✅' : '⬜'; ?></span>
		<div>
			<div style="font-size:13px;font-weight:700;color:var(--gt-white);margin-bottom:3px"><?php echo esc_html( $item[0] ); ?></div>
			<div style="font-size:13px;color:var(--gt-subtle);line-height:1.4"><?php echo esc_html( $item[2] ); ?></div>
		</div>
	</div>
	<?php endforeach; ?>
	</div>
</div>


<?php
// ── Cron Coordinator Status ───────────────────────────────────────────────────
$cron_summary = class_exists( 'GT_BOGO_Cron_Coordinator' ) ? GT_BOGO_Cron_Coordinator::get_summary() : null;
$cron_status  = class_exists( 'GT_BOGO_Cron_Coordinator' ) ? GT_BOGO_Cron_Coordinator::get_status() : array();
$cron_log     = class_exists( 'GT_BOGO_Cron_Coordinator' ) ? GT_BOGO_Cron_Coordinator::get_execution_log( 10 ) : array();
$cron_health  = $cron_summary ? ( $cron_summary['missing'] === 0 && $cron_summary['mismatch'] === 0 ? 'good' : 'warn' ) : 'unknown';
?>
<?php
// ── Rate Limiter Status ───────────────────────────────────────────────────────
if ( class_exists( 'GT_BOGO_Rate_Limiter' ) ) :
	$rl_limits = GT_BOGO_Rate_Limiter::get_limits();
	?>
<div class="gt-card gt-animate-in" style="margin-bottom:20px">
	<div class="gt-card-header">
		<h2 class="gt-card-title">🛡 AJAX Rate Limiter</h2>
		<span class="gt-text-soft" style="font-size:13px"><?php echo esc_html( count( $rl_limits ) - 1 ); ?> actions protected &bull; IP-based &bull; Cloudflare-aware</span>
		<span style="font-size:11px;font-weight:700;color:#22c55e;padding:3px 10px;border-radius:50px;background:rgba(34,197,94,0.1);border:1px solid rgba(34,197,94,0.3)">All nopriv endpoints protected</span>
	</div>
	<div style="overflow-x:auto">
	<table style="width:100%;border-collapse:collapse;font-size:12px">
	<thead><tr>
		<th style="text-align:left;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Action</th>
		<th style="text-align:right;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Limit</th>
		<th style="text-align:right;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Window</th>
		<th style="text-align:right;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Type</th>
	</tr></thead>
	<tbody>
	<?php
	foreach ( $rl_limits as $action => [$limit, $window] ) :
		if ( $action === '_default' ) {
			continue;
		}
		$type  = $limit >= 60 ? 'standard' : ( $limit >= 20 ? 'restricted' : 'sensitive' );
		$color = $type === 'standard' ? '#22c55e' : ( $type === 'restricted' ? '#f59e0b' : '#E10000' );
		?>
	<tr style="border-bottom:1px solid var(--gt-border)">
		<td style="padding:7px 12px;font-family:monospace;font-size:11px;color:var(--gt-white)"><?php echo esc_html( $action ); ?></td>
		<td style="padding:7px 12px;text-align:right;color:var(--gt-soft)"><?php echo esc_html( $limit ); ?> req</td>
		<td style="padding:7px 12px;text-align:right;color:var(--gt-soft)"><?php echo esc_html( $window ); ?>s</td>
		<td style="padding:7px 12px;text-align:right"><span style="color:<?php echo esc_attr( $color ); ?>;font-size:11px;font-weight:700"><?php echo esc_html( strtoupper( $type ) ); ?></span></td>
	</tr>
	<?php endforeach; ?>
	</tbody>
	</table>
	</div>
</div>
<?php endif; ?>

<div class="gt-card gt-animate-in" style="margin-bottom:20px">
	<div class="gt-card-header">
		<h2 class="gt-card-title">⏱ WP-Cron Coordinator</h2>
		<span class="gt-text-soft" style="font-size:13px">
			<?php if ( $cron_summary ) : ?>
				<?php echo esc_html( $cron_summary['scheduled'] ); ?> scheduled &bull;
				<?php echo esc_html( $cron_summary['missing'] ); ?> missing &bull;
				<?php echo esc_html( $cron_summary['disabled'] ); ?> disabled &bull;
				<?php echo esc_html( $cron_summary['total'] ); ?> total
				<?php
			else :
				?>
				Coordinator not loaded<?php endif; ?>
		</span>
		<?php if ( $cron_health === 'good' ) : ?>
		<span style="font-size:11px;font-weight:700;color:#22c55e;padding:3px 10px;border-radius:50px;background:rgba(34,197,94,0.1);border:1px solid rgba(34,197,94,0.3)">All crons healthy</span>
		<?php elseif ( $cron_health === 'warn' ) : ?>
		<span style="font-size:11px;font-weight:700;color:#f59e0b;padding:3px 10px;border-radius:50px;background:rgba(245,158,11,0.1);border:1px solid rgba(245,158,11,0.3)">Issues detected</span>
		<?php endif; ?>
	</div>
	<?php if ( $cron_status ) : ?>
	<div style="overflow-x:auto;margin-bottom:20px">
	<table style="width:100%;border-collapse:collapse;font-size:12px">
	<thead><tr>
		<th style="text-align:left;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Hook</th>
		<th style="text-align:left;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Schedule</th>
		<th style="text-align:left;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Next Run (UTC)</th>
		<th style="text-align:left;padding:8px 12px;color:var(--gt-subtle);font-weight:600;border-bottom:1px solid var(--gt-border)">Status</th>
	</tr></thead>
	<tbody>
		<?php
		foreach ( $cron_status as $row ) :
			$sc    = $row['status'];
			$color = $sc === 'ok' ? '#22c55e' : ( $sc === 'disabled' ? '#64748b' : '#ef4444' );
			?>
	<tr style="border-bottom:1px solid var(--gt-border)">
		<td style="padding:7px 12px;color:var(--gt-white);font-family:monospace;font-size:11px"><?php echo esc_html( $row['hook'] ); ?></td>
		<td style="padding:7px 12px;color:var(--gt-soft)"><?php echo esc_html( $row['schedule'] ); ?></td>
		<td style="padding:7px 12px;color:var(--gt-soft)"><?php echo esc_html( $row['next_run'] ); ?></td>
		<td style="padding:7px 12px"><span style="color:<?php echo esc_attr( $color ); ?>;font-weight:700;font-size:11px"><?php echo esc_html( strtoupper( $row['status'] ) ); ?></span></td>
	</tr>
		<?php endforeach; ?>
	</tbody>
	</table>
	</div>
	<?php endif; ?>
	<?php if ( $cron_log ) : ?>
	<div>
		<p style="font-size:12px;font-weight:700;color:var(--gt-subtle);text-transform:uppercase;letter-spacing:.06em;margin:0 0 10px">Recent Executions (last 10)</p>
		<div style="overflow-x:auto">
		<table style="width:100%;border-collapse:collapse;font-size:12px">
		<thead><tr>
			<th style="text-align:left;padding:6px 10px;color:var(--gt-subtle);border-bottom:1px solid var(--gt-border)">Hook</th>
			<th style="text-align:left;padding:6px 10px;color:var(--gt-subtle);border-bottom:1px solid var(--gt-border)">Ran At</th>
			<th style="text-align:right;padding:6px 10px;color:var(--gt-subtle);border-bottom:1px solid var(--gt-border)">Duration</th>
			<th style="text-align:right;padding:6px 10px;color:var(--gt-subtle);border-bottom:1px solid var(--gt-border)">Memory</th>
		</tr></thead>
		<tbody>
		<?php foreach ( $cron_log as $entry ) : ?>
		<tr style="border-bottom:1px solid var(--gt-border)">
			<td style="padding:5px 10px;font-family:monospace;font-size:11px;color:var(--gt-white)"><?php echo esc_html( $entry['hook'] ); ?></td>
			<td style="padding:5px 10px;color:var(--gt-soft)"><?php echo esc_html( $entry['ran_at'] ); ?></td>
			<td style="padding:5px 10px;text-align:right;color:<?php echo (float) $entry['duration'] > 5 ? '#f59e0b' : 'var(--gt-soft)'; ?>"><?php echo esc_html( number_format( (float) $entry['duration'], 3 ) ); ?>s</td>
			<td style="padding:5px 10px;text-align:right;color:var(--gt-soft)"><?php echo esc_html( number_format( (int) $entry['mem_kb'] ) ); ?>KB</td>
		</tr>
		<?php endforeach; ?>
		</tbody>
		</table>
		</div>
	</div>
	<?php endif; ?>
	<div style="margin-top:16px;display:flex;gap:10px;flex-wrap:wrap">
		<a href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=system-status&gtbogo_force_cron=1' ), 'gtbogo_force_cron' ) ); ?>"
			class="gt-btn gt-btn-ghost" style="font-size:12px">Force Reschedule All Crons</a>
	</div>
</div>

<div class="gt-card gt-animate-in">
	<div class="gt-card-header"><h2 class="gt-card-title">🔧 Quick Actions</h2></div>
	<div style="display:flex;gap:12px;flex-wrap:wrap">
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=license' ) ); ?>" class="gt-btn gt-btn-primary">🔑 Manage License</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=free-rule' ) ); ?>" class="gt-btn gt-btn-ghost">⚙️ Free Rule</a>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=gt-bogo-engine&tab=settings' ) ); ?>" class="gt-btn gt-btn-ghost">⚙️ Settings</a>
		<a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="gt-btn gt-btn-ghost">🔌 Plugins</a>
		<a href="https://graphictshirts.shop/bogo/" target="_blank" rel="noopener" class="gt-btn gt-btn-ghost">🌐 GT BOGO Website ↗</a>
	</div>
</div>
<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
