"""STRUCTURE guard for the crypto honest-reference research report (spec 9, Task 8).

`build_honest_report` is PURE: it reads an in-memory feature store and takes the spread floor + unlock
calendar INJECTED (here `{}` / `[]`), and writes NOTHING. This test seeds a flat-funding xsfunding fixture
(the SAME shape the Task-1 fidelity test uses — funding is the sleeve's only return, no unlock_events needed)
plus a turnover feature so the honest capacity/cost pipeline has ADV to size against, then asserts the report
has the full shape the orchestrator + cockpit consume. No real DB: sqlite in-memory store only."""
from __future__ import annotations

from fxhnt.adapters.warehouse.timescale_feature_store import TimescaleFeatureStore
from fxhnt.application.honest_report import build_honest_report

_DAY = 86_400


def _seed_flat_funding(store: TimescaleFeatureStore, *, days: int = 120) -> None:
    """FLAT perp price, spot == perp (zero basis), nonzero cross-sectional funding + a large flat turnover
    (ADV) so the participation cap is non-binding — the xsfunding sleeve's only return here is funding."""
    funding = {"AAAUSDT": 0.002, "BBBUSDT": 0.002, "CCCUSDT": 0.002,
               "XXXUSDT": -0.002, "YYYUSDT": -0.002, "ZZZUSDT": -0.002}
    for sym, f in funding.items():
        store.write_features(sym, [(d * _DAY, {
            "close": 100.0, "spot_close": 100.0, "funding": f,
            "high": 100.0, "low": 100.0, "turnover": 1e9}) for d in range(days)])


def test_report_has_full_shape() -> None:
    store = TimescaleFeatureStore("sqlite://", table="bybit_features")
    _seed_flat_funding(store)

    aums = [35_000, 100_000, 350_000, 1_000_000, 10_000_000]
    ks = [1.0, 2.0, 4.0]
    report = build_honest_report(store, spread_bps={}, unlock_events=[],
                                 sleeves=["xsfunding"], aums=aums, ks=ks)
    store.close()

    assert set(report) >= {"aums", "ks", "base", "sleeves", "book",
                           "gates", "leverage_ceiling", "edge4_pump", "survivorship"}

    assert report["base"] == [aums[0], ks[0]]
    assert len(report["ks"]) == 3

    sleeve_out = report["sleeves"]["xsfunding"]
    curve = sleeve_out["capacity_curve"]
    assert set(curve) == set(aums)                       # 5 AUM keys
    for aum in aums:
        assert set(curve[aum]) == set(ks)                # each with 3 k-entries
        for k in ks:
            assert set(curve[aum][k]) >= {"days", "sharpe", "total_return_pct", "maxdd_pct"}

    assert "capacity_ceiling" in sleeve_out                       # next to the curve, per spec 6/8
    assert sleeve_out["capacity_ceiling"] is None or isinstance(sleeve_out["capacity_ceiling"], (int, float))

    note = report["survivorship"]["note"]
    assert isinstance(note, str) and note and "survivorship" in note.lower()
    assert report["survivorship"]["n_coins"] > 0        # distinct symbols seen in the sleeve weights

    assert set(report["edge4_pump"]) >= {"sharpe", "survives_execution", "marginal_vs_positioning"}
