# check_urls.py
import sys
import os
import django

# Setup Django
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()

from django.urls import get_resolver

def show_urls():
    print("\n" + "="*60)
    print("REGISTERED URL PATTERNS")
    print("="*60)
    
    resolver = get_resolver()
    url_patterns = []
    
    def collect_urls(patterns, prefix=''):
        for pattern in patterns:
            if hasattr(pattern, 'url_patterns'):
                # It's an included URL conf
                new_prefix = prefix + str(pattern.pattern)
                collect_urls(pattern.url_patterns, new_prefix)
            else:
                # It's a single URL pattern
                url = prefix + str(pattern.pattern)
                name = getattr(pattern, 'name', None)
                namespace = getattr(pattern, 'namespace', None)
                url_patterns.append((url, name, namespace))
    
    collect_urls(resolver.url_patterns)
    
    # Sort by URL
    url_patterns.sort(key=lambda x: x[0])
    
    # Print by app
    current_app = None
    for url, name, namespace in url_patterns:
        if namespace and namespace != current_app:
            current_app = namespace
            print(f"\n[{current_app.upper()}]")
        
        if name:
            print(f"  {url:40} - {name}")
        else:
            print(f"  {url:40}")

if __name__ == '__main__':
    show_urls()