"""
Complete migration script - Extracts all endpoints from main.py to routers
This preserves all functionality while creating a clean modular structure
"""
import re
import os

def read_file(path):
    with open(path, 'r') as f:
        return f.read()

def extract_section(content, start_marker, end_marker):
    """Extract a section between two markers"""
    start = content.find(start_marker)
    if start == -1:
        return None
    end = content.find(end_marker, start)
    if end == -1:
        end = len(content)
    return content[start:end]

# Read original main.py
main_content = read_file('main.py')

# Find all endpoint definitions
endpoint_pattern = r'@app\.(get|post|put|delete|patch)\(["\']([^"\']+)["\']\)'
endpoints = re.findall(endpoint_pattern, main_content)

print(f"Found {len(endpoints)} endpoints in main.py:")
for method, path in endpoints[:20]:  # Show first 20
    print(f"  {method.upper():6} {path}")

if len(endpoints) > 20:
    print(f"  ... and {len(endpoints) - 20} more")

# Group endpoints by category
categories = {
    'auth': [],
    'characters': [],
    'game': [],
    'combat': [],
    'equipment': [],
    'crafting': [],
    'loot': [],
    'admin': [],
    'statistics': [],
    'health': []
}

for method, path in endpoints:
    if '/api/auth/' in path:
        categories['auth'].append((method, path))
    elif '/api/characters' in path:
        categories['characters'].append((method, path))
    elif '/api/game/combat' in path or '/api/game/pvp' in path:
        categories['combat'].append((method, path))
    elif '/api/game/equip' in path or '/api/game/unequip' in path or '/api/game/equipment' in path or '/api/game/repair' in path or '/api/game/repairable' in path or '/api/game/salvageable' in path:
        categories['equipment'].append((method, path))
    elif '/api/game/craft' in path or '/api/game/uncraft' in path or '/api/game/craftable' in path:
        categories['crafting'].append((method, path))
    elif '/api/game/corpse' in path or '/api/game/loot' in path:
        categories['loot'].append((method, path))
    elif '/api/internal/' in path:
        categories['admin'].append((method, path))
    elif '/api/statistics' in path or '/api/leaderboard' in path:
        categories['statistics'].append((method, path))
    elif '/health' in path:
        categories['health'].append((method, path))
    elif '/api/game/' in path:
        categories['game'].append((method, path))

print("\n" + "="*60)
print("Endpoint Distribution:")
for cat, endpoints_list in categories.items():
    if endpoints_list:
        print(f"  {cat:15}: {len(endpoints_list):2} endpoints")

print("\n" + "="*60)
print("\nNext steps:")
print("1. ✅ Auth router - already created")
print("2. ✅ Characters router - already created")
print("3. ⏳ Game routes router - needs creation (largest)")
print("4. ⏳ Combat router - needs creation")
print("5. ⏳ Equipment router - needs creation")
print("6. ⏳ Crafting router - needs creation")
print("7. ⏳ Loot router - needs creation")
print("8. ⏳ Admin router - needs creation")
print("9. ⏳ Statistics router - needs creation")
print("10. ⏳ Clean main.py - after all routers created")
