"""Card blocks aligned with Smartbank Figma design system.

Provides bank card components for displaying debit/credit cards with
balance, card details, and provider logos.
"""

from __future__ import annotations

from typing import Literal

import pydivkit as dk

from smarty_ui.tokens.theme import Theme, default_theme

CardSize = Literal["big", "small"]
CardProvider = Literal["humo", "uzcard", "visa", "mastercard"]


def bank_card(
    balance: str,
    card_number: str,
    card_name: str,
    *,
    bg_image_url: str | None = None,
    provider_logo_url: str | None = None,
    bank_logo_url: str | None = None,
    currency: str = "сум",
    size: CardSize = "big",
    show_favorite: bool = False,
    favorite_icon_url: str | None = None,
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Bank card component matching Smartbank Figma design.

    Displays a bank card with:
    - Background image
    - Balance amount (Title 01 style)
    - Card number and name (Caption 01 style)
    - Provider logo (Humo, Uzcard, etc.)
    - Optional favorite/star icon
    - Bank logo

    Args:
        balance: Formatted balance string (e.g., "22 653 266")
        card_number: Last 4 digits of card (e.g., "6847")
        card_name: Card name/type (e.g., "Зарплатная")
        bg_image_url: URL to card background image
        provider_logo_url: URL to payment provider logo (Humo, Uzcard, etc.)
        bank_logo_url: URL to bank logo
        currency: Currency suffix (default "сум")
        size: "big" (300x190) or "small" (152x96)
        show_favorite: Whether to show favorite/star icon
        favorite_icon_url: URL to favorite icon image
        action_url: Optional URL to navigate on card tap
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the bank card
    """
    # Get dimensions based on size
    if size == "big":
        width = theme.spacing.card_width_big  # 300
        height = theme.spacing.card_height_big  # 190
        corner_radius = theme.spacing.radius_lg  # 18
        balance_style = theme.typography.get_title_1()
        caption_style = theme.typography.get_caption_1()
        padding = theme.spacing.card_padding  # 20
    else:
        width = theme.spacing.card_width_small  # 152
        height = theme.spacing.card_height_small  # 96
        corner_radius = theme.spacing.radius_md  # 14
        balance_style = theme.typography.get_title_2()
        caption_style = theme.typography.get_caption_2()
        padding = 12

    # Build items list
    items: list[dk.Div] = []

    # Background image (absolute positioned)
    if bg_image_url:
        items.append(
            dk.DivImage(
                image_url=bg_image_url,
                width=dk.DivMatchParentSize(),
                height=dk.DivMatchParentSize(),
                scale=dk.DivImageScale.FILL,
            )
        )

    # Content overlay container
    content_items: list[dk.Div] = []

    # Balance text
    content_items.append(
        dk.DivText(
            text=f"{balance} {currency}",
            font_size=balance_style.size,
            font_weight=dk.DivFontWeight(balance_style.weight),
            text_color=theme.colors.text_on_primary,
            line_height=balance_style.line_height,
            width=dk.DivWrapContentSize(),
        )
    )

    # Card number and name row
    card_details = f"{card_number}  |  {card_name}"
    content_items.append(
        dk.DivText(
            text=card_details,
            font_size=caption_style.size,
            font_weight=dk.DivFontWeight(caption_style.weight),
            text_color=theme.colors.text_on_primary,
            line_height=caption_style.line_height,
            width=dk.DivWrapContentSize(),
            margins=dk.DivEdgeInsets(top=4),
        )
    )

    # Main content container (top-left aligned)
    items.append(
        dk.DivContainer(
            orientation=dk.DivContainerOrientation.VERTICAL,
            width=dk.DivMatchParentSize(),
            height=dk.DivMatchParentSize(),
            paddings=dk.DivEdgeInsets(
                left=padding,
                top=padding,
                right=padding,
                bottom=padding,
            ),
            items=content_items,
        )
    )

    # Favorite icon (top-right)
    if show_favorite and favorite_icon_url:
        items.append(
            dk.DivImage(
                image_url=favorite_icon_url,
                width=dk.DivFixedSize(value=24),
                height=dk.DivFixedSize(value=24),
                alignment_horizontal=dk.DivAlignmentHorizontal.RIGHT,
                alignment_vertical=dk.DivAlignmentVertical.TOP,
                margins=dk.DivEdgeInsets(top=16, right=16),
            )
        )

    # Provider logo (bottom-right)
    if provider_logo_url:
        items.append(
            dk.DivImage(
                image_url=provider_logo_url,
                width=dk.DivFixedSize(value=54),
                height=dk.DivFixedSize(value=14),
                alignment_horizontal=dk.DivAlignmentHorizontal.RIGHT,
                alignment_vertical=dk.DivAlignmentVertical.BOTTOM,
                margins=dk.DivEdgeInsets(bottom=padding, right=padding),
                scale=dk.DivImageScale.FIT,
            )
        )

    # Bank logo (bottom-left)
    if bank_logo_url:
        items.append(
            dk.DivImage(
                image_url=bank_logo_url,
                width=dk.DivFixedSize(value=100),
                height=dk.DivFixedSize(value=20),
                alignment_horizontal=dk.DivAlignmentHorizontal.LEFT,
                alignment_vertical=dk.DivAlignmentVertical.BOTTOM,
                margins=dk.DivEdgeInsets(bottom=padding, left=padding + 6),
                scale=dk.DivImageScale.FIT,
            )
        )

    # Build card container
    card_kwargs: dict = {
        "orientation": dk.DivContainerOrientation.OVERLAP,
        "width": dk.DivFixedSize(value=width),
        "height": dk.DivFixedSize(value=height),
        "border": dk.DivBorder(corner_radius=corner_radius),
        "items": items,
    }

    # Add action if provided
    if action_url:
        card_kwargs["actions"] = [dk.DivAction(log_id="bank_card_tap", url=action_url)]

    return dk.DivContainer(**card_kwargs)


def simple_card(
    *,
    title: str | None = None,
    subtitle: str | None = None,
    bg_color: str | None = None,
    corner_radius: int | None = None,
    padding: int | None = None,
    action_url: str | None = None,
    children: list[dk.Div] | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Simple card container with optional title and content.

    A flexible card component for general content display.

    Args:
        title: Optional card title (Title 02 style)
        subtitle: Optional subtitle (Text 02 style)
        bg_color: Background color, defaults to surface
        corner_radius: Border radius, defaults to radius_lg (18)
        padding: Inner padding, defaults to card_padding (20)
        action_url: Optional URL for card tap action
        children: List of child elements to render in the card
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the card
    """
    radius = corner_radius if corner_radius is not None else theme.spacing.radius_lg
    pad = padding if padding is not None else theme.spacing.card_padding

    items: list[dk.Div] = []

    # Add title if provided
    if title:
        title_style = theme.typography.get_title_2()
        items.append(
            dk.DivText(
                text=title,
                font_size=title_style.size,
                font_weight=dk.DivFontWeight(title_style.weight),
                text_color=theme.colors.text_primary,
                line_height=title_style.line_height,
                width=dk.DivMatchParentSize(),
            )
        )

    # Add subtitle if provided
    if subtitle:
        text_style = theme.typography.get_text_2()
        items.append(
            dk.DivText(
                text=subtitle,
                font_size=text_style.size,
                font_weight=dk.DivFontWeight(text_style.weight),
                text_color=theme.colors.text_secondary,
                line_height=text_style.line_height,
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(top=4) if title else None,
            )
        )

    # Add children
    if children:
        for child in children:
            items.append(child)

    card_kwargs: dict = {
        "orientation": dk.DivContainerOrientation.VERTICAL,
        "width": dk.DivMatchParentSize(),
        "border": dk.DivBorder(corner_radius=radius),
        "paddings": dk.DivEdgeInsets(left=pad, right=pad, top=pad, bottom=pad),
        "background": [dk.DivSolidBackground(color=bg_color or theme.colors.surface)],
        "items": items,
    }

    if action_url:
        card_kwargs["actions"] = [
            dk.DivAction(log_id="simple_card_tap", url=action_url)
        ]

    return dk.DivContainer(**card_kwargs)
