from pathlib import Path

from agent_framework.routing.config_loader import load_intents
from agent_framework.routing.enterprise_router import EnterpriseRouter


ROOT = Path(__file__).resolve().parents[2]


def _router_for_config() -> EnterpriseRouter:
    router = EnterpriseRouter.__new__(EnterpriseRouter)
    router.intents = load_intents(ROOT / "config" / "routing.yaml")
    return router


def test_non_recognition_without_transactional_action_routes_to_invoice_explanation():
    router = _router_for_config()
    decision = router._route_by_keyword(
        "contratamos o plano de duzentos e nove, mas eu não estou reconhecendo "
        "itens eventuais sessenta e seis e noventa e seis"
    )
    assert decision is not None
    assert decision.intent == "contas_invoice_explanation"
    assert decision.agent == "faturas_agent"
    assert decision.mcp_tools == ["invoice_explanation"]


def test_explicit_contestation_wins_over_non_recognition_language():
    router = _router_for_config()
    decision = router._route_by_keyword(
        "não reconheço essa cobrança e quero contestar agora"
    )
    assert decision is not None
    assert decision.intent == "contas_contestation"
    assert decision.agent == "contestacao_agent"
    assert "contestar_cobranca" in decision.mcp_tools


def test_contestation_intent_supports_explicit_action_and_contextual_reentry():
    intents = {item.name: item for item in load_intents(ROOT / "config" / "routing.yaml")}
    contest = intents["contas_contestation"]
    explanation = intents["contas_invoice_explanation"]

    assert "reentrada contextual" in contest.description
    assert "não reconheço essa cobrança" not in contest.keywords
    assert "não estou reconhecendo" in explanation.keywords
    assert explanation.examples[0].startswith("Contratamos um plano")
    assert "Contexto anterior" in contest.examples[0]


def test_mig0003_causal_bill_increase_routes_to_invoice_explanation():
    router = _router_for_config()
    decision = router._route_by_keyword("por que minha conta subiu esse mês?")
    assert decision is not None
    assert decision.intent == "contas_invoice_explanation"
    assert decision.agent == "faturas_agent"
    assert decision.mcp_tools == ["invoice_explanation"]


def test_generic_invoice_lookup_still_routes_to_invoice_query():
    router = _router_for_config()
    decision = router._route_by_keyword("quero minha fatura")
    assert decision is not None
    assert decision.intent == "contas_invoice_query"
    assert decision.mcp_tools == ["consultar_faturas"]


def test_invoice_query_does_not_keep_ambiguous_minha_conta_keyword():
    intents = {item.name: item for item in load_intents(ROOT / "config" / "routing.yaml")}
    invoice_query = intents["contas_invoice_query"]
    explanation = intents["contas_invoice_explanation"]
    assert "minha conta" not in invoice_query.keywords
    assert "minha conta subiu" in explanation.keywords
    assert explanation.priority > invoice_query.priority
