"""
================================================================================
Strategy **D042** — ``s042``  (Bear MA + **large** VIX rise — long straddle, not vertical)
================================================================================
**Idea:** Unlike D024 (bear call), this is **long vol** when ``close<SMA200`` and
``vix_chg_5 > 3.5`` (fear accelerating into a weak tape).
**Entry:** as above, ``VIX<38``, ``contracts>=80``.
**Exit:** Long straddle **12** DTE, hold **3**.
================================================================================
"""

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": "D042", "theme": "cross_spot_vix", "title": "SPY<SMA200 + sharp VIX rise long straddle"}

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


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    s200 = row.get("sma_200")
    if s200 is None or pd.isna(s200) or float(row["close"]) >= float(s200):
        return False
    dv = row.get("vix_chg_5")
    if dv is None or pd.isna(dv) or float(dv) <= 3.5:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 38.0:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
