"""
================================================================================
Strategy **D038** — ``s038``  (ATM IV contango 45 vs 7 — short straddle)
================================================================================
**Idea:** ``iv45 - iv7 >= 0.025`` (back IV materially above front), sell **35** DTE straddle.
**Entry:** inequality, ``VIX<=21``, ``rv_ratio_5_21<=1.05``, ``contracts>=80``.
**Exit:** Short straddle 35 DTE, hold **5**.
================================================================================
"""

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": "D038", "theme": "iv_term", "title": "Back IV above front IV + calm RV ratio short straddle"}

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


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