<?php

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

$data = isset( $data ) && is_array( $data ) ? $data : array( 'available' => false );
$fmt  = function ( $s ) {
	$s = (int) $s;
	if ( $s <= 0 ) {
		return '—';
	}
	if ( $s < 60 ) {
		return $s . 's';
	}
	$m = (int) floor( $s / 60 );
	$r = $s % 60;
	return $m . 'm' . ( $r ? ' ' . $r . 's' : '' );
};
?>
<div class="aieo-dmm-section">
	<h2><?php esc_html_e( 'Checkout Timing', 'ai-eshop-optimizer' ); ?></h2>
	<p class="description" style="max-width:840px;">
		<?php esc_html_e( 'How long shoppers actively engage vs sit idle during checkout, and a recommended exit-intent warm-up delay per product. The baseline EXCLUDES coupon-completed orders — a returning/fast cohort that would deflate genuine deliberation time.', 'ai-eshop-optimizer' ); ?>
	</p>

	<?php if ( empty( $data['available'] ) || empty( $data['rows'] ) ) : ?>
		<div class="aieo-dmm-card">
			<p class="description"><?php esc_html_e( 'No checkout-timing data captured yet. Rows appear here after successful checkouts (the session tracker records active vs idle time on the checkout pages).', 'ai-eshop-optimizer' ); ?></p>
		</div>
	<?php else :
		$all  = $data['all'];
		$base = $data['baseline'];
	?>
		<div class="aieo-dmm-card">
			<h3><?php printf( esc_html__( 'Overall (%d checkouts)', 'ai-eshop-optimizer' ), (int) $data['rows'] ); ?></h3>
			<table class="widefat striped" style="max-width:820px;">
				<thead><tr>
					<th></th>
					<th class="num"><?php esc_html_e( 'Median active', 'ai-eshop-optimizer' ); ?></th>
					<th class="num"><?php esc_html_e( 'Median idle', 'ai-eshop-optimizer' ); ?></th>
					<th class="num"><?php esc_html_e( 'Median total', 'ai-eshop-optimizer' ); ?></th>
					<th class="num"><?php esc_html_e( 'Recommended delay', 'ai-eshop-optimizer' ); ?></th>
				</tr></thead>
				<tbody>
					<tr>
						<td><strong><?php esc_html_e( 'Baseline (no coupon)', 'ai-eshop-optimizer' ); ?></strong> <span style="color:#9ca3af;">(<?php echo (int) $base['n']; ?>)</span></td>
						<td class="num"><?php echo esc_html( $fmt( $base['median_active'] ) ); ?></td>
						<td class="num"><?php echo esc_html( $fmt( $base['median_idle'] ) ); ?></td>
						<td class="num"><?php echo esc_html( $fmt( $base['median_total'] ) ); ?></td>
						<td class="num"><strong style="color:#15803d;"><?php echo esc_html( $fmt( $base['recommended'] ) ); ?></strong></td>
					</tr>
					<tr>
						<td><?php esc_html_e( 'All orders', 'ai-eshop-optimizer' ); ?> <span style="color:#9ca3af;">(<?php echo (int) $all['n']; ?>)</span></td>
						<td class="num"><?php echo esc_html( $fmt( $all['median_active'] ) ); ?></td>
						<td class="num"><?php echo esc_html( $fmt( $all['median_idle'] ) ); ?></td>
						<td class="num"><?php echo esc_html( $fmt( $all['median_total'] ) ); ?></td>
						<td class="num"><?php echo esc_html( $fmt( $all['recommended'] ) ); ?></td>
					</tr>
				</tbody>
			</table>
			<p class="description"><?php esc_html_e( 'Recommended delay = the baseline median active time — fire the offer around when genuine deliberation typically ends and idle begins.', 'ai-eshop-optimizer' ); ?></p>
		</div>

		<div class="aieo-dmm-card">
			<h3><?php esc_html_e( 'Per product (coupon-excluded)', 'ai-eshop-optimizer' ); ?></h3>
			<?php if ( empty( $data['products']['available'] ) ) : ?>
				<p class="description"><?php esc_html_e( 'The per-product breakdown needs the Order-Products analytics table (Tools → Product/Orders analytics). The overall figures above work without it.', 'ai-eshop-optimizer' ); ?></p>
			<?php elseif ( empty( $data['products']['rows'] ) ) : ?>
				<p class="description"><?php esc_html_e( 'No per-product rows yet.', 'ai-eshop-optimizer' ); ?></p>
			<?php else : ?>
				<table class="widefat striped">
					<thead><tr>
						<th><?php esc_html_e( 'Product', 'ai-eshop-optimizer' ); ?></th>
						<th class="num"><?php esc_html_e( 'Checkouts', 'ai-eshop-optimizer' ); ?></th>
						<th class="num"><?php esc_html_e( 'Avg active', 'ai-eshop-optimizer' ); ?></th>
						<th class="num"><?php esc_html_e( 'Avg idle', 'ai-eshop-optimizer' ); ?></th>
						<th class="num"><?php esc_html_e( 'Recommended delay', 'ai-eshop-optimizer' ); ?></th>
					</tr></thead>
					<tbody>
					<?php foreach ( $data['products']['rows'] as $r ) :
						$pid  = (int) $r['product_id'];
						$name = get_the_title( $pid );
						if ( ! $name ) { $name = '#' . $pid; }
					?>
						<tr>
							<td><a href="<?php echo esc_url( admin_url( 'post.php?post=' . $pid . '&action=edit' ) ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $name ); ?></a></td>
							<td class="num"><?php echo esc_html( number_format_i18n( (int) $r['orders'] ) ); ?></td>
							<td class="num"><?php echo esc_html( $fmt( $r['avg_active'] ) ); ?></td>
							<td class="num"><?php echo esc_html( $fmt( $r['avg_idle'] ) ); ?></td>
							<td class="num"><strong style="color:#15803d;"><?php echo esc_html( $fmt( $r['avg_active'] ) ); ?></strong></td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			<?php endif; ?>
		</div>
	<?php endif; ?>
</div>
