<?php
/**
 * AI Crawlers — recent activity + recognised bot families.
 *
 * @package WPLens
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// phpcs:disable WordPress.Security.NonceVerification.Recommended

$repo   = new \WPLens\Crawler\LogRepository();
$recent = $repo->recent( 100 );
$bots   = \WPLens\Crawler\BotRegistry::all();
?>
<div class="wrap lens-wrap">
	<h1>
		<img class="lens-icon" src="<?php echo esc_url( WPLENS_PLUGIN_URL . 'assets/icon.svg' ); ?>" alt="" />
		<?php esc_html_e( 'AI Crawlers', 'lens-ai-visibility-monitor' ); ?>
	</h1>
	<?php settings_errors( 'lens-ai-visibility-monitor' ); ?>

	<div class="lens-card">
		<header style="margin-bottom:12px;"><h2 style="margin:0;font-size:18px;"><?php esc_html_e( 'Recognised bot families', 'lens-ai-visibility-monitor' ); ?></h2></header>
		<div class="lens-grid cols-3">
			<?php
			$families = array();
			foreach ( $bots as $slug => $info ) {
				$families[ $info['family'] ][] = $info['label'];
			}
			foreach ( $families as $family => $labels ) :
			?>
			<div class="lens-card hoverable" style="padding:14px;">
				<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;">
					<strong style="font-size:14px;"><?php echo esc_html( $family ); ?></strong>
					<span class="lens-pill"><?php echo (int) count( $labels ); ?></span>
				</div>
				<div style="font-size:12px;color:#646970;font-family:ui-monospace,Consolas,monospace;line-height:1.6;">
					<?php echo esc_html( implode( ', ', $labels ) ); ?>
				</div>
			</div>
			<?php endforeach; ?>
		</div>
	</div>

	<div class="lens-card" style="margin-top:20px;">
		<header style="display:flex;justify-content:space-between;margin-bottom:12px;">
			<h2 style="margin:0;font-size:18px;"><?php esc_html_e( 'Recent activity', 'lens-ai-visibility-monitor' ); ?></h2>
			<div class="actions" style="display:flex;gap:8px;">
				<input id="lens-crawler-filter" type="search" placeholder="<?php esc_attr_e( 'Filter bots…', 'lens-ai-visibility-monitor' ); ?>" class="lens-input" style="width:200px;">
			</div>
		</header>
		<?php if ( empty( $recent ) ) : ?>
			<div class="lens-empty">
				<h3><?php esc_html_e( 'No activity yet', 'lens-ai-visibility-monitor' ); ?></h3>
				<p><?php esc_html_e( 'AI bots tend to start crawling within 24–48 hours of a new site going live. Some only revisit weekly. Check back later — this dashboard auto-populates as bots arrive.', 'lens-ai-visibility-monitor' ); ?></p>
			</div>
		<?php else : ?>
			<table class="lens-table" id="lens-crawler-table">
				<thead><tr>
					<th><?php esc_html_e( 'When', 'lens-ai-visibility-monitor' ); ?></th>
					<th><?php esc_html_e( 'Bot', 'lens-ai-visibility-monitor' ); ?></th>
					<th><?php esc_html_e( 'Family', 'lens-ai-visibility-monitor' ); ?></th>
					<th><?php esc_html_e( 'URI', 'lens-ai-visibility-monitor' ); ?></th>
					<th><?php esc_html_e( 'Status', 'lens-ai-visibility-monitor' ); ?></th>
				</tr></thead>
				<tbody>
					<?php foreach ( $recent as $row ) : ?>
						<tr>
							<td class="muted"><?php echo esc_html( $row['visited_at'] ); ?></td>
							<td><?php echo esc_html( $row['bot_slug'] ); ?></td>
							<td><span class="lens-pill"><?php echo esc_html( $row['bot_family'] ); ?></span></td>
							<td class="mono"><?php echo esc_html( $row['request_uri'] ); ?></td>
							<td><?php
								$code = (int) $row['response_code'];
								$cls  = $code < 400 ? 'ok' : ( $code < 500 ? 'warn' : 'bad' );
								echo '<span class="lens-pill ' . esc_attr( $cls ) . '">' . esc_html( $code ) . '</span>';
							?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>

	<?php if ( current_user_can( 'manage_options' ) ) : ?>
	<div class="lens-card" style="margin-top:20px;">
		<header style="margin-bottom:12px;"><h2 style="margin:0;font-size:18px;"><?php esc_html_e( 'Maintenance', 'lens-ai-visibility-monitor' ); ?></h2></header>
		<form method="post" onsubmit="return confirm('<?php echo esc_js( __( 'Wipe all crawler log rows? This cannot be undone.', 'lens-ai-visibility-monitor' ) ); ?>');">
			<?php wp_nonce_field( 'wplens_wipe_logs' ); ?>
			<button type="submit" name="wplens_wipe_logs" class="lens-btn" style="color:#b32d2e;border-color:#b32d2e;"><?php esc_html_e( 'Wipe all crawler logs', 'lens-ai-visibility-monitor' ); ?></button>
		</form>
	</div>
	<?php endif; ?>
</div>

