<?php

namespace XZD\AI\V5\Agents;

use XZD\AI\V5\Core\Logger;
use XZD\AI\V5\Tools\SiteTypeDetector;

/**
 * Monitor Agent — 主动智能引擎
 *
 * 核心原则：每个决策基于实时真实数据，不做假设。
 * - 商城消息 → 必须有商品 + 用户实际购物车/订单数据
 * - 论坛消息 → 必须有论坛帖子 + 用户实际互动数据
 * - 推荐文章 → 从知识库或真实文章表查询
 */
class MonitorAgent extends BaseAgent
{
    /** @var array|null 真实站点上下文缓存 */
    private ?array $realCtx = null;

    public function getId(): string
    {
        return 'monitor';
    }

    public function getName(): string
    {
        return '监控 Agent';
    }

    public function getDescription(): string
    {
        return '分析用户行为和系统状态，主动提供AI建议';
    }

    public function getToolCategories(): array
    {
        return ['content', 'wp', 'seo'];
    }

    public function getMinRole(): string
    {
        return 'admin';
    }

    /**
     * 获取真实站点上下文（缓存单次请求）
     *
     * 不做假设，全部查真实数据。
     */
    private function getRealContext(): array
    {
        if ($this->realCtx !== null) return $this->realCtx;

        global $wpdb;

        // 实际产品数
        $productCount = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('shop_product','product') AND post_status='publish'"
        );

        // 实际论坛帖子数
        $forumCount = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type IN ('forum_post','topic','reply') AND post_status='publish'"
        );

        // 实际文章数
        $postCount = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish'"
        );

        // 知识库条目数
        $knowledgeCount = (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->prefix}xzd_ai_knowledge WHERE status='active'"
        );

        // 站点类型标签
        $siteType = $this->getSiteType();
        $types = $siteType['types'] ?? ['general'];

        // 对 shop/forum 标签做真实性校验：有类型标签但无实际数据 → 降级
        $verifiedTypes = $types;
        if (in_array('shop', $verifiedTypes) && $productCount === 0) {
            $verifiedTypes = array_values(array_diff($verifiedTypes, ['shop']));
        }
        if (in_array('forum', $verifiedTypes) && $forumCount === 0) {
            $verifiedTypes = array_values(array_diff($verifiedTypes, ['forum']));
        }
        if (empty($verifiedTypes)) $verifiedTypes = ['blog'];

        $this->realCtx = [
            'product_count'   => $productCount,
            'forum_count'     => $forumCount,
            'post_count'      => $postCount,
            'knowledge_count' => $knowledgeCount,
            'types'           => $verifiedTypes,
            'primary'         => $verifiedTypes[0] ?? 'blog',
        ];

        return $this->realCtx;
    }

    /**
     * 获取缓存站点类型
     */
    private function getSiteType(): array
    {
        $cached = get_transient('xzd_site_type_cache');
        if ($cached && is_array($cached)) return $cached;
        try {
            $detector = SiteTypeDetector::instance();
            $result = $detector->detect();
            set_transient('xzd_site_type_cache', $result, HOUR_IN_SECONDS);
            return $result;
        } catch (\Throwable $e) {
            return ['primary' => 'general', 'types' => ['general']];
        }
    }

    /**
     * 从知识库搜索相关内容
     */
    private function searchKnowledge(string $keyword, int $limit = 3): array
    {
        global $wpdb;
        $table = $wpdb->prefix . 'xzd_ai_knowledge';
        $like = '%' . $wpdb->esc_like($keyword) . '%';
        $rows = $wpdb->get_results($wpdb->prepare(
            "SELECT id, title, LEFT(content,200) AS excerpt FROM {$table}
             WHERE status='active' AND (title LIKE %s OR content LIKE %s)
             ORDER BY created_at DESC LIMIT %d",
            $like, $like, $limit
        ), ARRAY_A);
        return $rows ?: [];
    }

    /**
     * 获取真实相关文章
     */
    private function getRealRelatedPosts(int $postId, int $limit = 3): array
    {
        if (!$postId) return $this->getRecentPosts($limit);

        $posts = get_posts([
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'posts_per_page' => $limit,
            'post__not_in'   => [$postId],
            'orderby'        => 'rand',
        ]);

        if (empty($posts)) return [];

        return array_map(fn($p) => [
            'id'    => $p->ID,
            'title' => $p->post_title,
            'url'   => get_permalink($p->ID),
        ], $posts);
    }

    /**
     * 获取最近文章
     */
    private function getRecentPosts(int $limit = 3): array
    {
        $posts = get_posts([
            'post_type'      => 'post',
            'post_status'    => 'publish',
            'posts_per_page' => $limit,
            'orderby'        => 'date',
            'order'          => 'DESC',
        ]);
        return array_map(fn($p) => [
            'id'    => $p->ID,
            'title' => $p->post_title,
            'url'   => get_permalink($p->ID),
        ], $posts);
    }

    /**
     * 检查用户是否有真实未支付订单
     */
    private function userHasPendingOrder(int $userId): bool
    {
        if (!$userId) return false;
        try {
            $adapter = \XZD\AI\V5\Theme\AdapterRegistry::getCurrent();
            $orders = $adapter->getOrders($userId, 50);
            $pendingCount = count(array_filter($orders, fn($o) => !in_array($o['status'] ?? '', ['paid','cancelled','refunded'])));
            return $pendingCount > 0;
        } catch (\Throwable $e) {
            return false;
        }
    }

    /**
     * 获取知识库中的网站概况
     */
    private function getSiteOverview(): string
    {
        global $wpdb;
        $row = $wpdb->get_row(
            "SELECT content FROM {$wpdb->prefix}xzd_ai_knowledge WHERE title LIKE '%网站概况%' AND status='active' LIMIT 1",
            ARRAY_A
        );
        return $row['content'] ?? '';
    }

    // ═══════════════════════════════════════
    // 事件评估入口
    // ═══════════════════════════════════════

    public function evaluate(string $event, array $context): array
    {
        $ctx = $this->getRealContext();
        $context['_real_ctx'] = $ctx;

        return match ($event) {
            'stay_120s'        => $this->handleStay($context, $ctx),
            'scroll_depth_80'  => $this->handleScrollDepth($context, $ctx),
            'idle_60s'         => $this->handleIdle($context, $ctx),
            'exit_intent'      => $this->handleExitIntent($context, $ctx),
            'return_visitor'   => $this->handleReturnVisitor($context, $ctx),
            'multi_page_view'  => $this->handleMultiPage($context, $ctx),
            'search_no_result' => $this->handleSearchNoResult($context, $ctx),
            'product_browse_3' => $this->handleProductBrowse($context, $ctx),
            'cart_abandon'     => $this->handleCartAbandon($context, $ctx),
            'forum_browse_3'   => $this->handleForumBrowse($context, $ctx),
            'forum_no_post'    => $this->handleForumNoPost($context, $ctx),
            'article_reading'  => $this->handleArticleReading($context, $ctx),
            'first_visit'      => $this->handleFirstVisit($context, $ctx),
            default            => ['should_engage' => false, 'reason' => 'unknown_event'],
        };
    }

    // ═══════════════════════════════════════
    // 事件处理器
    // ═══════════════════════════════════════

    private function handleStay(array $ctx, array $real): array
    {
        $title = $ctx['page_title'] ?? '';
        $postId = $ctx['post_id'] ?? 0;
        $prefix = $title ? "看了这么久「{$title}」" : '看了这么久';

        // 有商品 + 在产品页 → 产品相关
        if ($real['product_count'] > 0 && ($ctx['page_type'] ?? '') === 'product') {
            return [
                'should_engage'    => true,
                'message'          => "{$prefix}，需要我帮你对比同类产品吗？",
                'cooldown_seconds' => HOUR_IN_SECONDS,
            ];
        }

        // 有文章 → 推荐相关文章
        if ($postId && $real['post_count'] > 0) {
            $related = $this->getRealRelatedPosts($postId, 2);
            $titles = implode('、', array_column($related, 'title'));
            return [
                'should_engage'    => true,
                'message'          => $titles ? "{$prefix}，推荐你阅读 👇" : "{$prefix}，需要我帮你总结这篇文章吗？",
                'suggestions'      => $related,
                'cooldown_seconds' => HOUR_IN_SECONDS,
            ];
        }

        return ['should_engage' => false, 'reason' => 'no_content'];
    }

    private function handleScrollDepth(array $ctx, array $real): array
    {
        $postId = $ctx['post_id'] ?? 0;
        $pageType = $ctx['page_type'] ?? '';

        // 产品页 + 有真实商品
        if ($real['product_count'] > 0 && $pageType === 'product') {
            $related = $this->getRealRelatedPosts($postId, 2);
            return [
                'should_engage'    => true,
                'message'          => '浏览了不少商品呢！需要我帮你做购买决策吗？',
                'suggestions'      => $related,
                'cooldown_seconds' => 1800,
            ];
        }

        // 论坛帖 + 有真实帖子
        if ($real['forum_count'] > 0 && $pageType === 'forum_post') {
            return [
                'should_engage'    => true,
                'message'          => '快看完了！觉得有用的话，可以点赞收藏或参与讨论哦',
                'cooldown_seconds' => 3600,
            ];
        }

        // 文章页 → 推荐真实相关文章
        if ($postId) {
            $related = $this->getRealRelatedPosts($postId, 3);
            if (!empty($related)) {
                $titles = array_map(fn($p) => "《{$p['title']}》", $related);
                return [
                    'should_engage'    => true,
                    'message'          => '快看完了！给你推荐几篇相关文章 👇',
                    'suggestions'      => $related,
                    'cooldown_seconds' => 3600,
                ];
            }
        }

        return [
            'should_engage'    => true,
            'message'          => '快看完了！有什么想了解的吗？试试问我吧~',
            'cooldown_seconds' => 3600,
        ];
    }

    private function handleIdle(array $ctx, array $real): array
    {
        if ($real['forum_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => '还在吗？有想讨论的话题可以发帖，或者看看热门帖子？',
                'cooldown_seconds' => 1800,
            ];
        }
        return [
            'should_engage'    => true,
            'message'          => '还在吗？有什么可以帮你的？试试搜索你感兴趣的内容~',
            'cooldown_seconds' => 1800,
        ];
    }

    private function handleExitIntent(array $ctx, array $real): array
    {
        $userId = $ctx['user_id'] ?? 0;

        // 只有登录用户 + 有真实商品 + 有未支付订单 → 购物车消息
        if ($real['product_count'] > 0 && $userId > 0 && $this->userHasPendingOrder($userId)) {
            return [
                'should_engage'    => true,
                'message'          => '购物车还有未结算的商品哦，现在下单享优惠！',
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        // 有商品但无未支付订单 → 商品推荐
        if ($real['product_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => '别急着走！看看我们的热门商品，也许有你喜欢的~',
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        // 有论坛实帖 → 论坛引导
        if ($real['forum_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => '别急着走！注册会员可以参与讨论，和大家一起交流~',
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        // 通用博客站 → 内容推荐
        $recentPosts = $this->getRecentPosts(2);
        if (!empty($recentPosts)) {
            $titles = implode('》、《', array_column($recentPosts, 'title'));
            return [
                'should_engage'    => true,
                'message'          => "别急着走！最近有这些精彩内容 👇 注册会员可获得更多AI功能~",
                'suggestions'      => $recentPosts,
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        return [
            'should_engage'    => true,
            'message'          => '别急着走！注册会员可获得更多AI功能，试试搜索你感兴趣的内容？',
            'cooldown_seconds' => DAY_IN_SECONDS,
        ];
    }

    private function handleReturnVisitor(array $ctx, array $real): array
    {
        $days = $ctx['days_since'] ?? 3;
        $newPosts = $this->countNewPosts($days);

        if ($real['product_count'] > 0 && !$ctx['user_id']) {
            return [
                'should_engage'    => true,
                'message'          => "欢迎回来！最近有新商品上架，快来挑挑看吧",
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        if ($real['forum_count'] > 0 && ($ctx['user_id'] ?? 0)) {
            return [
                'should_engage'    => true,
                'message'          => "欢迎回来！社区最近很热闹，快来看看新话题",
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        if ($newPosts > 0) {
            return [
                'should_engage'    => true,
                'message'          => "欢迎回来！这 {$days} 天发布了 {$newPosts} 篇新文章，快来查看吧",
                'cooldown_seconds' => DAY_IN_SECONDS,
            ];
        }

        return [
            'should_engage'    => true,
            'message'          => '欢迎回来！有什么可以帮你的吗？',
            'cooldown_seconds' => DAY_IN_SECONDS,
        ];
    }

    private function handleMultiPage(array $ctx, array $real): array
    {
        $count = $ctx['page_count'] ?? 5;

        if ($real['product_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => "浏览了 {$count} 件商品，需要我帮你对比一下吗？",
                'cooldown_seconds' => 1800,
            ];
        }

        return [
            'should_engage'    => true,
            'message'          => "浏览了 {$count} 个页面，需要我帮你整理一下今天看过的内容吗？",
            'cooldown_seconds' => 7200,
        ];
    }

    private function handleSearchNoResult(array $ctx, array $real): array
    {
        $query = $ctx['search_query'] ?? '';
        $q = $query ? "「{$query}」" : '';

        // 从知识库搜索相关内容
        $kbResults = $this->searchKnowledge($query, 3);
        if (!empty($kbResults)) {
            $titles = implode('、', array_map(fn($r) => "《{$r['title']}》", $kbResults));
            return [
                'should_engage'    => true,
                'message'          => $query
                    ? "没找到关于{$q}的内容，但知识库中有这些相关内容 👇"
                    : '没找到相关内容？试试这些 👇',
                'suggestions'      => array_map(function($r) { return ['id' => $r['id'], 'title' => $r['title'], 'url' => $r['source_url'] ?? '']; }, $kbResults),
                'cooldown_seconds' => 300,
            ];
        }

        // 知识库无结果，从真实文章搜索
        if ($query) {
            $posts = get_posts([
                's'              => $query,
                'post_type'      => 'post',
                'post_status'    => 'publish',
                'posts_per_page' => 3,
            ]);
            if (!empty($posts)) {
                $titles = implode('、', array_map(fn($p) => "《{$p->post_title}》", $posts));
                return [
                    'should_engage'    => true,
                    'message'          => "没找到精确匹配{$q}的内容，但你可能对这些感兴趣 👇",
                    'suggestions'      => array_map(fn($p) => ['id' => $p->ID, 'title' => $p->post_title, 'url' => get_permalink($p->ID)], $posts),
                    'cooldown_seconds' => 300,
                ];
            }
        }

        return [
            'should_engage'    => true,
            'message'          => $query
                ? "没找到关于{$q}的内容，我可以帮你生成相关内容！"
                : '没找到相关内容？我可以帮你生成！',
            'cooldown_seconds' => 300,
        ];
    }

    // ── 商城站专用事件 ──

    private function handleProductBrowse(array $ctx, array $real): array
    {
        // 必须有真实商品才触发
        if ($real['product_count'] === 0) {
            return ['should_engage' => false, 'reason' => 'no_products'];
        }
        return [
            'should_engage'    => true,
            'message'          => '看了3个同类产品了，需要我帮你做详细的对比分析吗？',
            'cooldown_seconds' => 1800,
        ];
    }

    private function handleCartAbandon(array $ctx, array $real): array
    {
        $userId = $ctx['user_id'] ?? 0;
        // 必须有真实商品 + 用户有未支付订单
        if ($real['product_count'] === 0) {
            return ['should_engage' => false, 'reason' => 'no_products'];
        }
        if (!$userId || !$this->userHasPendingOrder($userId)) {
            return ['should_engage' => false, 'reason' => 'no_pending_order'];
        }
        return [
            'should_engage'    => true,
            'message'          => '购物车里的宝贝在等你呢！现在下单享限时优惠~',
            'cooldown_seconds' => 7200,
        ];
    }

    // ── 论坛站专用事件 ──

    private function handleForumBrowse(array $ctx, array $real): array
    {
        if ($real['forum_count'] === 0) {
            return ['should_engage' => false, 'reason' => 'no_forum_posts'];
        }
        return [
            'should_engage'    => true,
            'message'          => '浏览了3个帖子还没有互动？发表你的看法，和大家一起讨论吧！',
            'cooldown_seconds' => 7200,
        ];
    }

    private function handleForumNoPost(array $ctx, array $real): array
    {
        if ($real['forum_count'] === 0) {
            return ['should_engage' => false, 'reason' => 'no_forum_posts'];
        }
        return [
            'should_engage'    => true,
            'message'          => '新来的朋友！去发个帖子介绍自己，或者看看新手引导吧~',
            'cooldown_seconds' => 0,
        ];
    }

    // ── 通用事件 ──

    private function handleArticleReading(array $ctx, array $real): array
    {
        $length = $ctx['content_length'] ?? 0;
        if ($length < 2000) {
            return ['should_engage' => false, 'reason' => 'short_article'];
        }
        return [
            'should_engage'    => true,
            'message'          => '这篇长文内容丰富，需要我帮你提取要点，或者回答相关问题吗？',
            'cooldown_seconds' => 3600,
        ];
    }

    private function handleFirstVisit(array $ctx, array $real): array
    {
        // 从知识库获取网站概况，生成真实欢迎语
        $overview = $this->getSiteOverview();
        $siteName = get_bloginfo('name');
        $siteDesc = get_bloginfo('description');

        // 有真实论坛
        if ($real['forum_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => "欢迎来到{$siteName}！我是AI助手，可以帮你找帖子、回答问题，试试问我吧~",
                'cooldown_seconds' => 0,
            ];
        }

        // 有真实商品
        if ($real['product_count'] > 0) {
            return [
                'should_engage'    => true,
                'message'          => "欢迎光临{$siteName}！我是AI导购，可以帮你找商品、比价格，有需要就问我~",
                'cooldown_seconds' => 0,
            ];
        }

        // 博客/通用站点
        $recentPosts = $this->getRecentPosts(2);
        if (!empty($recentPosts)) {
            $titles = implode('》、《', array_column($recentPosts, 'title'));
            return [
                'should_engage'    => true,
                'message'          => "欢迎来到{$siteName}！{$siteDesc}。我是AI助手，最近有这些精彩内容 👇",
                'suggestions'      => $recentPosts,
                'cooldown_seconds' => 0,
            ];
        }

        return [
            'should_engage'    => true,
            'message'          => "欢迎来到{$siteName}！我是AI助手，可以帮你搜索文章、生成内容，试试问我吧~",
            'cooldown_seconds' => 0,
        ];
    }

    // ═══════════════════════════════════════
    // AI 建议生成
    // ═══════════════════════════════════════

    public function generateSuggestions(): array
    {
        $suggestions = [];

        $aiAvailable = $this->isAiAvailable();
        $aiStatus = $this->getAiStatus();

        if (!$aiAvailable) {
            $suggestions[] = [
                'type'        => 'ai_disconnected',
                'priority'    => 'high',
                'title'       => 'AI 服务未连接',
                'description' => '请先在「AI配置」页面添加并测试AI连接。没有AI连接，标签生成、SEO优化、内容处理等功能无法使用。',
                'action'      => 'configure_ai',
                'requires_ai' => false,
                'metric'      => 0,
            ];
        }

        // 无标签文章
        $untagged = $this->countUntaggedPosts();
        if ($untagged > 0) {
            $suggestions[] = [
                'type'        => 'missing_tags',
                'priority'    => $untagged > 10 ? 'high' : 'medium',
                'title'       => "{$untagged} 篇文章缺少标签",
                'description' => $aiAvailable
                    ? "建议使用「批量标签生成」工具为这些文章补充标签，提升SEO和用户体验。"
                    : "文章缺少标签会影响SEO，但当前AI未连接，无法自动生成。请先配置AI连接。",
                'action'      => $aiAvailable ? 'generate_tags_batch' : 'configure_ai',
                'action_params' => ['count' => min($untagged, 50)],
                'requires_ai' => true,
                'metric'      => $untagged,
            ];
        }

        // 过期草稿
        $staleDrafts = $this->countStaleDrafts();
        if ($staleDrafts > 0) {
            $suggestions[] = [
                'type'        => 'stale_drafts',
                'priority'    => $staleDrafts > 5 ? 'medium' : 'low',
                'title'       => "{$staleDrafts} 篇草稿超过7天未处理",
                'description' => '建议清理过期草稿，发布有价值的内容或删除无效草稿。',
                'action'      => 'review_drafts',
                'requires_ai' => false,
                'metric'      => $staleDrafts,
            ];
        }

        // 无SEO描述
        $noSeoCount = $this->countPostsWithoutMeta('_xzd_ai_seo_desc');
        if ($noSeoCount > 3) {
            $suggestions[] = [
                'type'        => 'missing_seo',
                'priority'    => $noSeoCount > 20 ? 'high' : 'medium',
                'title'       => "{$noSeoCount} 篇文章缺少SEO描述",
                'description' => $aiAvailable ? 'SEO描述有助于搜索引擎排名，建议批量生成。' : '文章缺少SEO描述，但AI未连接无法自动生成。',
                'action'      => $aiAvailable ? 'generate_seo_batch' : 'configure_ai',
                'action_params' => ['count' => min($noSeoCount, 50)],
                'requires_ai' => true,
                'metric'      => $noSeoCount,
            ];
        }

        // 近期错误
        $recentErrors = $this->countRecentErrors();
        if ($recentErrors > 0) {
            $suggestions[] = [
                'type'        => 'recent_errors',
                'priority'    => $recentErrors > 5 ? 'high' : 'medium',
                'title'       => "近24小时有 {$recentErrors} 次操作失败",
                'description' => '建议查看操作日志中的失败记录，排查API连接或权限问题。',
                'action'      => 'view_error_logs',
                'requires_ai' => false,
                'metric'      => $recentErrors,
            ];
        }

        // 成本预警
        $monthCost = $this->getMonthCost();
        $budget = \XZD\AI\V5\Core\Config::instance()->get('cost.monthly_budget', 0);
        if ($budget > 0 && $monthCost > $budget * 0.8) {
            $pct = round($monthCost / $budget * 100);
            $suggestions[] = [
                'type'        => 'budget_warning',
                'priority'    => $monthCost > $budget ? 'high' : 'medium',
                'title'       => "本月费用 ¥{$monthCost}，已使用预算的 {$pct}%",
                'description' => '建议检查成本追踪面板，优化AI调用频率或更换更经济的模型。',
                'action'      => 'view_cost_analytics',
                'metric'      => $monthCost,
            ];
        }

        // 队列积压
        $queueBacklog = $this->countQueueBacklog();
        if ($queueBacklog > 10) {
            $suggestions[] = [
                'type'        => 'queue_backlog',
                'priority'    => $queueBacklog > 50 ? 'high' : 'medium',
                'title'       => "任务队列积压 {$queueBacklog} 条",
                'description' => '队列积压可能影响批量处理性能，建议清理或检查Action Scheduler状态。',
                'action'      => 'view_queue',
                'metric'      => $queueBacklog,
            ];
        }

        // 插件更新
        $pluginUpdates = $this->countPendingUpdates();
        if ($pluginUpdates > 0) {
            $suggestions[] = [
                'type'        => 'pending_updates',
                'priority'    => $pluginUpdates > 3 ? 'medium' : 'low',
                'title'       => "{$pluginUpdates} 个插件/主题有待更新",
                'description' => '保持插件更新有助于安全性和兼容性。',
                'action'      => 'view_updates',
                'metric'      => $pluginUpdates,
            ];
        }

        $order = ['high' => 0, 'medium' => 1, 'low' => 2];
        usort($suggestions, fn($a, $b) => $order[$a['priority']] - $order[$b['priority']]);

        return $suggestions;
    }

    public function getHealthSummary(): array
    {
        global $wpdb;

        $postCounts = wp_count_posts();
        $userCounts = count_users();

        return [
            'total_posts'       => (int) ($postCounts->publish ?? 0),
            'total_drafts'      => (int) ($postCounts->draft ?? 0),
            'total_pages'       => (int) ($postCounts->page ?? 0),
            'total_users'       => (int) ($userCounts['total_users'] ?? 0),
            'untagged_posts'    => $this->countUntaggedPosts(),
            'stale_drafts'      => $this->countStaleDrafts(),
            'no_seo_posts'      => $this->countPostsWithoutMeta('_xzd_ai_seo_desc'),
            'no_cover_posts'    => $this->countPostsWithoutThumbnail(),
            'recent_errors_24h' => $this->countRecentErrors(),
            'queue_backlog'     => $this->countQueueBacklog(),
            'month_cost_rmb'    => $this->getMonthCost(),
            'total_operations'  => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}xzd_ai_operation_log"),
            'active_chats'      => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}xzd_ai_chat_sessions WHERE status = 'active'"),
            'total_connections' => count(array_filter(
                \XZD\AI\V5\Core\Config::instance()->get('connections.text', []),
                fn($c) => !empty($c['api_key'])
            )),
        ];
    }

    public function runFullScan(): array
    {
        $health = $this->getHealthSummary();
        $suggestions = $this->generateSuggestions();

        $score = 100;
        $issues = [];

        if ($health['untagged_posts'] > 10) { $score -= 10; $issues[] = '多篇文章缺少标签'; }
        if ($health['stale_drafts'] > 5) { $score -= 5; $issues[] = '存在过期草稿'; }
        if ($health['no_seo_posts'] > 10) { $score -= 10; $issues[] = '多篇文章缺少SEO描述'; }
        if ($health['recent_errors_24h'] > 5) { $score -= 15; $issues[] = '近期错误率较高'; }
        if ($health['queue_backlog'] > 50) { $score -= 10; $issues[] = '任务队列积压'; }
        if ($health['total_connections'] === 0) { $score -= 20; $issues[] = '未配置AI连接'; }
        if ($health['month_cost_rmb'] > 0) {
            $budget = \XZD\AI\V5\Core\Config::instance()->get('cost.monthly_budget', 0);
            if ($budget > 0 && $health['month_cost_rmb'] > $budget * 0.9) { $score -= 10; $issues[] = '接近月度预算上限'; }
        }

        $score = max(0, $score);

        return [
            'score'        => $score,
            'grade'        => $score >= 90 ? 'A' : ($score >= 70 ? 'B' : ($score >= 50 ? 'C' : 'D')),
            'issues'       => $issues,
            'health'       => $health,
            'suggestions'  => $suggestions,
            'scanned_at'   => current_time('mysql'),
        ];
    }

    // ═══════════════════════════════════════
    // 数据库查询辅助
    // ═══════════════════════════════════════

    private function isAiAvailable(): bool
    {
        $connections = \XZD\AI\V5\Core\Config::instance()->get('connections.text', []);
        foreach ($connections as $conn) {
            if (!empty($conn['api_key']) && ($conn['is_enabled'] ?? true)) {
                return true;
            }
        }
        return false;
    }

    private function getAiStatus(): array
    {
        $connections = \XZD\AI\V5\Core\Config::instance()->get('connections.text', []);
        $total = count($connections);
        $enabled = 0;
        $withKey = 0;
        foreach ($connections as $conn) {
            if ($conn['is_enabled'] ?? true) $enabled++;
            if (!empty($conn['api_key'])) $withKey++;
        }
        return [
            'total'   => $total,
            'enabled' => $enabled,
            'with_key' => $withKey,
            'available' => $enabled > 0 && $withKey > 0,
        ];
    }

    private function countUntaggedPosts(): int
    {
        global $wpdb;
        return (int) $wpdb->get_var(
            "SELECT COUNT(DISTINCT p.ID) FROM {$wpdb->posts} p
             LEFT JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
             LEFT JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag'
             WHERE p.post_type = 'post' AND p.post_status = 'publish'
             AND tt.term_taxonomy_id IS NULL"
        );
    }

    private function countStaleDrafts(): int
    {
        global $wpdb;
        return (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->posts}
             WHERE post_type = 'post' AND post_status = 'draft'
             AND post_modified < %s",
            date('Y-m-d H:i:s', strtotime('-7 days'))
        ));
    }

    private function countPostsWithoutMeta(string $metaKey): int
    {
        global $wpdb;
        return (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->posts} p
             WHERE p.post_type = 'post' AND p.post_status = 'publish'
             AND NOT EXISTS (
                 SELECT 1 FROM {$wpdb->postmeta} pm
                 WHERE pm.post_id = p.ID AND pm.meta_key = %s
             )",
            $metaKey
        ));
    }

    private function countPostsWithoutThumbnail(): int
    {
        global $wpdb;
        return (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->posts} p
             WHERE p.post_type = 'post' AND p.post_status = 'publish'
             AND NOT EXISTS (
                 SELECT 1 FROM {$wpdb->postmeta} pm
                 WHERE pm.post_id = p.ID AND pm.meta_key = '_thumbnail_id'
             )"
        );
    }

    private function countRecentErrors(): int
    {
        global $wpdb;
        return (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->prefix}xzd_ai_operation_log
             WHERE status != 'success' AND created_at > %s",
            date('Y-m-d H:i:s', strtotime('-24 hours'))
        ));
    }

    private function getMonthCost(): float
    {
        global $wpdb;
        return (float) $wpdb->get_var($wpdb->prepare(
            "SELECT COALESCE(SUM(cost_rmb), 0) FROM {$wpdb->prefix}xzd_ai_cost_log
             WHERE created_at >= %s",
            date('Y-m-01')
        ));
    }

    private function countQueueBacklog(): int
    {
        global $wpdb;
        return (int) $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->prefix}xzd_ai_queue WHERE status = 'pending'"
        );
    }

    private function countPendingUpdates(): int
    {
        if (!function_exists('get_plugin_updates') || !function_exists('get_theme_updates')) {
            require_once ABSPATH . 'wp-admin/includes/update.php';
        }
        $pluginUpdates = count(get_plugin_updates());
        $themeUpdates = count(get_theme_updates());
        $coreUpdates = get_core_updates();
        $coreNeedsUpdate = 0;
        if (!empty($coreUpdates) && isset($coreUpdates[0]->response) && $coreUpdates[0]->response !== 'latest') {
            $coreNeedsUpdate = 1;
        }
        return $pluginUpdates + $themeUpdates + $coreNeedsUpdate;
    }

    private function countNewPosts(int $days): int
    {
        global $wpdb;
        return (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM {$wpdb->posts}
             WHERE post_type = 'post' AND post_status = 'publish'
             AND post_date > %s",
            date('Y-m-d H:i:s', strtotime("-{$days} days"))
        ));
    }
}
