use eidos_kernel::schema::Node;

/// Minimum phrase length for untrusted lexical mention edges.
const MIN_MENTIONABLE_CHARS: usize = 6;

/// A surface form is "mentionable" (distinctive enough to mean a real reference) when it's
/// a multi-word phrase of reasonable length -- precision over recall, to stay neat-not-noisy.
/// Single tokens (even long ones) are deliberately excluded for now (eval-gated to add later).
fn is_mentionable(form: &str) -> bool {
    form.chars().filter(|c| c.is_alphanumeric()).count() >= MIN_MENTIONABLE_CHARS
        && form.split_whitespace().count() >= 2
}

/// Build the mention index: a sorted, dedup'd list of (mentionable_form, node_id) pairs,
/// drawn from each node's title + aliases. Consumed by the inline Aho-Corasick scan in
/// `edges::build_edges`.
pub(super) fn mention_index(nodes: &[Node]) -> Vec<(String, String)> {
    let mut index = Vec::new();
    for node in nodes {
        for form in std::iter::once(&node.title).chain(node.aliases.iter()) {
            let form = form.trim().to_lowercase();
            if is_mentionable(&form) {
                index.push((form, node.id.clone()));
            }
        }
    }
    index.sort();
    index.dedup();
    index
}
