"""Payment status widget block aligned with Smartbank Figma design system.

Provides payment status widgets for displaying payment results
with success, error, and pending states.

Figma reference:
- Success: node-id=12638:30587 / node-id=16657:54218 (status=complete, type=payment)
- Error: node-id=12638:30631 / node-id=16657:54219 (status=error, type=payment)
- Pending: node-id=16657:54021 (status=pending, type=payment)
"""

from __future__ import annotations

from typing import Literal, TypedDict

import pydivkit as dk

from smarty_ui.composites.status_bubble import (
    status_bubble,
    payment_success_bubble,
    payment_error_bubble,
    payment_pending_bubble,
)
from smarty_ui.tokens.theme import Theme, default_theme

PaymentStatus = Literal["success", "error", "pending"]


class PaymentData(TypedDict, total=False):
    """Data structure for a payment."""

    amount: str
    recipient: str
    card_info: str
    icon_url: str | None
    currency: str
    status: PaymentStatus
    action_url: str | None


def payment_status_widget(
    amount: str,
    recipient: str,
    card_info: str,
    *,
    status: PaymentStatus = "success",
    icon_url: str | None = None,
    currency: str = "сум",
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Payment status widget for displaying payment results.

    A colored card showing the payment status with:
    - Status label (e.g., "Оплата успешно выполнена", "Ошибка при оплате")
    - Merchant/service icon in circular container
    - Amount in large text
    - Recipient/phone number
    - Card info

    Design specs (from Figma):
    - Width: 350px
    - Height: 142px
    - Corner radius: 16px
    - Success: Blue gradient background (#076AE2 to #035DCA)
    - Error: Red solid background (#FF516E)
    - Pending: Grey solid background (#80858E)
    - Icon: 48px circular container with merchant logo

    Args:
        amount: Formatted amount string (e.g., "20 000")
        recipient: Recipient info (e.g., "Номер: +998 (90) 988-23-03")
        card_info: Card info (e.g., "Карта списания: ·· 6847")
        status: Payment status - "success", "error", or "pending"
        icon_url: Optional merchant/service icon URL
        currency: Currency suffix (default "сум")
        action_url: Optional URL to navigate on tap
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the payment status widget
    """
    return status_bubble(
        amount,
        status=status,
        bubble_type="payment",
        recipient=recipient,
        card_info=card_info,
        icon_url=icon_url,
        currency=currency,
        action_url=action_url,
        theme=theme,
    )


def payment_success_widget(
    amount: str,
    recipient: str,
    card_info: str,
    *,
    icon_url: str | None = None,
    currency: str = "сум",
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Successful payment widget.

    Shows a blue gradient card indicating the payment was completed.

    Args:
        amount: Formatted amount string
        recipient: Recipient/phone number
        card_info: Card info
        icon_url: Optional merchant/service icon URL
        currency: Currency suffix
        action_url: Optional tap action URL
        theme: Theme tokens to use

    Returns:
        A payment status widget showing success
    """
    return payment_success_bubble(
        amount,
        recipient,
        card_info,
        icon_url=icon_url,
        currency=currency,
        action_url=action_url,
        theme=theme,
    )


def payment_failed_widget(
    amount: str,
    recipient: str,
    card_info: str,
    *,
    icon_url: str | None = None,
    currency: str = "сум",
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Failed payment widget.

    Shows a red card indicating the payment failed.

    Args:
        amount: Formatted amount string
        recipient: Recipient/phone number
        card_info: Card info
        icon_url: Optional merchant/service icon URL
        currency: Currency suffix
        action_url: Optional tap action URL
        theme: Theme tokens to use

    Returns:
        A payment status widget showing error
    """
    return payment_error_bubble(
        amount,
        recipient,
        card_info,
        icon_url=icon_url,
        currency=currency,
        action_url=action_url,
        theme=theme,
    )


def payment_pending_widget(
    amount: str,
    recipient: str,
    card_info: str,
    *,
    icon_url: str | None = None,
    currency: str = "сум",
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Pending payment widget.

    Shows a grey card indicating the payment is pending.

    Args:
        amount: Formatted amount string
        recipient: Recipient/phone number
        card_info: Card info
        icon_url: Optional merchant/service icon URL
        currency: Currency suffix
        action_url: Optional tap action URL
        theme: Theme tokens to use

    Returns:
        A payment status widget showing pending
    """
    return payment_pending_bubble(
        amount,
        recipient,
        card_info,
        icon_url=icon_url,
        currency=currency,
        action_url=action_url,
        theme=theme,
    )


def payment_widget_from_data(
    data: PaymentData,
    *,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Create a payment widget from a data dictionary.

    Args:
        data: Payment data dictionary
        theme: Theme tokens to use

    Returns:
        A payment status widget
    """
    return payment_status_widget(
        amount=data.get("amount", "0"),
        recipient=data.get("recipient", ""),
        card_info=data.get("card_info", ""),
        status=data.get("status", "success"),
        icon_url=data.get("icon_url"),
        currency=data.get("currency", "сум"),
        action_url=data.get("action_url"),
        theme=theme,
    )
