"""
================================================================================
Strategy **D043** — ``s043``  (OPEX week + **rising** VIX — long straddle)
================================================================================
**Idea:** Opposite interaction to D022: expiry week **plus** VIX **rising** (``vix_chg_5>0.4``)
may embed gamma/flow stress.
**Entry:** ``is_op_exp_week``, ``vix_chg_5>0.4``, ``VIX<30``, ``contracts>=80``.
**Exit:** Long straddle **14** DTE, hold **3**.
================================================================================
"""

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": "D043", "theme": "calendar_opex", "title": "OPEX week + VIX rising long straddle"}

HOLD_SESSIONS = 3
TRADE_KIND = "sl"
TRADE_PARAMS = (14,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    fl = row.get("is_op_exp_week")
    if fl is None or pd.isna(fl) or not bool(fl):
        return False
    dv = row.get("vix_chg_5")
    if dv is None or pd.isna(dv) or float(dv) <= 0.4:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 30.0:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
