"""
================================================================================
Strategy **D020** — ``s020``
================================================================================

Theme
-----
**Realized vol term structure spike (5d vs 21d) — long straddle.**

Economic idea
-------------
``rv_ratio_5_21 > 1.2`` means very recent realized volatility is **much** higher than the
month horizon. That can precede continued chop. This sleeve buys **10 DTE** straddle
when **VIX is not** exploding (``VIX < 26``) to separate from crisis tail.

Entry
-----
1. ``rv_ratio_5_21 > 1.2``.
2. ``VIX < 26``.
3. ``iv_atm(10)`` exists.
4. ``n_contracts[i] >= 80``.

Exit
----
Long straddle 10 DTE, hold **3** 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": "D020", "theme": "realized_vol_term", "title": "RV5>>RV21 + non-extreme VIX long straddle"}

HOLD_SESSIONS = 3
TRADE_KIND = "sl"
TRADE_PARAMS = (10,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    rr = row.get("rv_ratio_5_21")
    if rr is None or pd.isna(rr) or float(rr) <= 1.2:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 26.0:
        return False
    if ctx.iv_atm_dte(i, 10) is None:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
