#!/usr/bin/env python3
"""
Verify that dss-mcp-server.py properly exports Context Compiler tools
"""

import sys
from pathlib import Path

# Add the server directory to path
sys.path.insert(0, str(Path(__file__).parent))

# Import the server module
print("=" * 60)
print("CONTEXT COMPILER TOOL VERIFICATION")
print("=" * 60)

# Test imports
print("\n1. Testing Context Compiler imports...")
try:
    from core import (
        get_active_context,
        resolve_token,
        validate_manifest,
        list_skins,
        get_compiler_status
    )
    print("   ✓ All Context Compiler functions imported successfully")
    CONTEXT_COMPILER_AVAILABLE = True
except ImportError as e:
    print(f"   ✗ Context Compiler import failed: {e}")
    CONTEXT_COMPILER_AVAILABLE = False
    sys.exit(1)

# Test the server's tool list
print("\n2. Checking MCP server tool list...")
try:
    # We need to simulate the MCP server initialization
    # to see what tools it would export
    import asyncio
    from mcp.server import Server
    from mcp.server.stdio import stdio_server
    from mcp.types import Tool, TextContent

    # Create a test server instance
    server = Server("dss-test")

    # Import the list_tools function logic
    print("   Checking if server exports tools properly...")

    # Read the actual server file and check for context_compiler_tools
    with open(Path(__file__).parent / "servers" / "dss-mcp-server.py", "r") as f:
        server_code = f.read()

    if "context_compiler_tools" in server_code:
        print("   ✓ context_compiler_tools defined in server")
    else:
        print("   ✗ context_compiler_tools NOT found in server")
        sys.exit(1)

    if "dss_get_resolved_context" in server_code:
        print("   ✓ dss_get_resolved_context tool defined")
    else:
        print("   ✗ dss_get_resolved_context NOT found")
        sys.exit(1)

    if "dss_resolve_token" in server_code:
        print("   ✓ dss_resolve_token tool defined")
    else:
        print("   ✗ dss_resolve_token NOT found")
        sys.exit(1)

    if "dss_validate_manifest" in server_code:
        print("   ✓ dss_validate_manifest tool defined")
    else:
        print("   ✗ dss_validate_manifest NOT found")
        sys.exit(1)

    if "dss_list_skins" in server_code:
        print("   ✓ dss_list_skins tool defined")
    else:
        print("   ✗ dss_list_skins NOT found")
        sys.exit(1)

    if "dss_get_compiler_status" in server_code:
        print("   ✓ dss_get_compiler_status tool defined")
    else:
        print("   ✗ dss_get_compiler_status NOT found")
        sys.exit(1)

    # Check if tools are returned
    if "return dss_tools + devtools_tools + browser_tools + context_compiler_tools" in server_code:
        print("   ✓ context_compiler_tools added to tool list return")
    else:
        print("   ✗ context_compiler_tools NOT added to return statement")
        sys.exit(1)

except Exception as e:
    print(f"   ✗ Error checking server tools: {e}")
    sys.exit(1)

# Test tool handlers
print("\n3. Checking MCP server tool handlers...")
try:
    with open(Path(__file__).parent / "servers" / "dss-mcp-server.py", "r") as f:
        server_code = f.read()

    handlers = [
        'elif name == "dss_get_resolved_context"',
        'elif name == "dss_resolve_token"',
        'elif name == "dss_validate_manifest"',
        'elif name == "dss_list_skins"',
        'elif name == "dss_get_compiler_status"'
    ]

    for handler in handlers:
        if handler in server_code:
            tool_name = handler.split('"')[1]
            print(f"   ✓ {tool_name} handler implemented")
        else:
            tool_name = handler.split('"')[1]
            print(f"   ✗ {tool_name} handler NOT found")
            sys.exit(1)

except Exception as e:
    print(f"   ✗ Error checking tool handlers: {e}")
    sys.exit(1)

# Test Context Compiler functionality
print("\n4. Testing Context Compiler functionality...")
try:
    import json

    # Test list_skins
    skins_json = list_skins()
    skins = json.loads(skins_json)
    print(f"   ✓ list_skins() returned {len(skins)} skins: {skins}")

    # Test get_compiler_status
    status_json = get_compiler_status()
    status = json.loads(status_json)
    print(f"   ✓ get_compiler_status() returned status: {status['status']}")

    if status['status'] == 'active':
        print("   ✓ Context Compiler is active and ready")
    else:
        print(f"   ✗ Context Compiler status is: {status['status']}")
        sys.exit(1)

except Exception as e:
    print(f"   ✗ Context Compiler functionality test failed: {e}")
    sys.exit(1)

print("\n" + "=" * 60)
print("✅ ALL VERIFICATIONS PASSED")
print("=" * 60)
print("\nContext Compiler tools are properly integrated into dss-mcp-server.py")
print("and should be available to Claude Code after MCP server restart.")
print("\nIf tools are not showing up in Claude Code, try:")
print("1. Fully restart Claude Code (not just /mcp restart)")
print("2. Check Claude Code logs for connection errors")
print("3. Verify MCP server configuration in Claude settings")
