"""Contacts list block aligned with Smartbank Figma design system.

Provides a card container for displaying contact lists with numbered avatars.
"""

from __future__ import annotations

from typing import TypedDict

import pydivkit as dk

from smarty_ui.composites import assistant_bubble
from smarty_ui.composites.contact_item import contact_item
from smarty_ui.primitives import button, smarty_button
from smarty_ui.tokens.theme import Theme, default_theme


class ContactData(TypedDict, total=False):
    """Data structure for a contact item."""

    name: str
    phone: str
    avatar_url: str | None
    action_url: str | None


def search_for_contacts(
    names: list[str],
    *,
    theme: Theme = default_theme,
):
    yes_button = smarty_button(
        text="Search",
    )
    no_button = smarty_button(
        text="Cancel",
    )
    buttons = dk.DivContainer(
        orientation=dk.DivContainerOrientation.HORIZONTAL,
        width=dk.DivMatchParentSize(),
        items=[yes_button, no_button],
    )
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        items=[
            assistant_bubble(
                text="I need to find contacts by name for " + ", ".join(names)
            ),
            buttons,
        ],
    )


def contacts_list(
    contacts: list[ContactData],
    *,
    show_index: bool = True,
    corner_radius: int = 8,
    padding: int = 16,
    item_spacing: int = 12,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Contacts list component matching Smartbank Figma design.

    A white card container with:
    - List of contact items (numbered avatar + name + phone)
    - Dividers between items
    - Rounded corners and shadow

    Args:
        contacts: List of contact data dicts with keys:
            - name: Contact name
            - phone: Phone number
            - avatar_url: Optional avatar image URL
            - action_url: Optional tap action URL
        show_index: Whether to show numbered avatars (default True)
        corner_radius: Border radius (default 8)
        padding: Container padding (default 16)
        item_spacing: Vertical spacing between items (default 12)
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the contacts list
    """
    content_items: list[dk.Div] = []
    total_contacts = len(contacts)

    for i, contact_data in enumerate(contacts):
        is_last = i == total_contacts - 1

        item = contact_item(
            name=contact_data.get("name", ""),
            phone=contact_data.get("phone", ""),
            index=i + 1 if show_index and not contact_data.get("avatar_url") else None,
            avatar_url=contact_data.get("avatar_url"),
            show_divider=not is_last,
            action_url=contact_data.get("action_url"),
            theme=theme,
        )

        # Add spacing before item (except first)
        if i > 0:
            item = dk.DivContainer(
                orientation=dk.DivContainerOrientation.VERTICAL,
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(top=item_spacing),
                items=[item],
            )

        content_items.append(item)

    # Build card container
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        width=dk.DivMatchParentSize(),
        border=dk.DivBorder(
            corner_radius=corner_radius,
            stroke=dk.DivStroke(color=theme.colors.grey_light, width=1),
        ),
        background=[dk.DivSolidBackground(color=theme.colors.background)],
        paddings=dk.DivEdgeInsets(
            left=padding,
            right=padding,
            top=padding + 4,  # Extra top padding as per design
            bottom=padding,
        ),
        items=content_items,
    )
