#!/usr/bin/env python3
"""
Generate MCP method registrations for RevitMCPBridge2026
Scans all *Methods.cs files and generates case statements for MCPServer.cs
"""

import re
import os

def to_camel_case(pascal_case):
    """Convert PascalCase to camelCase"""
    if not pascal_case:
        return pascal_case
    return pascal_case[0].lower() + pascal_case[1:]

def extract_methods_from_file(filepath):
    """Extract all public static string method names from a C# file"""
    methods = []
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
        # Match: public static string MethodName(UIApplication uiApp, JObject parameters)
        pattern = r'public\s+static\s+string\s+(\w+)\s*\(\s*UIApplication\s+uiApp,\s*JObject\s+parameters\s*\)'
        matches = re.findall(pattern, content)
        methods.extend(matches)
    return methods

def get_class_name(filepath):
    """Extract class name from filepath (e.g., AnnotationMethods.cs -> AnnotationMethods)"""
    basename = os.path.basename(filepath)
    return basename.replace('.cs', '')

def generate_registration(method_name, class_name):
    """Generate a case statement for method registration"""
    camel_case_name = to_camel_case(method_name)
    return f'''                    case "{camel_case_name}":
                        return await ExecuteInRevitContext(uiApp => {class_name}.{method_name}(uiApp, parameters));
'''

# Categories that need registration (0% or partial)
categories_to_register = {
    'AnnotationMethods': 33,
    'FamilyMethods': 29,
    'FilterMethods': 27,
    'MEPMethods': 35,
    'MaterialMethods': 27,
    'ParameterMethods': 29,
    'StructuralMethods': 26,
    'DetailMethods': 32,  # Partial: 1/33 registered, need 32 more
    'DimensionMethods': 1,
    'ScheduleMethods': 1,  # Partial: 33/34 registered, need 1 more
}

# Main generation
print("// ====================================================================")
print("// AUTO-GENERATED METHOD REGISTRATIONS")
print("// Generated by generate_registrations.py")
print("// Add these to MCPServer.cs in the HandleRequest switch statement")
print("// ====================================================================\n")

total_generated = 0

for category in sorted(categories_to_register.keys()):
    filepath = f"{category}.cs"

    if not os.path.exists(filepath):
        print(f"// WARNING: {filepath} not found!")
        continue

    methods = extract_methods_from_file(filepath)

    if not methods:
        print(f"// WARNING: No methods found in {filepath}")
        continue

    print(f"// {category} ({len(methods)} methods)")
    print()

    for method in sorted(methods):
        registration = generate_registration(method, category)
        print(registration)
        total_generated += 1

    print()

print(f"// ====================================================================")
print(f"// TOTAL: {total_generated} method registrations generated")
print(f"// ====================================================================")
