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

Provides a card container for displaying service/payment lists with
a title and multiple service items.
"""

from __future__ import annotations

from typing import TypedDict

import pydivkit as dk

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


class ServiceItemData(TypedDict, total=False):
    """Data structure for a service list item."""

    title: str
    icon_url: str | None
    action_url: str | None


def service_list(
    title: str,
    items: list[ServiceItemData],
    *,
    corner_radius: int | None = None,
    padding_horizontal: int = 16,
    padding_top: int = 16,
    padding_bottom: int = 8,
    item_spacing: int = 24,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Service list card component matching Smartbank Figma design.

    A white card container with:
    - Title header (Controls 01 - Bold 16px)
    - List of service items with icons and dividers
    - Rounded corners and shadow

    Matches the Figma design for "Оплата услуг" (Payment Services) list.

    Args:
        title: Card title text (e.g., "Оплата услуг")
        items: List of service item data dicts with keys:
            - title: Service name
            - icon_url: Optional icon URL
            - action_url: Optional tap action URL
        corner_radius: Border radius, defaults to 20
        padding_horizontal: Horizontal padding (default 16)
        padding_top: Top padding (default 16)
        padding_bottom: Bottom padding (default 8)
        item_spacing: Vertical spacing between items (default 24)
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the service list card
    """
    radius = corner_radius if corner_radius is not None else 20

    # Get typography style for title - Controls 01 Bold 16px
    controls_style = theme.typography.get_controls_1(bold=True)

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

    # Title
    content_items.append(
        dk.DivText(
            text=title,
            font_size=controls_style.size,
            font_weight=dk.DivFontWeight(controls_style.weight),
            text_color=theme.colors.text_primary,
            line_height=controls_style.line_height,
            width=dk.DivMatchParentSize(),
        )
    )

    # Service items
    total_items = len(items)
    for i, item_data in enumerate(items):
        is_last = i == total_items - 1

        # Create service list item
        item = service_list_item(
            title=item_data.get("title", ""),
            icon_url=item_data.get("icon_url"),
            show_divider=not is_last,  # No divider on last item
            action_url=item_data.get("action_url"),
            theme=theme,
        )

        # Add spacing before item (except first)
        if i == 0:
            # First item gets margin from title
            item = dk.DivContainer(
                orientation=dk.DivContainerOrientation.VERTICAL,
                width=dk.DivMatchParentSize(),
                margins=dk.DivEdgeInsets(top=item_spacing),
                items=[item],
            )
        else:
            # Subsequent items get spacing
            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=radius),
        background=[dk.DivSolidBackground(color=theme.colors.background)],
        paddings=dk.DivEdgeInsets(
            left=padding_horizontal,
            right=padding_horizontal,
            top=padding_top,
            bottom=padding_bottom,
        ),
        items=content_items,
    )


def service_list_from_data(
    title: str,
    services: list[tuple[str, str | None, str | None]],
    *,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Convenience function to create a service list from tuples.

    Args:
        title: Card title text
        services: List of tuples (title, icon_url, action_url)
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the service list card

    Example:
        service_list_from_data(
            "Оплата услуг",
            [
                ("Телефония", "https://...", "smartbank://services/phone"),
                ("Интернет", "https://...", "smartbank://services/internet"),
                ("Услуги", "https://...", None),
            ],
        )
    """
    items: list[ServiceItemData] = [
        {"title": name, "icon_url": icon, "action_url": action}
        for name, icon, action in services
    ]
    return service_list(title, items, theme=theme)
