"""Utility helpers for YAML Doctor."""

from __future__ import annotations


def severity_icon(sev) -> str:
    val = sev.value if hasattr(sev, "value") else str(sev)
    return {
        "critical": "🔴",
        "high": "🟠",
        "medium": "🟡",
        "low": "🔵",
        "info": "⚪",
    }.get(val, "⚪")


def grade_color(grade) -> str:
    val = grade.value if hasattr(grade, "value") else str(grade)
    return {
        "A+": "green",
        "A": "green",
        "B": "yellow",
        "C": "yellow",
        "D": "red",
        "F": "red",
    }.get(val, "white")


def score_bar(score: int, width: int = 20) -> str:
    filled = int(score / 100 * width)
    empty = width - filled
    if score >= 85:
        color = "green"
    elif score >= 65:
        color = "yellow"
    else:
        color = "red"
    return f"[{color}]{'█' * filled}{'░' * empty}[/{color}]"


def truncate(text: str, max_len: int = 80) -> str:
    if len(text) <= max_len:
        return text
    return text[:max_len] + "…"
