"""Tests for the rule-based classifier."""

import pytest

from ai_checkup.models import Conversation, Message
from ai_checkup.classifier import (
    classify_question_level,
    detect_judgment,
    detect_deep_thinking,
    detect_narrative,
    detect_output,
    classify_conversation,
    classify_all,
)


class TestQuestionLevel:
    def test_l1_how(self):
        assert classify_question_level("帮我写一个Python脚本") == "L1_How"
        assert classify_question_level("如何实现用户登录") == "L1_How"
        assert classify_question_level("修复这个bug") == "L1_How"

    def test_l2_what(self):
        assert classify_question_level("什么是微服务架构") == "L2_What"
        assert classify_question_level("对比React和Vue") == "L2_What"
        assert classify_question_level("分析一下性能问题") == "L2_What"

    def test_l3_why(self):
        assert classify_question_level("为什么系统总是崩溃") == "L3_Why"
        assert classify_question_level("本质原因是什么") == "L3_Why"
        assert classify_question_level("根本问题在哪") == "L3_Why"

    def test_l4_meta(self):
        assert classify_question_level("复盘一下上月的决策") == "L4_Meta"
        assert classify_question_level("设计一个方法论框架") == "L4_Meta"
        assert classify_question_level("反思这次项目失败") == "L4_Meta"

    def test_default_to_l1(self):
        assert classify_question_level("你好") == "L1_How"
        assert classify_question_level("谢谢") == "L1_How"

    def test_priority_higher_wins(self):
        # L4 should win over L1
        assert classify_question_level("复盘一下，帮我写总结") == "L4_Meta"
        # L3 should win over L2
        assert classify_question_level("为什么要分析这个") == "L3_Why"


class TestJudgment:
    def test_direction_setting(self):
        has, types = detect_judgment("我觉得应该用React")
        assert has
        assert "direction_setting" in types

    def test_evaluation(self):
        has, types = detect_judgment("哪个框架好？")
        assert has
        assert "evaluation" in types

    def test_rejection(self):
        has, types = detect_judgment("不对，这样不行")
        assert has
        assert "rejection" in types

    def test_no_judgment(self):
        has, types = detect_judgment("帮我写一个脚本")
        assert not has
        assert len(types) == 0


class TestDeepThinking:
    def test_reflection(self):
        assert detect_deep_thinking("复盘一下这次的经验")
        assert detect_deep_thinking("反思项目为什么失败")
        assert detect_deep_thinking("总结经验教训")

    def test_no_deep(self):
        assert not detect_deep_thinking("帮我写代码")


class TestNarrative:
    def test_writing(self):
        assert detect_narrative("写一篇公众号文章")
        assert detect_narrative("把方案写成说明文档")

    def test_no_narrative(self):
        assert not detect_narrative("帮我分析数据")


class TestOutput:
    def test_output_keywords(self):
        assert detect_output("帮我部署应用")
        assert detect_output("创建用户管理模块")
        assert detect_output("实现登录功能")

    def test_no_output(self):
        assert not detect_output("为什么系统崩溃")


class TestClassifyConversation:
    def test_full_classification(self):
        conv = Conversation(
            id="test-1",
            messages=[
                Message(role="user", content="复盘一下上月决策，我觉得应该改方向"),
                Message(role="assistant", content="OK"),
            ],
            platform="test",
        )
        result = classify_conversation(conv)
        assert result["question_level"] == "L4_Meta"
        assert result["has_judgment"]
        assert result["has_deep_thinking"]

    def test_classify_all(self, sample_conversations):
        results = classify_all(sample_conversations)
        assert len(results) == len(sample_conversations)
        for conv in sample_conversations:
            assert conv.id in results
