"""Chat bubble composite aligned with Smartbank Figma design system.

Provides chat message bubble components for chatbot/assistant interfaces.
"""

from __future__ import annotations

from typing import Literal

import pydivkit as dk

from smarty_ui.tokens.theme import Theme, default_theme

ChatBubbleType = Literal["assistant", "user", "error"]


def chat_bubble(
    text: str,
    *,
    bubble_type: ChatBubbleType = "assistant",
    max_width: int | None = 248,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Chat message bubble component.

    A rounded container for displaying chat messages with different styles
    for assistant, user, and error messages.

    Styles:
    - assistant: Light blue background (50% opacity), rounded except top-left
    - user: Grey light background, rounded except top-right
    - error: Light blue background (50% opacity), rounded except top-left

    Args:
        text: Message text content
        bubble_type: Type of bubble - "assistant", "user", or "error"
        max_width: Maximum width of the bubble (default 248)
        theme: Theme tokens to use

    Returns:
        A DivContainer representing the chat bubble
    """
    # Get typography style - Text 01 Medium 14px
    text_style = theme.typography.get_text_1()

    # Determine background color and corner radius based on type
    if bubble_type == "user":
        # User messages: grey-light background, rounded except top-right
        bg_color = theme.colors.grey_light  # #F9F9F9 # blue-light with 50% opacity

        corners = dk.DivCornersRadius(
            top_left=16,
            top_right=0,
            bottom_left=16,
            bottom_right=16,
        )
    else:
        # Assistant/error messages: blue-light 50% opacity, rounded except top-left
        bg_color = "#e9ecef"  # e9ecef
        corners = dk.DivCornersRadius(
            top_left=0,
            top_right=16,
            bottom_left=16,
            bottom_right=16,
        )

    # Build text element
    text_element = dk.DivText(
        text=text,
        font_size=text_style.size,
        font_weight=dk.DivFontWeight(text_style.weight),
        text_color=theme.colors.text_primary,
        line_height=text_style.line_height,
        width=dk.DivWrapContentSize(
            constrained=True,
            max_size=dk.DivSizeUnitValue(
                value=max_width,
                unit=dk.DivSizeUnit.DP,
            ),
        )
        if max_width
        else dk.DivWrapContentSize(),
    )

    # Build bubble container
    return dk.DivContainer(
        orientation=dk.DivContainerOrientation.VERTICAL,
        content_alignment_horizontal=dk.DivContentAlignmentHorizontal.CENTER,
        content_alignment_vertical=dk.DivContentAlignmentVertical.CENTER,
        width=dk.DivWrapContentSize(),
        height=dk.DivWrapContentSize(),
        border=dk.DivBorder(corners_radius=corners),
        background=[dk.DivSolidBackground(color=bg_color)],
        paddings=dk.DivEdgeInsets(
            left=16,
            right=16,
            top=12,
            bottom=12,
        ),
        items=[text_element],
    )


def assistant_bubble(
    text: str,
    *,
    max_width: int | None = 248,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Convenience function for assistant chat bubble.

    Args:
        text: Message text content
        max_width: Maximum width of the bubble
        theme: Theme tokens to use

    Returns:
        A chat bubble styled for assistant messages
    """
    return chat_bubble(text, bubble_type="assistant", max_width=max_width, theme=theme)


def user_bubble(
    text: str,
    *,
    max_width: int | None = 248,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Convenience function for user chat bubble.

    Args:
        text: Message text content
        max_width: Maximum width of the bubble
        theme: Theme tokens to use

    Returns:
        A chat bubble styled for user messages
    """
    return chat_bubble(text, bubble_type="user", max_width=max_width, theme=theme)


def error_bubble(
    text: str,
    *,
    max_width: int | None = 248,
    theme: Theme = default_theme,
) -> dk.DivContainer:
    """Convenience function for error chat bubble.

    Args:
        text: Message text content
        max_width: Maximum width of the bubble
        theme: Theme tokens to use

    Returns:
        A chat bubble styled for error messages
    """
    return chat_bubble(text, bubble_type="error", max_width=max_width, theme=theme)
