"""
================================================================================
Strategy **D048** — ``s048``  (Q4 months + moderate VIX — short straddle)
================================================================================
**Idea:** ``month in (10,11,12)`` with ``VIX in [14, 23)`` and **VRP** at 33d.
**Entry:** quarter filter + VIX band + ``iv_atm(33)>1.08*rv21``, ``contracts>=80``.
**Exit:** Short straddle **33** DTE, 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": "D048", "theme": "calendar_season", "title": "Q4 months + mid VIX + VRP short straddle"}

HOLD_SESSIONS = 6
TRADE_KIND = "ss"
TRADE_PARAMS = (33,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    m = row.get("month")
    if m is None or pd.isna(m) or int(m) not in (10, 11, 12):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx < 14.0 or vx >= 23.0:
        return False
    rv = float(row["rv21"]) if pd.notna(row.get("rv21")) else float("nan")
    if not math.isfinite(rv) or rv <= 0:
        return False
    iv = ctx.iv_atm_dte(i, 33)
    if iv is None or iv <= 1.08 * rv:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
