from backtesting import engine as backtest
from scoring import engine as scoring


def test_metric_specific_staleness_does_not_apply_generic_30_day_fill():
    lookup = {"2024-01-01": 42}

    assert backtest._metric_observation(lookup, "2024-01-03", "fear_greed") == (42, "2024-01-01", 2)
    assert backtest._metric_observation(lookup, "2024-01-04", "fear_greed") == (None, None, None)
    assert backtest._metric_observation(lookup, "2024-01-08", "200w_sma") == (42, "2024-01-01", 7)
    assert backtest._metric_observation(lookup, "2024-01-02", "unknown_metric") == (None, None, None)


def test_current_context_score_uses_only_the_common_backtest_panel():
    cached_scored = {
        "composite_score": 100,
        "metrics": [
            {"key": "fear_greed", "score": 10},
            {"key": "puell_multiple", "score": 0},
            {"key": "sopr", "score": 10},
            {"key": "vdd_multiple", "score": 10},
        ],
    }

    score, coverage = backtest._common_panel_current_score(cached_scored)

    assert score == 50.0
    assert coverage == {
        "available_count": 2,
        "panel_count": len(backtest.BACKTEST_METRIC_PANEL),
        "available_keys": ["fear_greed", "puell_multiple"],
    }


def test_live_and_backtest_outputs_publish_panel_and_coverage_metadata():
    live = scoring.score_all({"fear_greed": {"value": 10}})

    assert live["metric_panel"]["id"] == "live-all-v1"
    assert live["metric_panel"]["count"] == live["total_count"]
    assert live["coverage"]["available_count"] == live["scored_count"]
    assert live["coverage"]["ratio"] == live["scored_count"] / live["total_count"]
    assert "score_version" in live

    metadata = backtest._backtest_data_quality_metadata([3, 5, 9])
    assert metadata["metric_panel"]["id"] == "historical-common-v1"
    assert metadata["metric_panel"]["keys"] == list(backtest.BACKTEST_METRIC_PANEL)
    assert metadata["coverage"] == {
        "minimum_metrics": 3,
        "maximum_metrics": 9,
        "average_metrics": 5.7,
        "panel_count": 9,
    }
    assert metadata["staleness_days"] == backtest.METRIC_MAX_AGE_DAYS
