from __future__ import annotations

from router.query_classifier import classify_query_for_route
from router.route_selector import choose_route
from router.store_sufficiency import check_store_sufficiency


def _metadata() -> dict:
    return {
        "freshness_threshold_hours": 6.0,
        "refresh_age_hours": 1.0,
        "feature_age_hours": 1.0,
        "risk_age_hours": 1.0,
        "feature_ready": True,
        "risk_ready": True,
        "ranking_ready": False,
        "aggregate_ready": True,
        "precomputed_aggregates": [
            "baseline_period_summary",
            "period_trend_summary",
        ],
        "canonical_semantic_coverage": 0.95,
        "canonical_relation_types": 25,
        "canonical_links_total": 2000,
        "canonical_entities_total": 400,
    }


def _route(question_class: str, question_text: str) -> str:
    meta = _metadata()
    flags = classify_query_for_route(question_text, {"question_class": question_class}, meta)
    suff = check_store_sufficiency(flags, meta)
    parsed_as_trend_or_risk = question_class in {"period_trend", "anomaly_control", "ambiguous_fuzzy"} or (
        question_class == "heavy_analytical" and "baseline" in question_text.lower()
    )
    selection = choose_route(flags, suff, parsed_as_trend_or_risk=parsed_as_trend_or_risk)
    return selection.chosen_route


def test_router_subset_q06_to_q12() -> None:
    cases = [
        ("drilldown_explain", "Объясни сальдо через движения.", "hybrid_store_plus_live"),
        ("drilldown_explain", "Почему проводка на этот счет?", "live_mcp_drilldown"),
        ("drilldown_explain", "Цепочка документ -> проводки -> субконто.", "live_mcp_drilldown"),
        ("drilldown_explain", "Источник регистра для строки движения.", "live_mcp_drilldown"),
        ("drilldown_explain", "Почему выбрано это субконто3?", "live_mcp_drilldown"),
        ("cross_entity", "Свяжи документы покупателей и проводки.", "hybrid_store_plus_live"),
        ("cross_entity", "Свяжи контрагентов, договоры и проводки.", "hybrid_store_plus_live"),
    ]
    for question_class, text, expected_route in cases:
        assert _route(question_class, text) == expected_route


def test_router_subset_q26_to_q30() -> None:
    cases = [
        ("heavy_analytical", "Полный риск-срез за июнь.", "batch_refresh_then_store"),
        ("heavy_analytical", "Рейтинг риск-счетов.", "batch_refresh_then_store"),
        ("heavy_analytical", "Рейтинг риск-контрагентов.", "batch_refresh_then_store"),
        ("heavy_analytical", "Baseline closed/open periods.", "store_feature_risk"),
        ("heavy_analytical", "Company anomaly summary.", "batch_refresh_then_store"),
    ]
    for question_class, text, expected_route in cases:
        assert _route(question_class, text) == expected_route


def test_router_period_trend_anomaly_stays_feature_store() -> None:
    assert _route("period_trend", "Аномальный рост расходных операций?") == "store_feature_risk"
