import pytest

from app.intent.intent_analyzer import IntentAnalyzer
from app.models.intent import QueryIntent
from app.retrieval.query_rewriter import apply_query_rewrite


@pytest.fixture
def analyzer():
    return IntentAnalyzer()


def test_code_lookup_intent(analyzer):
    result = analyzer.analyze("Where is ProcessDelta defined?")
    assert result.type == QueryIntent.CODE_LOOKUP
    assert result.confidence >= 0.8
    assert "code" in [st.value for st in result.source_types_used]


def test_historical_intent(analyzer):
    result = analyzer.analyze("Why was the Redis consumer refactored?")
    assert result.type == QueryIntent.HISTORICAL
    assert "commit" in [st.value for st in result.source_types_used]


def test_documentation_intent(analyzer):
    result = analyzer.analyze("How to configure the docs ingestion service?")
    assert result.type == QueryIntent.DOCUMENTATION


def test_architecture_intent(analyzer):
    result = analyzer.analyze("Explain the indexing pipeline architecture flow")
    assert result.type in (QueryIntent.ARCHITECTURE, QueryIntent.MIXED)


def test_general_intent(analyzer):
    result = analyzer.analyze("hello")
    assert result.type == QueryIntent.GENERAL
    assert result.confidence == 0.5


def test_empty_question_defaults_to_general(analyzer):
    result = analyzer.analyze("   ")
    assert result.type == QueryIntent.GENERAL


def test_architecture_intent_for_functionality_question(analyzer):
    result = analyzer.analyze("What are the main features and functionality of this repo?")
    assert result.type == QueryIntent.ARCHITECTURE
    assert result.confidence >= 0.8


def test_apply_query_rewrite_boosts_overview_questions():
    intent = IntentAnalyzer().analyze("What are the functionalities of this repository?")
    rewritten = apply_query_rewrite(intent, "What are the functionalities of this repository?")
    assert "README overview architecture" in rewritten
