"""
================================================================================
Strategy **D013** — ``s013``
================================================================================

Theme
-----
**Very low VIX + percentile context (short strangle, different deltas from D004).**

Economic idea
-------------
When **VIX is below 13** *and* its **252-session percentile rank** is also below 0.15, we
are in a historically rare calm bucket. This sleeve sells a **tighter delta** strangle
than D004 (more aggressive) but only in that joint condition.

Entry
-----
1. ``VIX < 13``.
2. ``vix_pct_rank_252`` finite and ``< 0.15``.
3. ``bb_width_20 > 0.03`` (not simultaneously in a dead squeeze; avoids double-selling calm).
4. ``n_contracts[i] >= 120``.

Exit
----
Short strangle 45 DTE, put **-0.18**, call **0.12**, 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": "D013", "theme": "vix_regime", "title": "Very low VIX + low pct rank + not ultra-squeeze strangle"}

HOLD_SESSIONS = 5
TRADE_KIND = "sg"
TRADE_PARAMS = (45, -0.18, 0.12)


def wants_entry(i: int, row: pd.Series, ch: OptionChain, spy: float, ctx: ResearchContext) -> bool:
    vx = ctx.vix(i)
    if not math.isfinite(vx) or vx >= 13.0:
        return False
    pr = row.get("vix_pct_rank_252")
    if pr is None or pd.isna(pr) or float(pr) >= 0.15:
        return False
    bw = row.get("bb_width_20")
    if bw is None or pd.isna(bw) or float(bw) <= 0.03:
        return False
    if ctx.chain_contracts(i) < 120:
        return False
    if not ch.contracts:
        return False
    _ = spy
    return True
