from .base import (
    PaymentProvider,
    PaymentRequest,
    PaymentCreationResult,
    PaymentNotificationResult,
    PaymentRefundRequest,
    PaymentRefundResult,
    SubscriptionUpdateResult,
)

_PROVIDER_REGISTRY: dict[str, type[PaymentProvider]] = {}


def register_payment_provider(provider_cls: type[PaymentProvider]) -> None:
    """Register a payment provider class keyed by its declared channel."""
    channel = provider_cls.channel
    if not channel:
        raise ValueError("Payment provider must declare a non-empty channel")
    _PROVIDER_REGISTRY[channel] = provider_cls


def get_payment_provider(channel: str) -> PaymentProvider:
    """Instantiate a provider for the requested channel."""
    try:
        provider_cls = _PROVIDER_REGISTRY[channel]
    except KeyError as exc:
        raise ValueError(f"Unsupported payment channel: {channel}") from exc
    return provider_cls()


__all__ = [
    "PaymentProvider",
    "PaymentRequest",
    "PaymentCreationResult",
    "PaymentNotificationResult",
    "PaymentRefundRequest",
    "PaymentRefundResult",
    "SubscriptionUpdateResult",
    "register_payment_provider",
    "get_payment_provider",
]

# Ensure built-in providers are registered on import.
from . import pingxx  # noqa: E402,F401
from . import stripe  # noqa: E402,F401
from . import alipay  # noqa: E402,F401
from . import wechatpay  # noqa: E402,F401
