"""RSI (Wilder, rolling-mean smoothing) — port fedele da V15."""

from __future__ import annotations

import pandas as pd


def calc_rsi(df: pd.DataFrame, period: int = 14) -> pd.Series:
    """
    RSI on `df['close']` using rolling-mean smoothing (V14/V15 convention).

    Returns the full series so callers can read iloc[-1] (current) and
    iloc[-2] (previous) for `rsi`/`rsi_prev`.
    """
    delta = df["close"].diff()
    gain = delta.where(delta > 0, 0).rolling(period).mean()
    loss = (-delta.where(delta < 0, 0)).rolling(period).mean()
    return 100 - (100 / (1 + gain / (loss + 1e-9)))
