#!/bin/bash

echo "=================================================="
echo "Claude Plugin Marketplace Integration Verification"
echo "=================================================="
echo ""

# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'

# Check files
echo -e "${BLUE}1. Checking implementation files...${NC}"
files=(
    "lib/plugin_marketplace.py"
    "lib/plugin_skill_loader.py"
    "lib/dispatcher_plugin_integration.py"
    "lib/plugin_kg_integration.py"
    "lib/plugin_cli.py"
    "tests/test_plugin_system.py"
    "docs/PLUGIN-MARKETPLACE-INTEGRATION.md"
    "PLUGIN-IMPLEMENTATION-SUMMARY.md"
)

for file in "${files[@]}"; do
    if [ -f "$file" ]; then
        lines=$(wc -l < "$file")
        echo -e "${GREEN}✓${NC} $file ($lines lines)"
    else
        echo -e "${RED}✗${NC} $file (missing)"
    fi
done

echo ""
echo -e "${BLUE}2. Running plugin system tests...${NC}"
python3 tests/test_plugin_system.py 2>&1 | grep -E "^(==|[[]|Total|Exit)" || echo "Tests completed"

echo ""
echo -e "${BLUE}3. Checking plugin inventory...${NC}"
python3 << 'PYTEST'
import sys
sys.path.insert(0, 'lib')
from plugin_marketplace import get_marketplace_registry
registry = get_marketplace_registry()
print(f"  Plugins loaded: {len(registry.plugins)}")
for plugin in registry.list_plugins():
    print(f"    - {plugin.name} ({len(plugin.capabilities)} capabilities)")
PYTEST

echo ""
echo -e "${BLUE}4. Checking skill generation...${NC}"
python3 << 'PYTEST'
import sys
sys.path.insert(0, 'lib')
from plugin_skill_loader import get_plugin_skill_loader
loader = get_plugin_skill_loader()
skills = loader.list_skills()
print(f"  Skills generated: {len(skills)}")
for cat in loader.category_index.keys():
    count = len(loader.category_index[cat])
    print(f"    - {cat}: {count} skills")
PYTEST

echo ""
echo -e "${BLUE}5. Checking knowledge graph exports...${NC}"
if [ -d "/tmp/.luzia-kg-exports" ]; then
    echo "  Export directory: /tmp/.luzia-kg-exports"
    ls -lh /tmp/.luzia-kg-exports/ | tail -4 | while read line; do
        echo "    $line"
    done
else
    echo "  Export directory not found"
fi

echo ""
echo -e "${BLUE}6. Testing CLI commands...${NC}"
python3 << 'PYTEST'
import sys
sys.path.insert(0, 'lib')
from plugin_cli import get_plugin_cli

cli = get_plugin_cli()
print("  Available commands:")
print("    - plugins list")
print("    - plugins <name>")
print("    - plugins skills")
print("    - plugins find '<task>'")
print("    - plugins export")
print("    - plugins stats")
print("    - plugins help")
PYTEST

echo ""
echo "=================================================="
echo -e "${GREEN}✓ Plugin system verification complete!${NC}"
echo "=================================================="
echo ""
echo "Quick start:"
echo "  python3 -c \"import sys; sys.path.insert(0, 'lib'); from plugin_cli import get_plugin_cli; cli = get_plugin_cli(); cli.cmd_list_plugins([])\""
echo ""
