"""
================================================================================
Strategy **D024** — ``s024``
================================================================================

Theme
-----
**Bearish spot vs long MA + rising fear (bear call), distinct from D009 stack.**

Economic idea
-------------
If SPY is **below** ``SMA200`` while VIX **rises** at least **+2 points** over five
sessions, slow-money drawdowns can continue; a **bear call** expresses bearish/flat
views with capped loss. This does **not** require SMA20<SMA50 ordering (unlike D009).

Entry
-----
1. ``close < sma_200`` (finite).
2. ``vix_chg_5 > 2.0``.
3. ``VIX < 40`` (cap).
4. ``n_contracts[i] >= 80``.

Exit
----
Bear call 28 DTE, short delta **0.20**, wing **7**, hold **6**.
================================================================================
"""

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": "D024", "theme": "cross_spot_vix", "title": "SPY<SMA200 + VIX rising bear call"}

HOLD_SESSIONS = 6
TRADE_KIND = "vtc"
TRADE_PARAMS = (28, 0.20, 7.0)


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):
        return False
    if float(row["close"]) >= float(s200):
        return False
    dv = row.get("vix_chg_5")
    if dv is None or pd.isna(dv) or float(dv) <= 2.0:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 40.0:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
