"""
Display formatting utilities for the NEXUS AUTOPARTS console application.

Functions for currency, numbers, text truncation, table layout, and
quality-tier visual bars.
"""


def format_currency(value) -> str:
    """Format a numeric value as USD currency.

    None  -> '──'
    0     -> '$0.00'
    45.99 -> '$45.99'
    """
    if value is None:
        return "──"
    return f"${value:,.2f}"


def format_number(value) -> str:
    """Format an integer with thousands separators.

    None  -> '0'
    13685 -> '13,685'
    """
    if value is None:
        return "0"
    return f"{value:,}"


def truncate(text, max_len) -> str:
    """Truncate text to *max_len* characters, appending '...' if trimmed.

    None            -> ''
    fits            -> text unchanged
    too long        -> text[:max_len-3] + '...'
    """
    if text is None:
        return ""
    if len(text) <= max_len:
        return text
    return text[: max_len - 3] + "..."


def pad_right(text, width) -> str:
    """Pad *text* to *width* with spaces on the right, or truncate if longer.

    None     -> ''
    fits     -> ljust(width)
    too long -> text[:width]
    """
    if text is None:
        return ""
    if len(text) > width:
        return text[:width]
    return text.ljust(width)


def format_table_row(values, widths, separator=" │ ") -> str:
    """Join *values* padded to corresponding *widths* with *separator*.

    Each value is passed through :func:`pad_right` to ensure uniform column
    widths, then all columns are joined by the separator string.
    """
    cells = [pad_right(str(v), w) for v, w in zip(values, widths)]
    return separator.join(cells)


# ── Quality-tier bars ──────────────────────────────────────────────────

_QUALITY_BARS = {
    "oem":      "███████████",
    "premium":  "██████████░",
    "standard": "███████░░░░",
    "economy":  "█████░░░░░░",
}


def quality_bar(tier) -> str:
    """Return a Unicode block-bar representing a quality tier.

    Recognised tiers: oem, premium, standard, economy.
    Unknown tiers fall back to a minimal bar.
    """
    return _QUALITY_BARS.get(tier, "░░░░░░░░░░░")
