<?php
/**
 * AI Crawlers tab body.
 *
 * Rendered via kantan_render_ai_crawlers_tab(). Available vars:
 *   $days, $totals (array: hits, crawlers, pages, crawl_hits, fetch_hits),
 *   $crawler_rows (objects: crawler, crawler_type, hits, pages, last_seen),
 *   $top_pages (objects: page_url, hits, crawlers, last_seen),
 *   $show_seo_upsell (bool), $upsell_full (bool)
 */
if (!defined('ABSPATH')) {
    exit;
}

// Crawler-type presentation. 'crawl' = indexing/training; 'fetch' = a human asked
// an assistant to read the page live.
$type_meta = array(
    'crawl' => array('label' => __('Training crawl', 'kantan-analytics'), 'pill' => 'bg-amber-100 text-amber-800'),
    'fetch' => array('label' => __('Live fetch', 'kantan-analytics'),     'pill' => 'bg-emerald-100 text-emerald-800'),
);
$bar_palette = array('bg-blue-500', 'bg-emerald-500', 'bg-amber-500', 'bg-purple-500', 'bg-pink-500', 'bg-cyan-500', 'bg-orange-500', 'bg-indigo-500');
?>

<!-- ==================== AI CRAWLERS TAB ==================== -->

<div class="flex justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-900"><?php esc_html_e('AI Crawlers', 'kantan-analytics'); ?></h1>
    <?php /* translators: %d: number of days in the reporting period */ ?>
    <span class="text-sm text-gray-400"><?php printf(esc_html__('Last %d days', 'kantan-analytics'), (int) $days); ?></span>
</div>

<?php if ($totals['hits'] <= 0): ?>

    <!-- Empty state -->
    <div class="bg-white rounded-xl shadow-md p-6 mb-8">
        <div class="text-center py-10">
            <svg class="w-12 h-12 text-gray-300 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 3v4M3 5h4M6 17v4m-2-2h4m5-16l2.286 6.857L21 12l-5.714 2.143L13 21l-2.286-6.857L5 12l5.714-2.143L13 3z"></path>
            </svg>
            <p class="text-gray-700 font-medium"><?php esc_html_e('No AI crawlers seen yet', 'kantan-analytics'); ?></p>
            <p class="text-gray-500 text-sm mt-1 max-w-md mx-auto">
                <?php esc_html_e('When an AI crawler like GPTBot, ClaudeBot, PerplexityBot, or CCBot fetches a page on your site, it will show up here — recorded server-side, since crawlers do not run the tracking script. They are kept entirely separate from your human visitor stats.', 'kantan-analytics'); ?>
            </p>
        </div>
    </div>

<?php else: ?>

    <!-- Headline stats -->
    <div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-8">
        <div class="bg-white rounded-xl shadow-md p-6">
            <p class="text-sm font-medium text-gray-500"><?php esc_html_e('Crawler hits', 'kantan-analytics'); ?></p>
            <p class="text-3xl font-bold text-gray-900 mt-1"><?php echo number_format($totals['hits']); ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-md p-6">
            <p class="text-sm font-medium text-gray-500"><?php esc_html_e('AI crawlers', 'kantan-analytics'); ?></p>
            <p class="text-3xl font-bold text-gray-900 mt-1"><?php echo number_format($totals['crawlers']); ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-md p-6">
            <p class="text-sm font-medium text-gray-500"><?php esc_html_e('Pages crawled', 'kantan-analytics'); ?></p>
            <p class="text-3xl font-bold text-gray-900 mt-1"><?php echo number_format($totals['pages']); ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-md p-6">
            <p class="text-sm font-medium text-gray-500"><?php esc_html_e('Live fetches', 'kantan-analytics'); ?></p>
            <p class="text-3xl font-bold text-gray-900 mt-1"><?php echo number_format($totals['fetch_hits']); ?></p>
            <p class="text-xs text-gray-400 mt-1"><?php
                /* translators: %s: number of training-crawl hits (already formatted) */
                printf(esc_html__('%s training crawls', 'kantan-analytics'), number_format($totals['crawl_hits']));
            ?></p>
        </div>
    </div>

    <!-- By crawler -->
    <div class="bg-white rounded-xl shadow-md p-6 mb-8">
        <div class="flex items-center justify-between mb-6">
            <h2 class="text-2xl font-bold text-gray-900"><?php esc_html_e('By Crawler', 'kantan-analytics'); ?></h2>
            <?php kantan_ai_prompts('analytics-crawlers'); ?>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Crawler', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Type', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Hits', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Pages', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Last seen', 'kantan-analytics'); ?></th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php foreach ($crawler_rows as $row):
                        $type = isset($type_meta[$row->crawler_type]) ? $type_meta[$row->crawler_type] : $type_meta['crawl'];
                        $pct = $totals['hits'] > 0 ? round(($row->hits / $totals['hits']) * 100, 1) : 0;
                    ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="text-gray-800 font-medium"><?php echo esc_html($row->crawler); ?></span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap">
                                <span class="px-2 py-0.5 inline-flex text-xs font-medium rounded <?php echo esc_attr($type['pill']); ?>"><?php echo esc_html($type['label']); ?></span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right">
                                <span class="text-gray-800 font-medium"><?php echo number_format($row->hits); ?></span>
                                <span class="text-xs text-gray-400">(<?php echo esc_html($pct); ?>%)</span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-gray-600"><?php echo number_format($row->pages); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php
                                /* translators: %s: human-readable time difference, e.g. "3 hours" */
                                printf(esc_html__('%s ago', 'kantan-analytics'), human_time_diff(strtotime($row->last_seen)));
                            ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <!-- Most-crawled pages -->
    <div class="bg-white rounded-xl shadow-md p-6 mb-8">
        <div class="flex justify-between items-center mb-6">
            <h2 class="text-2xl font-bold text-gray-900"><?php esc_html_e('Most-Crawled Pages', 'kantan-analytics'); ?></h2>
            <span class="text-sm text-gray-400"><?php esc_html_e('Top 10', 'kantan-analytics'); ?></span>
        </div>
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Page', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Hits', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Crawlers', 'kantan-analytics'); ?></th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"><?php esc_html_e('Last seen', 'kantan-analytics'); ?></th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php foreach ($top_pages as $page): ?>
                        <tr class="hover:bg-gray-50">
                            <td class="px-6 py-4">
                                <div class="text-sm text-gray-700 truncate" style="max-width: 420px;"><?php echo esc_html($page->page_url); ?></div>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-gray-800 font-medium"><?php echo number_format($page->hits); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-gray-600"><?php echo number_format($page->crawlers); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?php
                                /* translators: %s: human-readable time difference, e.g. "3 hours" */
                                printf(esc_html__('%s ago', 'kantan-analytics'), human_time_diff(strtotime($page->last_seen)));
                            ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

<?php endif; ?>

<?php if ($show_seo_upsell): ?>
    <?php if ($upsell_full): ?>

        <!-- SEO upsell — full (directly-distributed build) -->
        <div class="rounded-xl border border-blue-200 bg-gradient-to-br from-blue-50 to-indigo-50 p-6">
            <div class="flex flex-col lg:flex-row lg:items-center gap-6">
                <div class="flex-1">
                    <span class="inline-block text-xs font-semibold uppercase tracking-wider text-blue-700 bg-blue-100 rounded px-2 py-0.5 mb-3">Kantan SEO</span>
                    <h2 class="text-xl font-bold text-gray-900"><?php esc_html_e('Turn AI crawling into an AEO strategy', 'kantan-analytics'); ?></h2>
                    <p class="text-gray-600 text-sm mt-2 max-w-xl"><?php
                        echo wp_kses_post(__('Kantan Analytics shows you <em>which</em> AI crawlers hit your site. Kantan SEO shows you <em>whether it is trending up</em> and <em>whether the right pages</em> are being crawled.', 'kantan-analytics'));
                    ?></p>
                    <ul class="mt-4 space-y-2 text-sm text-gray-700">
                        <li class="flex items-start gap-2"><svg class="w-5 h-5 text-blue-600 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg><?php echo wp_kses_post(__('<strong>AI-crawl trend</strong> over time, per crawler', 'kantan-analytics')); ?></li>
                        <li class="flex items-start gap-2"><svg class="w-5 h-5 text-blue-600 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg><?php echo wp_kses_post(__('<strong>Coverage gaps</strong> — which of your key pages AI has <em>not</em> crawled', 'kantan-analytics')); ?></li>
                        <li class="flex items-start gap-2"><svg class="w-5 h-5 text-blue-600 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path></svg><?php echo wp_kses_post(__('<strong>AEO readiness</strong> — schema and answer-formatting tuned for AI engines', 'kantan-analytics')); ?></li>
                    </ul>
                </div>
                <div class="lg:w-64 flex-shrink-0">
                    <div class="bg-white rounded-lg border border-gray-200 p-4 mb-4">
                        <p class="text-xs text-gray-400 mb-2"><?php esc_html_e('AI-crawl trend (with SEO)', 'kantan-analytics'); ?></p>
                        <div class="flex items-center justify-between text-sm py-1">
                            <span class="text-gray-500">GPTBot</span>
                            <span class="font-mono text-gray-300 blur-[2px] select-none">+3•%</span>
                        </div>
                        <div class="flex items-center justify-between text-sm py-1">
                            <span class="text-gray-500">PerplexityBot</span>
                            <span class="font-mono text-gray-300 blur-[2px] select-none">+1••%</span>
                        </div>
                    </div>
                    <a href="<?php echo esc_url('https://trykantan.com/seo/'); ?>" target="_blank" rel="noopener" class="block w-full text-center px-4 py-2.5 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 transition text-sm"><?php esc_html_e('Upgrade to Kantan SEO', 'kantan-analytics'); ?></a>
                </div>
            </div>
        </div>

    <?php else: ?>

        <!-- SEO upsell — restrained (wp.org build) -->
        <div class="rounded-xl border border-gray-200 bg-gray-50 p-6">
            <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                <div>
                    <h2 class="text-base font-semibold text-gray-900"><?php esc_html_e('Turn AI crawling into an AEO strategy', 'kantan-analytics'); ?></h2>
                    <p class="text-gray-600 text-sm mt-1 max-w-2xl"><?php esc_html_e('Kantan SEO tracks your AI-crawl trend over time, flags key pages AI has not crawled yet, and tunes your schema and answer formatting for AI engines.', 'kantan-analytics'); ?></p>
                </div>
                <a href="<?php echo esc_url('https://trykantan.com/seo/'); ?>" target="_blank" rel="noopener" class="flex-shrink-0 text-sm font-medium text-blue-600 hover:text-blue-700 hover:underline whitespace-nowrap"><?php esc_html_e('Learn more about Kantan SEO →', 'kantan-analytics'); ?></a>
            </div>
        </div>

    <?php endif; ?>
<?php endif; ?>
