#!/usr/bin/env python3
"""Test script for plugin manager."""
import asyncio
import sys
from pathlib import Path

# Add backend to path
sys.path.insert(0, str(Path(__file__).parent / "app"))

from backend.managers.plugin_manager import get_plugin_manager


async def test_plugin_manager():
    """Test plugin manager functionality."""
    print("Testing Plugin Manager...")
    print("-" * 50)

    manager = get_plugin_manager()

    # Test plugin discovery
    print("\n1. Testing plugin discovery...")
    discovered = await manager.discover_plugins()
    print(f"✅ Discovered {len(discovered)} plugins: {discovered}")

    # Test plugin loading
    if "hello_world" in discovered:
        print("\n2. Testing plugin loading...")
        success = await manager.load_plugin("hello_world")
        if success:
            print("✅ Plugin loaded successfully")
        else:
            print("❌ Failed to load plugin")
            return

        # Test plugin enabling
        print("\n3. Testing plugin enabling...")
        success = await manager.enable_plugin("hello_world")
        if success:
            print("✅ Plugin enabled successfully")
        else:
            print("❌ Failed to enable plugin")
            return

        # Test hook triggering
        print("\n4. Testing hook system...")
        results = await manager.trigger_hook("pm3_command", "hw version")
        print(f"✅ Hook triggered, results: {results}")

        results = await manager.trigger_hook("update_check")
        print(f"✅ Hook triggered, results: {results}")

        # Test plugin info
        print("\n5. Testing plugin info...")
        info = manager.get_plugin_info("hello_world")
        print(f"   Name: {info.metadata.name}")
        print(f"   Version: {info.metadata.version}")
        print(f"   Status: {info.status.value}")
        print(f"   Enabled: {info.enabled}")

        # Test listing enabled plugins
        print("\n6. Testing enabled plugins list...")
        enabled = manager.get_enabled_plugins()
        print(f"✅ Enabled plugins: {enabled}")

        # Test plugin disabling
        print("\n7. Testing plugin disabling...")
        success = await manager.disable_plugin("hello_world")
        if success:
            print("✅ Plugin disabled successfully")
        else:
            print("❌ Failed to disable plugin")

        # Test plugin unloading
        print("\n8. Testing plugin unloading...")
        success = await manager.unload_plugin("hello_world")
        if success:
            print("✅ Plugin unloaded successfully")
        else:
            print("❌ Failed to unload plugin")

    else:
        print("⚠️  hello_world plugin not found")

    print("\n✅ Plugin manager test complete!")


if __name__ == "__main__":
    asyncio.run(test_plugin_manager())
