"""
================================================================================
Strategy **D011** — ``s011``
================================================================================

Theme
-----
**VIX collapse / mean reversion of fear (short straddle).**

Economic idea
-------------
When VIX **falls sharply** over five sessions (``vix_chg_5 < -4`` points), implied
volatility may have overshot to the downside near term, or realized shocks have abated.
This sleeve sells a **30 DTE** ATM straddle with a **short** hold to reduce exposure to
rebound risk.

Entry
-----
1. ``vix_chg_5 < -4.0``.
2. ``VIX > 16`` (avoid selling into already-depressed VIX prints that fail to mean revert).
3. ``iv_atm(30) > rv21`` (still positive VRP at the entry horizon).
4. ``n_contracts[i] >= 80``.

Exit
----
Short straddle 30 DTE, hold **4** sessions.
================================================================================
"""

from __future__ import annotations

import math

import pandas as pd

from RenTech.core.options_data_loader import OptionChain
from RenTech.strategy_stack.diverse_theta_strategies_v1.context import ResearchContext

META = {"sid": "D011", "theme": "vix_path", "title": "Large VIX drop in 5 sessions + VRP short straddle"}

HOLD_SESSIONS = 4
TRADE_KIND = "ss"
TRADE_PARAMS = (30,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    dv = row.get("vix_chg_5")
    if dv is None or pd.isna(dv) or float(dv) >= -4.0:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx <= 16.0:
        return False
    rv = float(row["rv21"]) if pd.notna(row.get("rv21")) else float("nan")
    if not math.isfinite(rv) or rv <= 0:
        return False
    iv = ctx.iv_atm_dte(i, 30)
    if iv is None or iv <= rv:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
