"""Type definitions for ``gate``.

TypedDicts are used so partners get auto-completion + mypy support
without forcing them to construct dataclasses. Mirrors the OpenAPI
spec at ``0Bit_apps/0Gate/0Gate_Widget/openapi/0bit-ramp-v1.yaml``.
"""

from __future__ import annotations

from typing import Any, Dict, List, Literal, Optional, TypedDict, Union

SessionStatus = Literal["open", "completed", "expired", "cancelled"]
SessionMode = Literal["test", "live"]

WebhookEventType = Literal[
    "gate_session.created",
    "gate_session.completed",
    "gate_session.expired",
    "gate_session.cancelled",
]


class Session(TypedDict):
    id: str
    object: Literal["session"]
    partner_id: str
    mode: SessionMode
    amount: str
    currency: str
    target_token: Optional[str]
    target_network: Optional[str]
    return_url: str
    cancel_url: Optional[str]
    status: SessionStatus
    expires_at: str
    created_at: Optional[str]
    metadata: Dict[str, Any]


class SessionWithClientSecret(Session):
    client_secret: str


class CreateSessionParams(TypedDict, total=False):
    amount: str
    currency: str
    return_url: str
    cancel_url: str
    target_token: str
    target_network: str
    metadata: Dict[str, Any]


class EmbedBootstrapParams(TypedDict, total=False):
    clientSecret: str
    requested_scopes: List[str]


class EmbedBootstrapResponse(TypedDict):
    embed_token: str
    expires_at: str
    partner_id: str
    mode: SessionMode
    session_id: Optional[str]
    amount: Optional[str]
    currency: Optional[str]
    target_token: Optional[str]
    target_network: Optional[str]
    return_url: Optional[str]


class _SessionCompletedData(Session):
    tx_refid: str


class WebhookEvent(TypedDict):
    """Envelope for any ``gate_session.*`` event.

    ``data`` is a :class:`Session` for the non-completed variants, and a
    :class:`Session` plus ``tx_refid`` for ``gate_session.completed``.
    Use ``event["type"]`` to discriminate before reading ``tx_refid``.
    """

    id: str
    type: WebhookEventType
    created_at: int
    data: Union[Session, _SessionCompletedData]


__all__ = [
    "SessionStatus",
    "SessionMode",
    "WebhookEventType",
    "Session",
    "SessionWithClientSecret",
    "CreateSessionParams",
    "EmbedBootstrapParams",
    "EmbedBootstrapResponse",
    "WebhookEvent",
]
