"""Receiver cards list block aligned with Smartbank Figma design system.

Provides a card container for displaying receiver/beneficiary cards.
"""

from __future__ import annotations

from typing import TypedDict

import pydivkit as dk

from smarty_ui.composites.receiver_card_item import receiver_card_item
from smarty_ui.tokens.theme import Theme, default_theme


class ReceiverCardData(TypedDict, total=False):
    """Data structure for a receiver card item."""

    holder_name: str
    card_last_digits: str
    provider_logo_url: str | None
    action_url: str | None


def receiver_cards_list(
    cards: list[ReceiverCardData],
    *,
    corner_radius: int = 8,
    padding: int = 16,
    item_spacing: int = 16,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Receiver cards list component matching Smartbank Figma design.

    A white card container with:
    - List of receiver card items (holder name + provider + card number)
    - Dividers between items
    - Rounded corners and shadow

    Args:
        cards: List of receiver card data dicts with keys:
            - holder_name: Card holder name
            - card_last_digits: Last 4 digits of card
            - provider_logo_url: Optional provider logo URL
            - action_url: Optional tap action URL
        corner_radius: Border radius (default 8)
        padding: Container padding (default 16)
        item_spacing: Vertical spacing between items (default 16)
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the receiver cards list
    """
    content_items: list[dk.Div] = []
    total_cards = len(cards)

    for i, card_data in enumerate(cards):
        is_last = i == total_cards - 1

        item = receiver_card_item(
            holder_name=card_data.get("holder_name", ""),
            card_last_digits=card_data.get("card_last_digits", ""),
            provider_logo_url=card_data.get("provider_logo_url"),
            show_divider=not is_last,
            action_url=card_data.get("action_url"),
            theme=theme,
        )

        # Add spacing before item (except first)
        if i > 0:
            item = dk.DivContainer(
                orientation=dk.DivContainerOrientation.VERTICAL,
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(top=item_spacing),
                items=[item],
            )

        content_items.append(item)

    # Build card container
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        border=dk.DivBorder(
            corner_radius=corner_radius,
            stroke=dk.DivStroke(color=theme.colors.grey_light, width=1),
        ),
        background=[dk.DivSolidBackground(color=theme.colors.background)],
        paddings=dk.DivEdgeInsets(
            left=padding,
            right=padding,
            top=padding,
            bottom=padding,
        ),
        items=content_items,
    )
