use uuid::Uuid;

use super::patterns::{mention_bracket_re, mention_tag_re, user_mention_re};

/// Extracts user UUIDs from all mention formats:
/// - Legacy: `<user>uuid</user>`
/// - Legacy: `<mention type="user" id="uuid">label</mention>`
/// - New: `@[user:uuid:label]`
pub fn extract_mentions(content: &str) -> Vec<Uuid> {
    let mut mentioned = Vec::new();

    for cap in user_mention_re().captures_iter(content) {
        if let Some(inner) = cap.get(1) {
            let token = inner.as_str().trim();
            if let Ok(uuid) = Uuid::parse_str(token) {
                if !mentioned.contains(&uuid) {
                    mentioned.push(uuid);
                }
            }
        }
    }

    for cap in mention_tag_re().captures_iter(content) {
        if let (Some(type_m), Some(id_m)) = (cap.get(1), cap.get(2)) {
            if type_m.as_str() == "user" {
                if let Ok(uuid) = Uuid::parse_str(id_m.as_str().trim()) {
                    if !mentioned.contains(&uuid) {
                        mentioned.push(uuid);
                    }
                }
            }
        }
    }

    for cap in mention_bracket_re().captures_iter(content) {
        if let (Some(type_m), Some(id_m)) = (cap.get(1), cap.get(2)) {
            if type_m.as_str() == "user" {
                if let Ok(uuid) = Uuid::parse_str(id_m.as_str().trim()) {
                    if !mentioned.contains(&uuid) {
                        mentioned.push(uuid);
                    }
                }
            }
        }
    }

    mentioned
}
