//! Short label and traffic-light indicator helpers for pipeline status display.

/// Format a short display label for a work item.
///
/// Extracts the leading numeric ID and optional type tag from the file stem
/// (e.g. `"293"` and `"story"` from `"293_story_register_all_bot_commands"`)
/// and combines them with the human-readable name from the front matter when
/// available. Known types (`story`, `bug`, `spike`, `refactor`) are shown as
/// bracketed labels; unknown or missing types are omitted silently.
///
/// Examples:
/// - `("293_story_foo", Some("Register all bot commands"))` → `"293 [story] — Register all bot commands"`
/// - `("375_bug_foo", None)` → `"375 [bug]"`
/// - `("293_story_foo", None)` → `"293 [story]"`
/// - `("no_number_here", None)` → `"no_number_here"`
pub(crate) fn story_short_label(stem: &str, name: Option<&str>) -> String {
    let mut parts = stem.splitn(3, '_');
    let first = parts.next().unwrap_or(stem);
    let (number, type_label) = if !first.is_empty() && first.chars().all(|c| c.is_ascii_digit()) {
        let t = parts.next().and_then(|t| match t {
            "story" | "bug" | "spike" | "refactor" => Some(t),
            _ => None,
        });
        (first, t)
    } else {
        (stem, None)
    };
    let prefix = match type_label {
        Some(t) => format!("{number} [{t}]"),
        None => number.to_string(),
    };
    match name {
        Some(n) => format!("{prefix} — {n}"),
        None => prefix,
    }
}

/// Choose the traffic-light indicator for a work item.
///
/// Priority: blocked > throttled > running > idle.
/// Uses coloured emoji so indicators render natively in all Matrix clients
/// (Element X and others do not support `<font data-mx-color>` HTML tags).
///
/// - 🔴 hard-blocked (retry limit exceeded)
/// - 🟠 throttled (rate-limit warning received)
/// - 🟢 running normally (active agent, no throttle)
/// - ⚪ idle / no active agent
pub(crate) fn traffic_light_dot(blocked: bool, throttled: bool, has_agent: bool) -> &'static str {
    if blocked {
        "\u{1F534} " // 🔴 — hard blocked
    } else if throttled {
        "\u{1F7E0} " // 🟠 — throttled
    } else if has_agent {
        "\u{1F7E2} " // 🟢 — running normally
    } else {
        "\u{26AA} " // ⚪ — idle / no agent
    }
}
