"""
================================================================================
Strategy **D018** — ``s018``
================================================================================

Theme
-----
**Skew steepness at 30 DTE (put IV minus call IV) — short risk reversal.**

Economic idea
-------------
Steep put skew (``IV_put(-0.20) - IV_call(0.12)`` large) can indicate crash insurance
demand. This sleeve sells the **standardized** skew pair at **30 DTE** when the diff
exceeds **0.05**, with SPY still **above** its 200-day MA (trend filter unrelated to D000).

Entry
-----
1. ``skew(30,-0.20,0.12) > 0.05`` (must exist).
2. ``close > sma_200``.
3. ``VIX`` between **15 and 28**.
4. ``n_contracts[i] >= 90``.

Exit
----
``rr`` 30 DTE, put **-0.20**, call **0.12**, hold **7**.
================================================================================
"""

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": "D018", "theme": "skew", "title": "Steep 30d skew + SPY>SMA200 short risk reversal"}

HOLD_SESSIONS = 7
TRADE_KIND = "rr"
TRADE_PARAMS = (30, -0.20, 0.12)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    sk = ctx.skew_iv_diff(i, 30, -0.20, 0.12)
    if sk is None or sk <= 0.05:
        return False
    s200 = row.get("sma_200")
    if s200 is None or pd.isna(s200) or float(row["close"]) <= float(s200):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 15.0 or vx > 28.0:
        return False
    if ctx.chain_contracts(i) < 90:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
