# tests/test_feature_block_ohlcv.py

import polars as pl

from ocf.data.normalize import normalize_ohlcv
from ocf.features.ohlcv import build_ohlcv_features


def test_ohlcv_feature_block_from_raw():
    """
    Test OHLCV-derived features using real Bloomberg-style raw data.

    Verifies:
    - normalization works
    - OHLCV feature builder runs in isolation
    - expected feature columns exist
    - no base columns are dropped

    Does NOT test alignment or joins.
    """

    raw_dir = "data/raw/bloomberg/spy"
    symbol = "SPY"

    # ------------------------------------------------------------------
    # Load + normalize OHLCV
    # ------------------------------------------------------------------
    raw_ohlcv = pl.read_csv(f"{raw_dir}/ohlcv.csv")
    underlying_daily = normalize_ohlcv(raw_ohlcv, symbol)

    assert not underlying_daily.is_empty()

    base_cols = set(underlying_daily.columns)

    # ------------------------------------------------------------------
    # Build OHLCV features
    # ------------------------------------------------------------------
    featured = build_ohlcv_features(
        underlying_daily,
        vol_window=21,
        ema_fast=10,
        ema_slow=21,
        bb_window=20,
    )

    # ------------------------------------------------------------------
    # Assertions
    # ------------------------------------------------------------------
    assert not featured.is_empty()

    # Base columns must be preserved
    assert base_cols.issubset(set(featured.columns))

    # Expected OHLCV-derived features
    expected_features = {
        # returns
        "ret_1d",
        "overnight_ret",
        "gap_size",

        # realized volatility
        "realized_vol_21d",
        "parkinson_vol_21d",
        "garman_klass_vol_21d",
        "yang_zhang_vol_21d",

        # vol-of-vol
        "vol_of_vol_21d",

        # trend & momentum
        "ema_10",
        "ema_21",
        "bollinger_pct_b",
        "trend_up",
    }

    assert expected_features.issubset(set(featured.columns))

    # Sanity: rolling features populated somewhere
    assert featured["ret_1d"].is_null().sum() < featured.height
    assert featured["realized_vol_21d"].is_null().sum() < featured.height