"""
================================================================================
Strategy **D049** — ``s049``  (ATM IV: near > far by calendar band — long straddle)
================================================================================
**Idea:** ``iv21 - iv55 > 0.02`` uses **different** DTE pair than D016/D037/D038; add
``day_of_month in [15,16,17,18]`` idiosyncratic calendar gate unrelated to OPEX week.
**Entry:** IV inequality + mid-month days + ``VIX<29``, ``contracts>=80``.
**Exit:** Long straddle **22** DTE, hold **4**.
================================================================================
"""

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": "D049", "theme": "iv_term_calendar", "title": "Mid-month + IV21 above IV55 long straddle"}

HOLD_SESSIONS = 4
TRADE_KIND = "sl"
TRADE_PARAMS = (22,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    dom = row.get("day_of_month")
    if dom is None or pd.isna(dom) or int(dom) not in (15, 16, 17, 18):
        return False
    a = ctx.iv_atm_dte(i, 21)
    b = ctx.iv_atm_dte(i, 55)
    if a is None or b is None or a - b <= 0.02:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 29.0:
        return False
    if ctx.chain_contracts(i) < 80 or not ch.contracts:
        return False
    _ = spy
    return True
