"""
================================================================================
Strategy **D019** — ``s019``
================================================================================

Theme
-----
**Skew flattening / low put-minus-call spread — long straddle.**

Economic idea
-------------
When skew at **40 DTE** for deltas **(-0.15, 0.10)** is **below 0.018**, the surface is
relatively flat in that region — potentially underpricing two-sided uncertainty before
macro windows. Buys **18 DTE** straddle (different from D016 inversion).

Entry
-----
1. ``skew(40,-0.15,0.10)`` exists and ``< 0.018``.
2. ``ret_20`` exists and ``abs(ret_20) < 0.04`` (SPY not in a huge 20d trend; avoids
   directional straddle bleed).
3. ``n_contracts[i] >= 80``.

Exit
----
Long straddle 18 DTE, hold **5** sessions.
================================================================================
"""

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": "D019", "theme": "skew", "title": "Flat 40d skew + mild 20d drift long straddle"}

HOLD_SESSIONS = 5
TRADE_KIND = "sl"
TRADE_PARAMS = (18,)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    sk = ctx.skew_iv_diff(i, 40, -0.15, 0.10)
    if sk is None or sk >= 0.018:
        return False
    r20 = row.get("ret_20")
    if r20 is None or pd.isna(r20) or abs(float(r20)) >= 0.04:
        return False
    if ctx.chain_contracts(i) < 80:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
