import pandas as pd

def validate_continuity(
    df: pd.DataFrame,
    timeframe: str,
    lookback: int | None = None,
):
    """
    Valida continuidad temporal del OHLCV.

    Si lookback se especifica, solo valida las últimas N velas.
    """
    if timeframe.endswith("h"):
        delta = pd.Timedelta(hours=int(timeframe.replace("h", "")))
    elif timeframe.endswith("d"):
        delta = pd.Timedelta(days=int(timeframe.replace("d", "")))
    else:
        raise NotImplementedError(f"Timeframe no soportado: {timeframe}")

    if lookback is not None:
        df = df.tail(lookback)

    diffs = df.index.to_series().diff()
    gaps = diffs[diffs > delta * 1.5]

    if not gaps.empty:
        raise RuntimeError(
            f"GAPS detectados en OHLCV:\n{gaps.head()}"
        )
