"""
================================================================================
Strategy **D001** — ``s001``
================================================================================

Theme
-----
**Bearish structural regime + bounded fear (defined-risk bear call).**

Economic idea
-------------
A **death cross** (short MA crossing *down* through the long MA) is often associated with
durable distribution, slower money reducing exposure, and a higher chance of **trending**
down moves that hurt short-vol strategies. This sleeve does **not** short a naked
straddle into that regime; it uses a **bear call credit spread** (short lower call, long
higher call) so upside shocks have capped loss. The VIX band **18–32** is an explicit
attempt to trade “bad trend, but not pure crash pricing”: below 18 the structure may be
paying too little credit; above 32 the short call wing can be too gamma-heavy for a
simple vertical.

Entry
-----
1. **Death cross today:** ``SMA50 < SMA200`` now and ``SMA50 >= SMA200`` yesterday.
2. **VIX band:** ``18 <= VIX <= 32``.
3. **Chain depth:** ``n_contracts[i] >= 80``.

Exit / holding
--------------
- **Structure:** bear call vertical (``vtc``) with short strike chosen by call delta
  ``0.22`` and wing ``8`` points (same expiry).
- **Hold:** **7** sessions.

Caveats
-------
Close-only trend proxy; no earnings filter (SPY only).
================================================================================
"""

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": "D001", "theme": "trend_ma_regime", "title": "Death cross + mid VIX bear call spread"}

HOLD_SESSIONS = 7
TRADE_KIND = "vtc"
TRADE_PARAMS = (32, 0.22, 8.0)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    if i < 1:
        return False
    r0 = ctx.row(i)
    r1 = ctx.row(i - 1)
    s50_0 = r0.get("sma_50")
    s200_0 = r0.get("sma_200")
    s50_1 = r1.get("sma_50")
    s200_1 = r1.get("sma_200")
    if any(pd.isna(x) for x in (s50_0, s200_0, s50_1, s200_1)):
        return False
    if float(s50_0) >= float(s200_0):
        return False
    if float(s50_1) < float(s200_1):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 18.0 or vx > 32.0:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy, row
    return True
