from __future__ import annotations

"""Helper to bridge the Stripe client with the GUI."""

import webbrowser
from dataclasses import dataclass
from typing import Optional

from .stripe_client import StripeClient


@dataclass
class PaymentModalController:
    """Controller used by the GUI to trigger Stripe checkout sessions."""

    client: StripeClient

    def open_checkout(self, tier: str, cadence: str, open_browser: bool = True) -> str:
        """Create a checkout session and optionally open it in the default browser."""
        session = self.client.create_checkout_session(tier, cadence)
        url = session["url"]
        if open_browser:
            webbrowser.open(url)
        return url

    @classmethod
    def for_test(cls) -> "PaymentModalController":
        """Utility to instantiate a test-ready controller."""
        return cls(StripeClient(test_mode=True, api_key="sk_test_dummy"))
