from __future__ import annotations

from .config import parse_price


def pay(price: str | float | int, description: str = ""):
    """FastAPI route decorator that attaches a402 pricing metadata.

    Usage::

        @router.get("/v1/data")
        @pay("$0.05")
        async def get_data(): ...

    A402Middleware reads the ``_a402_price`` and ``_a402_description`` attributes
    when building the route table, so the decorator alone is not sufficient —
    the route must also appear in the ``routes`` dict passed to A402Middleware,
    or the middleware must be configured to auto-discover decorated routes.
    """
    price_usd = parse_price(price)

    def decorator(func):
        func._a402_price = price_usd
        func._a402_description = description
        return func

    return decorator
