from __future__ import annotations

from dataclasses import dataclass
from typing import TypedDict


def parse_price(price: str | float | int) -> float:
    if isinstance(price, (int, float)):
        return float(price)
    s = str(price).strip()
    if s.startswith("$"):
        s = s[1:]
    return float(s)


class RouteConfig(TypedDict, total=False):
    method: str  # HTTP method to gate; defaults to "GET"
    path: str  # exact request path, e.g. "/v1/financials" (required)
    price: str | float
    description: str


# a402's hosted facilitator. Sellers don't run their own, so facilitator_url is
# optional and only set to override (self-hosting, or staging during dev). Swap to
# the prod host (p8l.app) when the mainnet facilitator ships.
DEFAULT_FACILITATOR_URL = "https://facilitator-testnet.a402.finance"


@dataclass
class A402Config:
    api_key: str
    facilitator_url: str = DEFAULT_FACILITATOR_URL
    # Per-route overrides are merged in by A402Middleware; not set here.
    # Facilitator request timeout (challenge + settle). Generous by default: the
    # multi-chain challenge does per-chain reads and slow-chain settles wait for
    # an on-chain receipt — a 10s default produced real spurious 502s on staging.
    timeout: float = 60.0
