"""Contact item composite aligned with Smartbank Figma design system.

Provides contact display with numbered avatar, name, and phone number.
"""

from __future__ import annotations

import pydivkit as dk

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


def contact_item(
    name: str,
    phone: str,
    *,
    index: int | None = None,
    avatar_url: str | None = None,
    show_divider: bool = True,
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Contact item showing avatar/number, name, and phone.

    Displays a horizontal row with:
    - Numbered avatar or image avatar (32x32)
    - Name (Text 03 Bold 12px)
    - Phone number (Text 02 Medium 12px, below name)
    - Optional divider line

    Args:
        name: Contact name (e.g., "Роман Горбунов #1")
        phone: Phone number (e.g., "+998 (90) 123–45–67")
        index: Optional index number for numbered avatar
        avatar_url: Optional URL to avatar image (overrides index)
        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 contact item
    """
    # Get typography styles
    name_style = theme.typography.get_text_3()  # Bold 12px
    phone_style = theme.typography.get_text_2()  # Medium 12px

    row_items: list[dk.Div] = []

    # Avatar (numbered or image)
    if avatar_url:
        avatar = dk.DivImage(
            image_url=avatar_url,
            width=dk.DivFixedSize(value=32),
            height=dk.DivFixedSize(value=32),
            border=dk.DivBorder(corner_radius=16),
            scale=dk.DivImageScale.FILL,
        )
        row_items.append(avatar)
    elif index is not None:
        avatar = numbered_avatar(index, size=32, theme=theme)
        row_items.append(avatar)

    # Text content (name + phone)
    text_items: list[dk.Div] = [
        # Name
        dk.DivText(
            text=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(),
        ),
        # Phone
        dk.DivText(
            text=phone,
            font_size=phone_style.size,
            font_weight=dk.DivFontWeight(phone_style.weight),
            text_color=theme.colors.text_primary,
            line_height=phone_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=8) if (avatar_url or index is not None) else None,
        items=text_items,
    )
    row_items.append(text_container)

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

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

    row = dk.DivContainer(**row_kwargs)

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

    # Wrap with divider
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        items=[
            row,
            dk.DivSeparator(
                delimiter_style=dk.DivSeparatorDelimiterStyle(
                    color=theme.colors.border,  # #EBF2FA
                ),
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(
                    top=12,
                    left=40,  # Align with text (32 avatar + 8 margin)
                ),
            ),
        ],
    )
