# Meta.json Generation Utilities
# Functions for generating meta.json files with category and tag emoji mappings

use frontmatter.nu collect_categories_and_tags

# Default emoji mappings for categories
export def get_default_category_emojis [] {
    {
        "architecture": "🏛️",
        "devops": "🛠️",
        "infrastructure": "🏗️",
        "rust": "🦀",
        "web3": "⛓️",
        "blockchain": "💎",
        "web-development": "🌐",
        "docker": "🐳",
        "kubernetes": "☸️",
        "ci-cd": "🔄",
        "async-programming": "🌊",
        "rust-programming": "🦀",
        "frontend": "💻",
        "backend": "⚙️",
        "programacion-asincrona": "🌊",
        "programacion-rust": "🦀",
        "cicd": "🔄"
    }
}

# Default emoji mappings for tags
export def get_default_tag_emojis [] {
    {
        "rust": "🦀",
        "leptos": "⚡",
        "axum": "🔧",
        "web-development": "🌐",
        "architecture": "🏛️",
        "devops": "🛠️",
        "infrastructure": "🏗️",
        "docker": "🐳",
        "kubernetes": "☸️",
        "ci-cd": "🔄",
        "blockchain": "💎",
        "web3": "⛓️",
        "microservices": "🔗",
        "self-hosted": "🏠",
        "patterns": "📐",
        "performance": "⚡",
        "testing": "🧪",
        "security": "🔒",
        "monitoring": "📊",
        "deployment": "🚀",
        "api": "🔌",
        "database": "🗄️",
        "frontend": "💻",
        "backend": "⚙️",
        "terraform": "🌍",
        "error-handling": "🛡️",
        "async": "⚡",
        "syntax": "🎨",
        "highlighting": "🖍️",
        "smart-contracts": "📜",
        "iac": "🏭",
        "gitlab": "🦊",
        "async-programming": "🌊",
        "rust-programming": "🦀",
        "optimization": "💡",
        "best-practices": "⭐"
    }
}

# Create emoji mappings for categories and tags
export def create_emoji_mappings [categories: list<string>, tags: list<string>] {
    let category_defaults = get_default_category_emojis
    let tag_defaults = get_default_tag_emojis

    mut categories_emojis = {}
    mut tags_emojis = {}

    # Assign emojis to categories
    for category in $categories {
        if $category in $category_defaults {
            $categories_emojis = ($categories_emojis | insert $category ($category_defaults | get $category))
        } else {
            $categories_emojis = ($categories_emojis | insert $category "📂")
        }
    }

    # Assign emojis to tags
    for tag in $tags {
        if $tag in $tag_defaults {
            $tags_emojis = ($tags_emojis | insert $tag ($tag_defaults | get $tag))
        } else {
            $tags_emojis = ($tags_emojis | insert $tag "🏷️")
        }
    }

    {
        categories_emojis: $categories_emojis,
        tags_emojis: $tags_emojis
    }
}

# Generate meta.json file with categories and tags emojis
export def generate_meta_json [content_type: string, lang: string, source_dir: string, target_dir: string] {
    let meta_file = $"($target_dir)/meta.json"

    print $"(ansi blue)  - Generating meta.json for ($content_type) ($lang)...(ansi reset)"

    # Collect categories and tags from markdown files
    let collected = collect_categories_and_tags $source_dir

    # Create emoji mappings
    let emoji_mappings = create_emoji_mappings $collected.categories $collected.tags

    # Save to JSON file
    $emoji_mappings | to json | save --force $meta_file

    let categories_count = ($collected.categories | length)
    let tags_count = ($collected.tags | length)
    print $"(ansi green)    ✅ Meta file: ($meta_file) - categories: ($categories_count), tags: ($tags_count)(ansi reset)"
}
