"""
================================================================================
Strategy **D000** — ``s000``
================================================================================

Theme
-----
**Trend / moving-average regime change (bullish structural shift).**

Economic idea
-------------
A **golden cross** (the short moving average of price crossing *up* through the long
moving average) is a slow, discretionary-style bull signal. The hypothesis embedded in
this sleeve is *not* that the cross predicts positive drift over the next few sessions
(we do not test spot alpha directly). Instead, the trade is a **short-dated cash-secured
put–like** expression: after a bull regime flip, index option skew and downside demand
often remain temporarily rich, while realized volatility may fall as trend followers
reduce two-sided hedging. This sleeve **only** fires on the crossing day itself (no
persistent “always long when above MA” logic), so entries are sparse and each is an
event-study style snapshot.

Entry (all must hold on session ``i``)
--------------------------------------
1. **Golden cross:** ``SMA50 > SMA200`` on session ``i`` and ``SMA50 <= SMA200`` on
   session ``i-1`` (using the same ``sma_50`` / ``sma_200`` columns carried on the
   research panel).
2. **Complacency cap:** ``VIX < 18`` at the close aligned to the Theta session (the
   panel’s ``vix_close``), to avoid selling puts into an already panicked surface.
3. **Chain depth:** at least **80** option contracts in the loaded chain for the day
   (``n_contracts[i]``), so strikes are not missing due to thin archives.

Exit / holding
--------------
- **Structure:** short one OTM put targeted by delta / DTE (engine finds the leg).
- **Horizon:** **10** sessions (Theta session count, not calendar days).
- **Exit mechanics:** identical to the research stack: conservative bid/ask at open,
  conservative marks at exit or settlement.

Data dependencies
-----------------
- Panel: ``sma_50``, ``sma_200``, ``vix_close``, ``close``.
- Caches: none beyond standard chain for execution.
- No look-ahead: cross uses **yesterday vs today** only.

Non-goals / caveats
-------------------
- Close-only MAs; no intraday cross detection.
- No position sizing or margin realism beyond the shared engine’s one-lot convention.
================================================================================
"""

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": "D000",
    "theme": "trend_ma_regime",
    "title": "Golden cross day + low VIX short OTM put",
}

HOLD_SESSIONS = 10
TRADE_KIND = "put"
TRADE_PARAMS = (35, -0.22)


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:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy, row
    return True
