from __future__ import annotations

from router.query_classifier import RouteDecisionFlags
from router.route_selector import choose_route
from router.store_sufficiency import StoreSufficiencyResult


def _flags() -> RouteDecisionFlags:
    return RouteDecisionFlags(
        needs_exact_object_trace=False,
        needs_causal_chain=False,
        needs_cross_entity_join=False,
        needs_full_period_aggregation=False,
        needs_ranking=False,
        needs_anomaly_summary=False,
        needs_runtime_truth=False,
        freshness_sensitive=False,
        ambiguous_object_scope=False,
        store_sufficiency_confident=False,
        precomputed_aggregate_available=True,
    )


def _suff() -> StoreSufficiencyResult:
    return StoreSufficiencyResult(
        canonical_sufficient=True,
        feature_sufficient=True,
        risk_sufficient=True,
        freshness_ok=True,
        aggregate_level_ok=True,
        ranking_ready=True,
        explanation_ready=True,
        reason_codes=[],
    )


def test_route_guard_exact_object_trace_to_live() -> None:
    flags = _flags()
    flags.needs_exact_object_trace = True
    result = choose_route(flags, _suff(), parsed_as_trend_or_risk=False)
    assert result.chosen_route == "live_mcp_drilldown"


def test_route_guard_heavy_without_aggregate_to_batch() -> None:
    flags = _flags()
    flags.needs_full_period_aggregation = True
    flags.precomputed_aggregate_available = False
    suff = _suff()
    suff.aggregate_level_ok = False
    result = choose_route(flags, suff, parsed_as_trend_or_risk=False)
    assert result.chosen_route == "batch_refresh_then_store"


def test_route_guard_cross_entity_causal_to_hybrid() -> None:
    flags = _flags()
    flags.needs_cross_entity_join = True
    flags.needs_causal_chain = True
    suff = _suff()
    suff.explanation_ready = False
    result = choose_route(flags, suff, parsed_as_trend_or_risk=False)
    assert result.chosen_route == "hybrid_store_plus_live"


def test_route_guard_simple_factual_to_store_canonical() -> None:
    result = choose_route(_flags(), _suff(), parsed_as_trend_or_risk=False)
    assert result.chosen_route == "store_canonical"


def test_route_guard_trend_to_store_feature_risk() -> None:
    result = choose_route(_flags(), _suff(), parsed_as_trend_or_risk=True)
    assert result.chosen_route == "store_feature_risk"

