from __future__ import annotations

from app.core.agent.utils.process_v2.evidence.assembler import DocsEvidenceAssembler
from app.core.agent.utils.process_v2.models import V2Domain, V2Intent, V2RouteAnchors, V2RouteResult, V2Subintent


def _route(*, hints: list[str], terms: list[str], subintent: str = V2Subintent.SUMMARY) -> V2RouteResult:
    return V2RouteResult(
        routing_domain=V2Domain.DOCS,
        intent=V2Intent.DOC_EXPLAIN,
        subintent=subintent,
        user_query="q",
        normalized_query="q",
        target_terms=terms,
        anchors=V2RouteAnchors(target_doc_hints=hints, endpoint_paths=["/health"] if "/health" in terms else []),
    )


def test_target_doc_hint_is_hard_boosted_over_readme() -> None:
    rows = [
        {
            "path": "docs/README.md",
            "title": "README",
            "content": "",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"summary_text": "index", "document_id": "docs.readme"},
        },
        {
            "path": "docs/api/health-endpoint.md",
            "title": "Health endpoint",
            "content": "",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"summary_text": "health summary", "document_id": "api.health"},
        },
    ]
    route = _route(
        hints=["docs/api/health-endpoint.md"],
        terms=["/health", "health"],
    )

    docs = DocsEvidenceAssembler().assemble_summaries(rows, route)

    assert docs[0].path == "docs/api/health-endpoint.md"
    assert docs[0].score_breakdown["target_doc_boost"] >= 1000


def test_find_files_prefers_exact_path_match() -> None:
    rows = [
        {
            "path": "docs/architecture/telegram-notify-app-overview.md",
            "title": "Overview",
            "content": "overview",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"document_id": "arch"},
        },
        {
            "path": "docs/domains/runtime-health-entity.md",
            "title": "Runtime health",
            "content": "runtime health",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"document_id": "domain.runtime"},
        },
    ]
    route = V2RouteResult(
        routing_domain=V2Domain.DOCS,
        intent=V2Intent.DOC_EXPLAIN,
        subintent=V2Subintent.FIND_FILES,
        user_query="Где находится runtime health?",
        normalized_query="Где находится runtime health?",
        target_terms=["runtime", "health"],
        anchors=V2RouteAnchors(
            target_doc_hints=["docs/domains/runtime-health-entity.md"],
            matched_aliases=["runtime-health-entity"],
        ),
    )

    files = DocsEvidenceAssembler().assemble_files(rows, route)

    assert files[0].path == "docs/domains/runtime-health-entity.md"
    assert files[0].match_reason in {"exact_path", "alias_match"}


def test_summary_ranking_penalizes_overview_doc_when_specific_api_doc_exists() -> None:
    rows = [
        {
            "path": "docs/overview/health-overview.md",
            "title": "Health overview",
            "content": "",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"summary_text": "Navigation page with related docs.", "document_id": "docs.health_overview"},
        },
        {
            "path": "docs/api/health-endpoint.md",
            "title": "Health endpoint",
            "content": "",
            "layer": "D1_DOCUMENT_CATALOG",
            "metadata": {"summary_text": "GET /health returns runtime status.", "document_id": "api.health"},
        },
    ]
    route = _route(
        hints=["health", "/health", "health endpoint"],
        terms=["health"],
    )

    docs = DocsEvidenceAssembler().assemble_summaries(rows, route)

    assert docs[0].path == "docs/api/health-endpoint.md"
    assert docs[0].score_breakdown["specificity_boost"] > docs[1].score_breakdown["specificity_boost"]
    assert docs[1].score_breakdown["generic_penalty"] < 0
