//! Emoji management for categories and tags
//!
//! Provides functions to get emojis for categories and tags from configuration

#[cfg(debug_assertions)]
use crate::categories::cache::ensure_fresh_cache;
use crate::categories::cache::load_meta_config;

/// Get emoji for a category from the loaded configuration
pub fn get_category_emoji(content_type: &str, language: &str, category: &str) -> String {
    #[cfg(debug_assertions)]
    ensure_fresh_cache();

    match load_meta_config(content_type, language) {
        Ok(config) => {
            // Try exact match first
            if let Some(emoji) = config.categories_emojis.get(category) {
                return emoji.clone();
            }

            // Try lowercase match
            let category_lower = category.to_lowercase();
            for (key, emoji) in &config.categories_emojis {
                if key.to_lowercase() == category_lower {
                    return emoji.clone();
                }
            }

            // Try partial match for tags (blog categories are often lowercase with dashes)
            for (key, emoji) in &config.categories_emojis {
                if category_lower.contains(&key.to_lowercase())
                    || key.to_lowercase().contains(&category_lower)
                {
                    return emoji.clone();
                }
            }

            // Default emoji if no match found
            get_default_category_emoji(content_type, category)
        }
        Err(_e) => {
            // During initial loading or if loading fails, provide sensible defaults
            get_default_category_emoji(content_type, category)
        }
    }
}

/// Get default emoji for a category when meta config is not available
/// Completely content-agnostic - uses generic emoji for all content types
fn get_default_category_emoji(_content_type: &str, _category: &str) -> String {
    // Use a generic document emoji for all content types
    // This eliminates hardcoded assumptions about specific content types
    "📄".to_string()
}

/// Get emoji for a tag from the loaded configuration
pub fn get_tag_emoji(content_type: &str, language: &str, tag: &str) -> String {
    #[cfg(debug_assertions)]
    ensure_fresh_cache();

    match load_meta_config(content_type, language) {
        Ok(config) => {
            // Try exact match first
            if let Some(emoji) = config.tags_emojis.get(tag) {
                return emoji.clone();
            }

            // Try lowercase match
            let tag_lower = tag.to_lowercase();
            for (key, emoji) in &config.tags_emojis {
                if key.to_lowercase() == tag_lower {
                    return emoji.clone();
                }
            }

            // Try partial match
            for (key, emoji) in &config.tags_emojis {
                if tag_lower.contains(&key.to_lowercase())
                    || key.to_lowercase().contains(&tag_lower)
                {
                    return emoji.clone();
                }
            }

            // Fallback to category emoji if tag not found
            get_default_tag_emoji(content_type, tag)
        }
        Err(_e) => {
            // During initial loading or if loading fails, provide sensible defaults
            get_default_tag_emoji(content_type, tag)
        }
    }
}

/// Get default emoji for a tag when meta config is not available
fn get_default_tag_emoji(_content_type: &str, _tag: &str) -> String {
    "🏷️".to_string() // Simple tag emoji for all tags
}
