"""Avatar row molecule: avatar image + name + optional subtitle."""

from __future__ import annotations

import pydivkit as dk

from smarty_ui.primitives.image import avatar
from smarty_ui.primitives.text import label, caption
from smarty_ui._helpers import hstack
from smarty_ui.tokens.theme import Theme, default_theme


def avatar_row(
    image_url: str,
    name: str,
    *,
    subtitle: str | None = None,
    avatar_size: int = 40,
    gap: int = 12,
    action_url: str | None = None,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Horizontal avatar + name (+ optional subtitle) row.

    Common in chat lists, user profiles, comment headers, etc.
    """
    # Right-side text column
    text_items: list[dk.Div] = [
        label(name, weight=theme.typography.medium_weight, theme=theme),
    ]
    if subtitle:
        text_items.append(
            caption(subtitle, theme=theme),
        )

    text_column = dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        items=text_items,
    )

    row = hstack(
        avatar(image_url, size=avatar_size, theme=theme),
        text_column,
        gap=gap,
    )

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

    return row
