from __future__ import annotations

from app.core.agent.processes.v2.intent_router.modules.anchors import V2AnchorExtractor
from app.core.agent.processes.v2.intent_router.modules.target_terms import V2TargetTermsExtractor


def test_target_terms_keeps_only_endpoint_for_short_explain_query() -> None:
    analysis = V2TargetTermsExtractor().extract("Кратко объясни как работает /health")

    assert analysis.target_terms == ["/health"]


def test_target_terms_keeps_domain_terms_without_question_words() -> None:
    analysis = V2TargetTermsExtractor().extract("Что делает runtime health")

    assert analysis.target_terms == ["runtime", "health"]


def test_target_terms_keeps_only_endpoint_for_file_marker_query() -> None:
    analysis = V2TargetTermsExtractor().extract("В каком файле описан /health?")

    assert analysis.target_terms == ["/health"]


def test_target_terms_drops_noisy_filler_words() -> None:
    analysis = V2TargetTermsExtractor().extract("Где там дока про health, покажи плз")

    assert analysis.target_terms == ["health"]


def test_target_terms_keeps_api_and_explicit_endpoint() -> None:
    analysis = V2TargetTermsExtractor().extract("Нужен краткий док-саммари по api /send")

    assert analysis.target_terms == ["/send", "api"]


def test_target_terms_keeps_identifier_like_token() -> None:
    analysis = V2TargetTermsExtractor().extract("Что делает telegram_notify?")

    assert analysis.target_terms == ["telegram_notify"]


def test_target_terms_rejects_broken_path_like_token() -> None:
    analysis = V2TargetTermsExtractor().extract("Опиши /actions/{action")

    assert analysis.target_terms == []


def test_file_names_accepts_real_doc_path() -> None:
    terms = V2TargetTermsExtractor().extract("docs/api/health.md")
    anchors = V2AnchorExtractor().extract("docs/api/health.md", terms).anchors

    assert anchors.file_names == ["docs/api/health.md"]
    assert anchors.endpoint_paths == []


def test_file_names_rejects_endpoint_path() -> None:
    terms = V2TargetTermsExtractor().extract("/health")
    anchors = V2AnchorExtractor().extract("/health", terms).anchors

    assert anchors.file_names == []


def test_target_terms_drop_noisy_english_file_words() -> None:
    analysis = V2TargetTermsExtractor().extract("pls show doc for /health")

    assert analysis.target_terms == ["/health"]


def test_doc_path_does_not_become_endpoint_path() -> None:
    analysis = V2TargetTermsExtractor().extract("docs/api/health-endpoint.md")

    assert analysis.endpoint_paths == []


def test_target_terms_drop_architecture_marker_words() -> None:
    analysis = V2TargetTermsExtractor().extract("Объясни architecture overview сервиса уведомлений")

    assert "объясни" not in analysis.target_terms
    assert "architecture" not in analysis.target_terms
    assert "overview" not in analysis.target_terms


def test_anchor_extractor_extracts_process_domain_and_subdomain() -> None:
    terms = V2TargetTermsExtractor().extract("Объясни billing invoice process")
    anchors = V2AnchorExtractor().extract("Объясни billing invoice process", terms).anchors

    assert anchors.process_domain == "billing"
    assert anchors.process_subdomain == "invoice"


def test_file_names_rejects_identifier_like_token() -> None:
    terms = V2TargetTermsExtractor().extract("telegram_notify")
    anchors = V2AnchorExtractor().extract("telegram_notify", terms).anchors

    assert anchors.file_names == []


def test_target_terms_extracts_api_like_anchor_from_method_query() -> None:
    analysis = V2TargetTermsExtractor().extract("Как работает метод health?")

    assert analysis.target_terms == ["/health", "health"]
    assert analysis.endpoint_paths == ["/health"]
    assert analysis.api_like_terms == ["health"]


def test_anchor_extractor_builds_endpoint_hints_for_short_api_like_query() -> None:
    terms = V2TargetTermsExtractor().extract("Что делает health?")
    anchors = V2AnchorExtractor().extract("Что делает health?", terms).anchors

    assert anchors.endpoint_paths == []
    assert "health" in anchors.target_doc_hints
    assert "/health" in anchors.target_doc_hints
    assert "health endpoint" in anchors.target_doc_hints


def test_anchor_extractor_keeps_templated_endpoint_for_docs_query() -> None:
    terms = V2TargetTermsExtractor().extract("Расскажи про endpoint /users/{id}")
    anchors = V2AnchorExtractor().extract("Расскажи про endpoint /users/{id}", terms).anchors

    assert anchors.endpoint_paths == ["/users/{id}"]
    assert "/users/{id}" in anchors.target_doc_hints
    assert "users endpoint" in anchors.target_doc_hints
