"""
================================================================================
Strategy **D012** — ``s012``
================================================================================

Theme
-----
**High VIX level + explicit IV-vs-RV inequality (short straddle).**

Economic idea
-------------
This is a **level** test: ``VIX >= 24`` indicates a stressed regime, but if **ATM IV**
near 40 DTE still exceeds realized vol by a **multiplicative** margin ``iv > rv * 1.4``,
the seller hypothesis is that the surface embeds an excessive premium for continuation.
Different from D008 (trend stack) and D011 (VIX path).

Entry
-----
1. ``VIX >= 24``.
2. ``iv_atm(40)`` exists and ``iv_atm(40) > 1.4 * rv21``.
3. ``rv21`` finite and positive.
4. ``n_contracts[i] >= 80``.

Exit
----
Short straddle 40 DTE, hold **5** 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": "D012", "theme": "vix_level_vrp", "title": "High VIX + IV>1.4*RV21 short straddle"}

HOLD_SESSIONS = 5
TRADE_KIND = "ss"
TRADE_PARAMS = (40,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 24.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, 40)
    if iv is None or iv <= 1.4 * rv:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
