import polars as pl

from ocf.data.normalize import (
    normalize_ohlcv,
    normalize_oi,
    normalize_ivol_30,
    normalize_ivol_60,
    normalize_sofr,
    normalize_usgg,
    normalize_vix,
)
from ocf.data.align import align_underlying_daily
from ocf.features.build import build_features


def test_public_features_api_from_raw():
    """
    Public API test for build_features().

    Verifies:
    - real raw data works
    - user-facing feature builder runs
    - include/exclude mechanism works
    - returns model-ready DataFrame
    """

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

    # Normalize
    ohlcv = normalize_ohlcv(pl.read_csv(f"{raw_dir}/ohlcv.csv"), symbol)
    oi = normalize_oi(pl.read_csv(f"{raw_dir}/oi.csv"), symbol)
    iv30 = normalize_ivol_30(pl.read_csv(f"{raw_dir}/ivol_30_mny.csv"), symbol)
    iv60 = normalize_ivol_60(pl.read_csv(f"{raw_dir}/ivol_60_mny.csv"), symbol)
    sofr = normalize_sofr(pl.read_csv(f"{raw_dir}/riskfree_sofr.csv"), symbol)
    usgg = normalize_usgg(pl.read_csv(f"{raw_dir}/riskfree_usgg.csv"), symbol)
    vix = normalize_vix(pl.read_csv(f"{raw_dir}/vix.csv"), symbol)

    underlying = align_underlying_daily(
        symbol=symbol,
        ohlcv=ohlcv,
        oi=oi,
        iv30=iv30,
        iv60=iv60,
        sofr=sofr,
        usgg=usgg,
        vix=vix,
    )

    # Public API call
    features = build_features(
        underlying,
        include=["ohlcv", "iv"],
    )

    assert not features.is_empty()
    assert "date" in features.columns

    # Should contain features from included blocks
    assert any(c.startswith("ret_") for c in features.columns)
    assert "iv_rank" in features.columns