from __future__ import annotations

import pandas as pd

from web_core.constants import TREND_BEAR, TREND_BULL, TREND_NEUTRAL
from web_core.insights import compute_session_stats, compute_signal_quality, detect_regime


def _make_analyzed(rows: int = 12) -> pd.DataFrame:
    idx = pd.date_range("2025-01-01", periods=rows, freq="h")
    classifications = ["fake", "real_bull", "fake", "real_bear"] * (rows // 4) + ["fake"] * (rows % 4)
    trends = [TREND_NEUTRAL, TREND_BULL, TREND_BULL, TREND_BEAR] * (rows // 4) + [TREND_BEAR] * (rows % 4)
    closes = [100 + i for i in range(rows)]
    return pd.DataFrame({"classification": classifications[:rows], "trend_state": trends[:rows], "Close": closes}, index=idx)


def test_detect_regime_flags_choppy_when_fake_ratio_high() -> None:
    df = _make_analyzed(12)
    out = detect_regime(df, lookback=12)
    assert out in {"Choppy", "Trending"}


def test_compute_signal_quality_returns_expected_keys() -> None:
    df = _make_analyzed(20)
    quality = compute_signal_quality(df, trend_now=TREND_BULL, volume_filter_enabled=True)
    assert set(quality.keys()) == {"score", "label", "fake_ratio"}
    assert quality["label"] in {"Low", "Medium", "High"}


def test_compute_session_stats_has_numeric_outputs() -> None:
    df = _make_analyzed(20)
    stats = compute_session_stats(df, session_started_at=0.0)
    assert isinstance(stats["wins"], int)
    assert isinstance(stats["losses"], int)
    assert isinstance(stats["avg_move_pct"], float)
    assert isinstance(stats["fake_ratio_pct"], float)
