"""Compute exponential backoff delay with optional jitter."""

import random


def compute_backoff_delay(
    attempt: int,
    base_delay: float = 0.5,
    max_delay: float = 8.0,
    jitter: bool = True,
) -> float:
    """Compute exponential backoff delay for a given attempt number.

    Args:
        attempt: Zero-based attempt index (0 = first retry).
        base_delay: Base delay in seconds before exponential scaling.
        max_delay: Maximum delay cap in seconds.
        jitter: If True, adds random jitter to avoid thundering herd.

    Returns:
        Delay in seconds to wait before the next attempt.
    """
    delay = min(base_delay * (2 ** attempt), max_delay)
    if jitter:
        delay += random.uniform(0, min(base_delay, delay))
    return delay
