# tests/test_feature_block_liquidity.py

import polars as pl

from ocf.data.normalize import (
    normalize_ohlcv,
    normalize_oi,
)
from ocf.features.liquidity import build_liquidity_features


def test_liquidity_feature_block_from_raw():
    """
    Test liquidity & positioning features using real Bloomberg-style data.

    Verifies:
    - normalization works
    - minimal canonical_underlying_daily construction
    - liquidity feature builder runs in isolation
    - expected liquidity feature columns exist
    - no base columns are dropped

    Does NOT test alignment or pipeline orchestration.
    """

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

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

    oi = normalize_oi(
        pl.read_csv(f"{raw_dir}/oi.csv"),
        symbol,
    )

    # ------------------------------------------------------------------
    # Minimal canonical_underlying_daily
    # (join OHLCV + OI only)
    # ------------------------------------------------------------------
    underlying_daily = (
        ohlcv
        .join(
            oi,
            on=["symbol", "date"],
            how="left",
        )
    )

    assert not underlying_daily.is_empty()

    base_cols = set(underlying_daily.columns)

    # ------------------------------------------------------------------
    # Build liquidity features
    # ------------------------------------------------------------------
    featured = build_liquidity_features(
        underlying_daily
    )

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

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

    # Expected liquidity feature columns
    expected_features = {
        "ba_spread",
        "ba_spread_pct",
        "ba_spread_vol_adj",
        "open_int_total",
        "open_int_imbalance",
        "call_oi_change_1d",
        "put_oi_change_1d",
        "volume_change_1d",
        "volume_to_oi",
        "volume_shock_20d",
        "spread_vol_20d",
    }

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

    # Sanity: OI-derived features populated somewhere
    assert featured["open_int_total"].is_null().sum() < featured.height