"""
================================================================================
Strategy **D028** — ``s028``  (late-month + short strangle distinct from OPEX week)
================================================================================
**Idea:** Late month can have different gamma/flow than early month; sell strangle mid-band VIX.
**Entry:** ``25 <= day_of_month <= 31``, ``NOT is_op_exp_week``, ``16<VIX<23``,
``iv_atm(38)>rv21``, ``contracts>=100``.
**Exit:** Short strangle 38 DTE **-0.22 / 0.16**, hold **5**.
================================================================================
"""

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": "D028", "theme": "calendar_flow", "title": "Late month non-OPEX + mid VIX + VRP strangle"}

HOLD_SESSIONS = 5
TRADE_KIND = "sg"
TRADE_PARAMS = (38, -0.22, 0.16)


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):
        return False
    d = int(dom)
    if d < 25 or d > 31:
        return False
    fl = row.get("is_op_exp_week")
    if fl is None or pd.isna(fl) or bool(fl):
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx <= 16.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, 38)
    if iv is None or iv <= rv:
        return False
    if ctx.chain_contracts(i) < 100 or not ch.contracts:
        return False
    _ = spy
    return True
