"""Receiver card item composite aligned with Smartbank Figma design system.

Provides card holder display with name, provider logo, and card number.
"""

from __future__ import annotations

import pydivkit as dk

from smarty_ui.tokens.theme import Theme, default_theme


def receiver_card_item(
    holder_name: str,
    card_last_digits: str,
    *,
    provider_logo_url: str | None = None,
    show_divider: bool = True,
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Receiver card item showing holder name, provider logo, and card number.

    Displays a vertical layout with:
    - Card holder name (Caption 01 SemiBold 12px)
    - Provider logo + bullet + last 4 digits row
    - Optional divider line

    Args:
        holder_name: Card holder name (e.g., "ALEKSAND GURYANOV")
        card_last_digits: Last 4 digits of card (e.g., "2269")
        provider_logo_url: URL to payment provider logo (Humo, Uzcard, etc.)
        show_divider: Whether to show bottom divider line (default True)
        action_url: Optional URL to navigate on tap
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the receiver card item
    """
    # Get typography styles
    name_style = theme.typography.get_caption_1()  # SemiBold 12px

    content_items: list[dk.Div] = []

    # Card holder name
    content_items.append(
        dk.DivText(
            text=holder_name,
            font_size=name_style.size,
            font_weight=dk.DivFontWeight(name_style.weight),
            text_color=theme.colors.text_primary,
            line_height=name_style.line_height,
            width=dk.DivMatchParentSize(),
        )
    )

    # Provider logo + card number row
    card_info_items: list[dk.Div] = []

    if provider_logo_url:
        card_info_items.append(
            dk.DivImage(
                image_url=provider_logo_url,
                width=dk.DivFixedSize(value=36),
                height=dk.DivFixedSize(value=10),
                scale=dk.DivImageScale.FIT,
            )
        )

    # Bullet + card digits
    card_info_items.append(
        dk.DivContainer(
            orientation=dk.DivContainerOrientation.HORIZONTAL,
            content_alignment_vertical=dk.DivAlignmentVertical.CENTER,
            width=dk.DivWrapContentSize(),
            margins=dk.DivEdgeInsets(left=8) if provider_logo_url else None,
            items=[
                dk.DivText(
                    text="••",
                    font_size=10,
                    text_color=theme.colors.text_primary,
                    width=dk.DivWrapContentSize(),
                ),
                dk.DivText(
                    text=f" {card_last_digits}",
                    font_size=10,
                    font_weight=dk.DivFontWeight("medium"),
                    text_color=theme.colors.text_primary,
                    line_height=14,
                    width=dk.DivWrapContentSize(),
                ),
            ],
        )
    )

    card_info_row = dk.DivContainer(
        orientation=dk.DivContainerOrientation.HORIZONTAL,
        content_alignment_vertical=dk.DivAlignmentVertical.CENTER,
        width=dk.DivMatchParentSize(),
        margins=dk.DivEdgeInsets(top=6),
        items=card_info_items,
    )
    content_items.append(card_info_row)

    # Build content container
    content_kwargs: dict = {
        "orientation": dk.DivContainerOrientation.VERTICAL,
        "width": dk.DivMatchParentSize(),
        "items": content_items,
    }

    if action_url:
        content_kwargs["actions"] = [
            dk.DivAction(log_id=f"receiver_card_{holder_name}", url=action_url)
        ]

    content = dk.DivContainer(**content_kwargs)

    # If no divider, return just the content
    if not show_divider:
        return content

    # Wrap with divider
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        items=[
            content,
            dk.DivSeparator(
                delimiter_style=dk.DivSeparatorDelimiterStyle(
                    color=theme.colors.border,  # #EBF2FA
                ),
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(top=16),
            ),
        ],
    )
