"""
================================================================================
Strategy **D007** — ``s007``
================================================================================

Theme
-----
**Melt-up / near highs with call-skew harvesting (short risk reversal).**

Economic idea
-------------
Near **252-session highs** (``dist_roll_high_252 > -0.01``), upside convexity demand can
rise; a **short risk reversal** sells an OTM put and buys a further OTM call, collecting
premium if the market **does not** gap down through the short put strike. This is
**not** a covered call; it is a skew trade with explicit long call wing.

Entry
-----
1. ``dist_roll_high_252 > -0.01`` (within ~1% of rolling max close).
2. ``VIX < 16`` (very calm; different band than other sleeves).
3. ``skew(40,-0.18,0.12) > 0.035`` (put IV meaningfully above call IV at those deltas).
4. ``n_contracts[i] >= 100``.

Exit
----
``rr`` at 40 DTE, put delta **-0.22**, long call delta **0.10**, hold **8**.
================================================================================
"""

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": "D007", "theme": "melt_up_skew", "title": "Near 252d highs + steep skew short risk reversal"}

HOLD_SESSIONS = 8
TRADE_KIND = "rr"
TRADE_PARAMS = (40, -0.22, 0.10)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    d = row.get("dist_roll_high_252")
    if d is None or pd.isna(d) or float(d) <= -0.01:
        return False
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 16.0:
        return False
    sk = ctx.skew_iv_diff(i, 40, -0.18, 0.12)
    if sk is None or sk <= 0.035:
        return False
    if ctx.chain_contracts(i) < 100:
        return False
    if not ch.contracts:
        return False
    _ = spy, row
    return True
