"""Card balance item composite aligned with Smartbank Figma design system.

Provides card balance display with thumbnail, amount, and card name.
"""

from __future__ import annotations

import pydivkit as dk

from smarty_ui.tokens.theme import Theme, default_theme


def card_balance_item(
    balance: str,
    card_name: str,
    *,
    card_thumbnail_url: str | None = None,
    card_last_digits: str | None = None,
    currency: str = "сум",
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Card balance item showing card thumbnail, balance, and name.

    Displays a horizontal row with:
    - Card thumbnail image (62x25) with last 4 digits overlay
    - Balance amount (Controls 02 Bold 14px)
    - Card name (Text 02 Medium 12px, grey)

    Args:
        balance: Formatted balance string (e.g., "12 489 650")
        card_name: Card name/type (e.g., "Зарплатная Smartbank")
        card_thumbnail_url: URL to card thumbnail image
        card_last_digits: Last 4 digits of card (e.g., "6847")
        currency: Currency suffix (default "сум")
        action_url: Optional URL to navigate on tap
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the card balance item
    """
    # Get typography styles
    balance_style = theme.typography.get_controls_2(bold=True)  # Bold 14px
    name_style = theme.typography.get_text_2()  # Medium 12px

    items: list[dk.Div] = []

    # Card thumbnail with last digits overlay
    if card_thumbnail_url:
        thumbnail_items: list[dk.Div] = [
            dk.DivImage(
                image_url=card_thumbnail_url,
                width=dk.DivMatchParentSize(),
                height=dk.DivMatchParentSize(),
                scale=dk.DivImageScale.FILL,
            )
        ]

        # Add last digits overlay if provided
        if card_last_digits:
            thumbnail_items.append(
                dk.DivContainer(
                    orientation=dk.DivContainerOrientation.HORIZONTAL,
                    content_alignment_vertical=dk.DivAlignmentVertical.CENTER,
                    width=dk.DivWrapContentSize(),
                    height=dk.DivWrapContentSize(),
                    alignment_horizontal=dk.DivAlignmentHorizontal.LEFT,
                    alignment_vertical=dk.DivAlignmentVertical.BOTTOM,
                    margins=dk.DivEdgeInsets(left=4, bottom=4),
                    items=[
                        # Bullet dot
                        dk.DivText(
                            text="•",
                            font_size=10,
                            text_color=theme.colors.text_on_primary,
                            width=dk.DivWrapContentSize(),
                        ),
                        # Card digits
                        dk.DivText(
                            text=f" {card_last_digits}",
                            font_size=10,
                            font_weight=dk.DivFontWeight("medium"),
                            text_color=theme.colors.text_on_primary,
                            line_height=14,
                            width=dk.DivWrapContentSize(),
                        ),
                    ],
                )
            )

        thumbnail = dk.DivContainer(
            orientation=dk.DivContainerOrientation.OVERLAP,
            width=dk.DivFixedSize(value=62),
            height=dk.DivFixedSize(value=25),
            border=dk.DivBorder(corner_radius=8),
            items=thumbnail_items,
        )
        items.append(thumbnail)

    # Text content (balance + card name)
    text_items: list[dk.Div] = [
        # Balance
        dk.DivText(
            text=f"{balance} {currency}",
            font_size=balance_style.size,
            font_weight=dk.DivFontWeight(balance_style.weight),
            text_color=theme.colors.text_primary,
            line_height=balance_style.line_height,
            width=dk.DivMatchParentSize(),
        ),
        # Card name
        dk.DivText(
            text=card_name,
            font_size=name_style.size,
            font_weight=dk.DivFontWeight(name_style.weight),
            text_color=theme.colors.text_secondary,
            line_height=name_style.line_height,
            width=dk.DivMatchParentSize(),
            margins=dk.DivEdgeInsets(top=2),
        ),
    ]

    text_container = dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(weight=1),
        margins=dk.DivEdgeInsets(left=12) if card_thumbnail_url else None,
        items=text_items,
    )
    items.append(text_container)

    # Build row container
    row_kwargs: dict = {
        "orientation": dk.DivContainerOrientation.HORIZONTAL,
        "content_alignment_vertical": dk.DivAlignmentVertical.CENTER,
        "width": dk.DivMatchParentSize(),
        "items": items,
    }

    if action_url:
        row_kwargs["actions"] = [
            dk.DivAction(log_id=f"card_balance_{card_name}", url=action_url)
        ]

    return dk.DivContainer(**row_kwargs)
