"""Text primitives aligned with Smartbank Figma design system.

Provides text components for all typography styles:
- Titles: title_1, title_2
- Controls: controls_1, controls_2
- Text: text_1, text_2, text_3
- Captions: caption_1, caption_2, caption_3
- Legacy: label, heading, paragraph, caption
"""

from __future__ import annotations

import pydivkit as dk

from smarty_ui.tokens.theme import Theme, default_theme


# =============================================================================
# Smartbank Typography Styles
# =============================================================================


def title_1(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Title 01 — Bold 26px.

    Primary heading style for main titles and large display text.
    """
    style = theme.typography.get_title_1()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def title_2(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Title 02 — Bold 20px.

    Secondary heading style for section titles.
    """
    style = theme.typography.get_title_2()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def controls_1(
    text: str,
    *,
    bold: bool = True,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Controls 01 — Bold/SemiBold 16px.

    Used for large button text and primary interactive elements.
    """
    style = theme.typography.get_controls_1(bold=bold)
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines,
        width=dk.DivWrapContentSize(),
    )


def controls_2(
    text: str,
    *,
    bold: bool = False,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Controls 02 — SemiBold/Bold 14px.

    Used for small button text and secondary interactive elements.
    """
    style = theme.typography.get_controls_2(bold=bold)
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines,
        width=dk.DivWrapContentSize(),
    )


def text_1(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Text 01 — Medium 14px.

    Primary body text style for paragraphs and descriptions.
    """
    style = theme.typography.get_text_1()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def text_2(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Text 02 — Medium 12px.

    Secondary body text for supporting content.
    """
    style = theme.typography.get_text_2()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_secondary,
        line_height=style.line_height,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def text_3(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Text 03 — Bold 12px.

    Emphasized small text for highlights.
    """
    style = theme.typography.get_text_3()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def caption_1(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Caption 01 — SemiBold 12px.

    Used for card details, labels, and metadata.
    """
    style = theme.typography.get_caption_1()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_secondary,
        line_height=style.line_height,
        max_lines=max_lines,
        width=dk.DivWrapContentSize(),
    )


def caption_2(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Caption 02 — Medium 10px.

    Small text for timestamps, hints, and fine print.
    """
    style = theme.typography.get_caption_2()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_secondary,
        line_height=style.line_height,
        max_lines=max_lines,
        width=dk.DivWrapContentSize(),
    )


def caption_3(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Caption 03 — Bold 10px.

    Emphasized small text for badges and tags.
    """
    style = theme.typography.get_caption_3()
    return dk.DivText(
        text=text,
        font_size=style.size,
        font_weight=dk.DivFontWeight(style.weight),
        text_color=color or theme.colors.text_primary,
        line_height=style.line_height,
        max_lines=max_lines,
        width=dk.DivWrapContentSize(),
    )


# =============================================================================
# Legacy Text Primitives (backward compatibility)
# =============================================================================


def label(
    text: str,
    *,
    size: int | None = None,
    color: str | None = None,
    max_lines: int = 1,
    weight: str = "regular",
    theme: Theme = default_theme,
) -> dk.DivText:
    """Single-line label -- the most basic text atom.

    Legacy function, prefer using text_1() or controls_2() for new code.
    """
    return dk.DivText(
        text=text,
        font_size=size or theme.typography.body_size,
        text_color=color or theme.colors.text_primary,
        max_lines=max_lines,
        font_weight=dk.DivFontWeight(weight),
        width=dk.DivWrapContentSize(),
    )


def heading(
    text: str,
    *,
    level: int = 1,
    color: str | None = None,
    max_lines: int = 0,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Heading text (h1-h4). ``level`` controls size.

    Legacy function, prefer using title_1() or title_2() for new code.
    """
    sizes = {
        1: theme.typography.h1_size,
        2: theme.typography.h2_size,
        3: theme.typography.h3_size,
        4: theme.typography.h4_size,
    }
    return dk.DivText(
        text=text,
        font_size=sizes.get(level, theme.typography.h3_size),
        font_weight=dk.DivFontWeight(theme.typography.bold_weight),
        text_color=color or theme.colors.text_primary,
        max_lines=max_lines if max_lines > 0 else None,
        width=dk.DivMatchParentSize(),
    )


def paragraph(
    text: str,
    *,
    size: int | None = None,
    color: str | None = None,
    max_lines: int = 0,
    line_height: int | None = None,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Multi-line body text.

    Legacy function, prefer using text_1() for new code.
    """
    return dk.DivText(
        text=text,
        font_size=size or theme.typography.body_size,
        text_color=color or theme.colors.text_primary,
        font_weight=dk.DivFontWeight(theme.typography.default_weight),
        max_lines=max_lines if max_lines > 0 else None,
        line_height=line_height or None,
        width=dk.DivMatchParentSize(),
    )


def caption(
    text: str,
    *,
    color: str | None = None,
    max_lines: int = 1,
    theme: Theme = default_theme,
) -> dk.DivText:
    """Small caption / helper text.

    Legacy function, prefer using caption_1() for new code.
    """
    return dk.DivText(
        text=text,
        font_size=theme.typography.caption_size,
        text_color=color or theme.colors.text_secondary,
        max_lines=max_lines,
        font_weight=dk.DivFontWeight(theme.typography.default_weight),
        width=dk.DivWrapContentSize(),
    )
