from __future__ import annotations

from router.query_classifier import RouteDecisionFlags
from router.store_sufficiency import check_store_sufficiency


def _base_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=True,
        precomputed_aggregate_available=True,
    )


def test_store_sufficiency_positive_case() -> None:
    flags = _base_flags()
    metadata = {
        "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": True,
        "aggregate_ready": True,
        "canonical_semantic_coverage": 0.95,
        "canonical_relation_types": 30,
        "canonical_links_total": 1000,
        "canonical_entities_total": 500,
    }
    result = check_store_sufficiency(flags, metadata)
    assert result.canonical_sufficient is True
    assert result.freshness_ok is True
    assert result.reason_codes == []


def test_store_sufficiency_heavy_ranking_not_ready() -> None:
    flags = _base_flags()
    flags.needs_full_period_aggregation = True
    flags.needs_ranking = True
    flags.precomputed_aggregate_available = False
    flags.freshness_sensitive = True
    metadata = {
        "freshness_threshold_hours": 6.0,
        "refresh_age_hours": 12.0,
        "feature_age_hours": 10.0,
        "risk_age_hours": 10.0,
        "feature_ready": True,
        "risk_ready": True,
        "ranking_ready": False,
        "aggregate_ready": True,
        "canonical_semantic_coverage": 0.95,
        "canonical_relation_types": 30,
        "canonical_links_total": 1000,
        "canonical_entities_total": 500,
    }
    result = check_store_sufficiency(flags, metadata)
    assert result.freshness_ok is False
    assert result.aggregate_level_ok is False
    assert result.ranking_ready is False
    assert "aggregate_not_sufficient" in result.reason_codes
    assert "ranking_not_ready" in result.reason_codes

