import polars as pl
from datetime import date

from ocf.pipelines.pipeline import (
    PipelineConfig,
    run_pipeline,
)


def test_pipeline_smoke_from_raw(tmp_path):
    """
    Smoke test: run full pipeline from raw CSV inputs.

    This test verifies:
    - raw ingestion
    - normalization
    - pipeline wiring
    - public Greeks API integration

    It does NOT validate numerical correctness.
    """

    # Arrange: load raw Bloomberg-style CSVs
    raw_dir = "data/raw/bloomberg/spy"

    raw_inputs = {
        "ohlcv": pl.read_csv(f"{raw_dir}/ohlcv.csv"),
        "oi": pl.read_csv(f"{raw_dir}/oi.csv"),
        "iv30": pl.read_csv(f"{raw_dir}/ivol_30_mny.csv"),
        "iv60": pl.read_csv(f"{raw_dir}/ivol_60_mny.csv"),
        "sofr": pl.read_csv(f"{raw_dir}/riskfree_sofr.csv"),
        "usgg": pl.read_csv(f"{raw_dir}/riskfree_usgg.csv"),
        "vix": pl.read_csv(f"{raw_dir}/vix.csv"),
        "chain": pl.read_csv(f"{raw_dir}/chain.csv"),
    }

    # Act: run pipeline
    config = PipelineConfig(
        symbol="SPY",
        snapshot_date=date(2024, 1, 19),
        build_per_option=True,
    )

    outputs = run_pipeline(
        config=config,
        raw_inputs=raw_inputs,
    )

    # Assert: minimal structural guarantees
    assert isinstance(outputs, dict)

    expected_keys = {
        "underlying_daily",
        "features",
        "option_chain_snapshot",
        "per_option_greeks",
    }

    assert expected_keys.issubset(outputs.keys())

    # underlying_daily
    underlying = outputs["underlying_daily"]
    assert isinstance(underlying, pl.DataFrame)
    assert underlying.height > 0
    assert "px_close" in underlying.columns

    # features
    features = outputs["features"]
    assert isinstance(features, pl.DataFrame)
    assert features.height > 0

    # option chain snapshot
    chain = outputs["option_chain_snapshot"]
    assert isinstance(chain, pl.DataFrame)
    assert chain.height > 0
    assert "strike" in chain.columns

    # per-option greeks
    greeks = outputs["per_option_greeks"]
    assert isinstance(greeks, pl.DataFrame)
    assert greeks.height > 0

    for col in ("delta", "gamma", "vega", "theta"):
        assert col in greeks.columns