from __future__ import annotations

from app.core.agent.utils.process_v2.models import V2Domain, V2Intent, V2RouteAnchors, V2RouteResult, V2Subintent
from app.core.agent.utils.process_v2.plan_resolver import V2RetrievalPolicyResolver


def _route(
    *,
    intent: str = V2Intent.DOC_EXPLAIN,
    subintent: str = V2Subintent.SUMMARY,
    entity_names: list[str] | None = None,
    file_names: list[str] | None = None,
    endpoint_paths: list[str] | None = None,
    target_doc_hints: list[str] | None = None,
    matched_aliases: list[str] | None = None,
    process_domain: str | None = None,
    process_subdomain: str | None = None,
) -> V2RouteResult:
    return V2RouteResult(
        routing_domain=V2Domain.DOCS if intent == V2Intent.DOC_EXPLAIN else V2Domain.GENERAL,
        intent=intent,
        subintent=subintent,
        user_query="q",
        normalized_query="q",
        anchors=V2RouteAnchors(
            entity_names=entity_names or [],
            file_names=file_names or [],
            endpoint_paths=endpoint_paths or [],
            target_doc_hints=target_doc_hints or [],
            matched_aliases=matched_aliases or [],
            process_domain=process_domain,
            process_subdomain=process_subdomain,
        ),
    )


def test_policy_maps_api_summary_to_fact_layers() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            endpoint_paths=["/health"],
            target_doc_hints=["docs/api/health-endpoint.md"],
        )
    )

    assert plan.profile == "docs_api_method_explain"
    assert plan.layers == ["D1_DOCUMENT_CATALOG", "D2_FACT_INDEX", "D0_DOC_CHUNKS"]
    assert plan.filters["path_prefixes"] == [
        "docs/api/",
        "docs/endpoints/",
        "docs/methods/",
        "api/",
        "endpoints/",
        "methods/",
    ]
    assert plan.filters["target_doc_hints"] == ["docs/api/health-endpoint.md"]


def test_policy_maps_logic_summary_to_workflow_layers_and_metadata_filters() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            matched_aliases=["logic flow"],
            process_domain="notifications",
            process_subdomain="delivery_loop",
        )
    )

    assert plan.profile == "docs_summary_logic_flow"
    assert plan.layers == ["D4_WORKFLOW_INDEX", "D1_DOCUMENT_CATALOG", "D0_DOC_CHUNKS"]
    assert plan.filters["metadata.domain"] == "notifications"
    assert plan.filters["metadata.subdomain"] == "delivery_loop"
    assert plan.filters["prefer_path_prefixes"][0] == "docs/logic/"


def test_policy_maps_entity_summary_to_entity_layers() -> None:
    plan = V2RetrievalPolicyResolver().resolve(_route(entity_names=["RuntimeManager"]))

    assert plan.profile == "docs_summary_domain_entity"
    assert plan.layers == ["D3_ENTITY_CATALOG", "D1_DOCUMENT_CATALOG", "D0_DOC_CHUNKS"]
    assert "%runtimemanager%" in plan.filters["prefer_like_patterns"]


def test_policy_keeps_api_method_profile_even_with_additional_entity_signal() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            endpoint_paths=["/health"],
            entity_names=["RuntimeManager"],
        )
    )

    assert plan.profile == "docs_api_method_explain"
    assert plan.layers == ["D1_DOCUMENT_CATALOG", "D2_FACT_INDEX", "D0_DOC_CHUNKS"]


def test_policy_uses_api_method_profile_for_endpoint_like_hints_without_explicit_path() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            target_doc_hints=["health", "/health", "health endpoint"],
        )
    )

    assert plan.profile == "docs_api_method_explain"
    assert "%health%" in plan.filters["prefer_like_patterns"]


def test_policy_uses_hard_and_soft_filters_for_find_files() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            subintent=V2Subintent.FIND_FILES,
            file_names=["docs/workflows/manual-send.md"],
            entity_names=["ManualSendWorker"],
            matched_aliases=["manual send"],
            process_domain="messaging",
            process_subdomain="manual_send",
        )
    )

    assert plan.profile == "file_lookup"
    assert plan.layers == ["D1_DOCUMENT_CATALOG", "D3_ENTITY_CATALOG"]
    assert plan.filters["path_prefixes"] == ["docs/workflows/"]
    assert plan.filters["metadata.domain"] == "messaging"
    assert "%manualsendworker%" in plan.filters["prefer_like_patterns"]


def test_policy_keeps_general_routes_in_general_profile() -> None:
    plan = V2RetrievalPolicyResolver().resolve(
        _route(
            intent=V2Intent.GENERAL_QA,
            endpoint_paths=["/health"],
            target_doc_hints=["docs/api/health-endpoint.md"],
        )
    )

    assert plan.profile == "general_qa_grounded_summary"
    assert plan.layers == ["D1_DOCUMENT_CATALOG", "D0_DOC_CHUNKS"]
    assert "path_prefixes" not in plan.filters
