"""
================================================================================
Strategy **D041** — ``s041``  (Bull MA + “VIX drifting down slowly” short put)
================================================================================
**Idea:** ``close>SMA200`` with **small** negative or slightly positive VIX change captures
slow bull complacency; sell OTM put **38** DTE.
**Entry:** spot>200 MA, ``-2.0 < vix_chg_5 < 0.5``, ``VIX<20``, ``contracts>=90``.
**Exit:** Short put 38 DTE **-0.23**, hold **10**.
================================================================================
"""

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": "D041", "theme": "cross_spot_vix", "title": "SPY>SMA200 + VIX drift band short put"}

HOLD_SESSIONS = 10
TRADE_KIND = "put"
TRADE_PARAMS = (38, -0.23)


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):
        return False
    dvf = float(dv)
    if dvf <= -2.0 or dvf >= 0.5:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 20.0:
        return False
    if ctx.chain_contracts(i) < 90 or not ch.contracts:
        return False
    _ = spy
    return True
