<?php
// File: modules/crawler-logs.php

defined('ABSPATH') || exit;

// Shared key validation used across ALL modules
require_once plugin_dir_path(__FILE__) . '../includes/sac-utils.php';

add_action('rest_api_init', function () {
    register_rest_route('myaio/v1', '/crawler-logs', [
        'methods'             => WP_REST_Server::READABLE,
        'callback'            => 'myaio_sac_get_crawler_logs',
        'permission_callback' => 'myaio_sac_rest_permission_check',
    ]);
});

if (!function_exists('myaio_sac_get_crawler_logs')) {
    function myaio_sac_get_crawler_logs(WP_REST_Request $request) {
        global $wp_filesystem;

        // Initialize WordPress filesystem API.
        if (empty($wp_filesystem)) {
            require_once ABSPATH . 'wp-admin/includes/file.php';
            WP_Filesystem();
        }

        $log_file = '/var/log/apache2/access.log'; // adjust path for your server

        if (!$wp_filesystem || !$wp_filesystem->exists($log_file) || !$wp_filesystem->is_readable($log_file)) {
            return new WP_REST_Response([
                'error' => 'Access log not found or not readable.',
                'path'  => esc_html($log_file),
            ], 500);
        }

        $contents = $wp_filesystem->get_contents($log_file);
        if (empty($contents)) {
            return new WP_REST_Response(['error' => 'Failed to read log file'], 500);
        }

        $lines = explode("\n", $contents);
        $bots = [];
        $bot_patterns = [
            'Googlebot'   => '/Googlebot/i',
            'Bingbot'     => '/Bingbot/i',
            'Applebot'    => '/Applebot/i',
            'YandexBot'   => '/YandexBot/i',
            'DuckDuckGo'  => '/DuckDuckBot/i',
            'AhrefsBot'   => '/AhrefsBot/i',
            'SemrushBot'  => '/SemrushBot/i',
            'MJ12bot'     => '/MJ12bot/i',
        ];

        foreach ($lines as $line) {
            if (preg_match('/\[(.*?)\] ".*? (.*?) .*?" (\d{3}) .*? "(.*?)" "(.*?)"/', $line, $matches)) {
                list($_, $timestamp, $url, $status, $referer, $ua) = $matches;
                $timestamp_unix = strtotime($timestamp);

                foreach ($bot_patterns as $bot_name => $pattern) {
                    if (preg_match($pattern, $ua)) {
                        // Ensure array is initialized (prevents notices; no output change)
                        if (!isset($bots[$bot_name]['hits'])) {
                            $bots[$bot_name]['hits'] = [];
                        }

                        $bots[$bot_name]['hits'][] = [
                            'url'        => esc_url_raw($url),
                            'status'     => (int) $status,
                            'timestamp'  => $timestamp_unix ? gmdate('c', $timestamp_unix) : null,
                            'user_agent' => sanitize_text_field($ua),
                        ];
                        break;
                    }
                }
            }
        }

        // Summarize per bot
        $summary = [];
        foreach ($bots as $bot => $data) {
            $hits = isset($data['hits']) ? $data['hits'] : [];

            $seen_times = array_column($hits, 'timestamp');

            // Avoid PHP 8 arrow functions for compatibility; keep same result.
            usort($seen_times, function ($a, $b) {
                return strtotime($a) <=> strtotime($b);
            });

            $errors = array_filter($hits, function ($h) {
                return isset($h['status']) && $h['status'] >= 400;
            });

            $redirects = array_filter($hits, function ($h) {
                return isset($h['status']) && in_array($h['status'], [301, 302], true);
            });

            $ok = array_filter($hits, function ($h) {
                return isset($h['status']) && (int) $h['status'] === 200;
            });

            $urls = array_unique(array_column($hits, 'url'));
            $status_cnt = array_count_values(array_column($hits, 'status'));

            $last_seen = null;
            if (!empty($seen_times)) {
                $tmp = $seen_times;
                $last_seen = end($tmp);
            }

            $summary[$bot] = [
                'total_visits'   => count($hits),
                'unique_urls'    => count($urls),
                'first_seen'     => $seen_times[0] ?? null,
                'last_seen'      => $last_seen,
                'error_count'    => count($errors),
                'redirect_count' => count($redirects),
                'ok_count'       => count($ok),
                'status_codes'   => $status_cnt,
                'sample_urls'    => array_slice($urls, 0, 10),
            ];
        }

        return rest_ensure_response([
            'bots_detected' => array_keys($summary),
            'bots'          => $summary,
        ]);
    }
}
