"""
Input validators for form and API data.

validate_amount() is the canonical money-input parser:
  - Strips whitespace, leading '$', commas
  - Raises ValueError for non-numeric, zero, or negative values
  - Returns Decimal rounded to 2 decimal places
  - Never returns float — Decimal only
"""
from __future__ import annotations

from decimal import Decimal, InvalidOperation


def validate_amount(value) -> Decimal:
    """
    Parse and validate a monetary amount from user input.

    Args:
        value: str, int, float, or Decimal representing a money amount.

    Returns:
        Decimal rounded to 2 decimal places (e.g. Decimal('29.99')).

    Raises:
        ValueError: If value is non-numeric, zero, or negative.
    """
    if value is None:
        raise ValueError("Amount must be a positive number")

    # Normalise string input: strip whitespace, leading $, commas
    if isinstance(value, str):
        value = value.strip().lstrip('$').replace(',', '')
        if not value:
            raise ValueError("Amount must be a positive number")

    try:
        amount = Decimal(str(value))
    except InvalidOperation:
        raise ValueError("Amount must be a positive number")

    if amount <= 0:
        raise ValueError("Amount must be a positive number")

    return round(amount, 2)
