#!/usr/bin/env nu
# Test RAG integration - demonstrates Phase 3 functionality
# Verifies that knowledge base can be loaded and queried

def main [
    --knowledge-base-dir: path = "config/knowledge-base"
    --verbose
    --test-queries
] {
    print "🔍 Testing RAG Integration (Phase 3)"
    print "===================================="
    print ""

    # Check files
    print "📋 Verifying knowledge base files..."
    let kb_dir = $knowledge_base_dir

    if not (($kb_dir + "/manifest.json") | path exists) {
        print "❌ Knowledge base files not found"
        return 1
    }

    print "✅ All knowledge base files found"

    # Load manifest
    print "\n📊 Analyzing knowledge base content..."
    let manifest = (open ($kb_dir + "/manifest.json"))
    print $"   Total documents: ($manifest.statistics.total_documents)"
    print $"   Best practice docs: ($manifest.statistics.best_practice_docs)"
    print $"   Extension docs: ($manifest.statistics.extension_docs)"
    print $"   Relationships: ($manifest.statistics.relationships)"

    # Category breakdown
    print "\n📈 Document statistics by category:"
    $manifest.statistics.by_category | each { |cat|
        if $cat.best_practice_count > 0 {
            print $"   • ($cat.category): ($cat.best_practice_count) best practices"
        }
        if $cat.extension_count > 0 {
            print $"   • ($cat.category): ($cat.extension_count) extensions"
        }
    }

    # Load and display sample documents
    print "\n🔎 Sample documents from knowledge base:"
    let bp_docs = (open ($kb_dir + "/best-practices-docs.json"))
    let ext_docs = (open ($kb_dir + "/extension-docs.json"))

    print "\n   Best Practices (first 3):"
    $bp_docs | first 3 | each { |doc|
        print ("     • " + $doc.id + ": " + $doc.title)
    }

    print "\n   Extensions (first 3):"
    $ext_docs | first 3 | each { |doc|
        print ("     • " + $doc.name + ": " + $doc.category)
    }

    # Relationship stats
    print "\n🔗 Document relationships:"
    let relationships = (open ($kb_dir + "/relationships.json"))
    let rel_relates = ($relationships | where relationship_type == "relates_to" | length)
    let rel_implements = ($relationships | where relationship_type == "implements" | length)
    let rel_depends = ($relationships | where relationship_type == "depends_on" | length)

    print ("   • relates_to: " + ($rel_relates | into string) + " relationships")
    print ("   • implements: " + ($rel_implements | into string) + " relationships")
    print ("   • depends_on: " + ($rel_depends | into string) + " relationships")

    # Test queries if requested
    if $test_queries {
        print "\n🧪 Testing knowledge base search..."
        let bp_docs = (open ($kb_dir + "/best-practices-docs.json"))

        let test_list = ["deployment", "security", "kubernetes", "configuration"]
        $test_list | each { |query|
            let content_matches = ($bp_docs | where { |doc| $doc.content | str contains $query })
            let tag_matches = ($bp_docs | where { |doc| $doc.tags | any { |tag| $tag | str contains $query } })
            let all_matches = ([$content_matches, $tag_matches] | flatten | uniq-by id)
            let count = ($all_matches | length | into string)
            print ("   Query '" + $query + "': " + $count + " matching documents")
        }
    }

    # Integration status
    print "\n✅ RAG Integration Status:"
    print "   • Knowledge base: Ready"
    print ("   • Documents indexed: " + ($manifest.statistics.total_documents | into string))
    print ("   • Relationships established: " + ($manifest.statistics.relationships | into string))
    print "   • Schema generated: Ready for SurrealDB"

    print "\n📋 Next steps:"
    print "   1. Start SurrealDB server:"
    print "      surreal start --bind 0.0.0.0:8000 file:./data.db"
    print ""
    print "   2. Load ai-service with knowledge base:"
    print ("      ai-service --load-knowledge-base " + $knowledge_base_dir)
    print ""
    print "   3. Query RAG endpoints:"
    print "      curl -X POST http://localhost:8083/api/v1/ai/ask \\"
    print "        -H 'Content-Type: application/json' \\"
    print "        -d '{\"question\": \"What are best practices for security?\"}'"
    print ""
    print "✨ Phase 3 (RAG Integration) is ready for testing!"

    0
}

main
