"""Billing configuration and Stripe client initialization."""

from __future__ import annotations

import stripe
from pydantic import BaseModel, Field


class BillingConfig(BaseModel):
    """Configuration for the billing module."""

    stripe_secret_key: str = Field(description="Stripe secret API key")
    stripe_webhook_secret: str = Field(description="Stripe webhook signing secret")
    success_url: str = Field(description="Redirect URL after successful checkout")
    cancel_url: str = Field(description="Redirect URL after cancelled checkout")
    product_plans: dict[str, str] = Field(
        default_factory=dict,
        description="Mapping of plan_name -> stripe_price_id",
    )


_config: BillingConfig | None = None


def init_billing(config: BillingConfig) -> None:
    """Initialize the billing module with the given configuration.

    Sets the Stripe API key and stores config for later use by all billing functions.

    Args:
        config: Billing configuration with Stripe credentials and plan mappings.
    """
    global _config
    _config = config
    stripe.api_key = config.stripe_secret_key


def get_config() -> BillingConfig:
    """Return the current billing configuration.

    Raises:
        RuntimeError: If init_billing has not been called.
    """
    if _config is None:
        raise RuntimeError(
            "Billing not initialized. Call init_billing(config) before using billing functions."
        )
    return _config
