"""Data validation helpers."""

from __future__ import annotations

from typing import Any, Optional


def require_float(value: Any, name: str = "value") -> float:
    """Convert ``value`` to float, raising ValueError on failure."""
    try:
        f = float(value)
    except (TypeError, ValueError) as e:
        raise ValueError(f"{name} must be a number, got: {value!r}") from e
    return f


def require_int(value: Any, name: str = "value") -> int:
    """Convert ``value`` to int, raising ValueError on failure."""
    try:
        return int(value)
    except (TypeError, ValueError) as e:
        raise ValueError(f"{name} must be an integer, got: {value!r}") from e


def require_symbol(value: Any) -> str:
    """Normalize and validate a symbol string."""
    s = str(value).strip().upper()
    if not s:
        raise ValueError("symbol cannot be empty")
    if len(s) > 20:
        raise ValueError(f"symbol too long: {s!r}")
    return s


def require_positive(value: float, name: str = "value") -> float:
    """Ensure a numeric value is positive."""
    if not isinstance(value, (int, float)) or value <= 0:
        raise ValueError(f"{name} must be a positive number, got: {value}")
    return value
