import os
import json
import re
from pathlib import Path
from utils import clean_title

def get_sort_key(name):
    match = re.match(r'^(\d+)[_-]', name)
    if match:
        return (int(match.group(1)), name)
    return (float('inf'), name)

def has_valid_page(directory):
    for root, dirs, files in os.walk(directory):
        dirs[:] = [d for d in dirs if not (d.startswith('.') or d.startswith('_'))]
        for f in files:
            if f in ('page.mdx', 'page.md', 'page.tsx', 'page.jsx') and not (f.startswith('.') or f.startswith('_')):
                return True
    return False

def main():
    root_dir = Path(__file__).parent.parent.resolve()
    app_dir = root_dir / "next-docs" / "src" / "app"
    top_level_routes = []
    
    for root, dirs, files in os.walk(app_dir):
        all_items = sorted(dirs + files, key=get_sort_key)
        is_root = Path(root) == app_dir

        valid_items = []
        for item in all_items:
            if item.startswith('.') or item.startswith('_') or item in ['layout.tsx', 'globals.css', 'favicon.ico', 'next-env.d.ts']:
                continue
                
            item_path = Path(root) / item
            
            if item_path.is_file():
                if item not in ('page.mdx', 'page.md', 'page.tsx', 'page.jsx'):
                    continue
            elif item_path.is_dir():
                if not has_valid_page(item_path):
                    continue
                    
            orig_name = item.rsplit('.', 1)[0] if item_path.is_file() and '.' in item else item
                
            if orig_name in ['layout', 'demo', 'favicon', 'page']:
                continue

            if is_root and orig_name == 'app':
                continue
                
            valid_items.append((item, orig_name))
            
        if not valid_items:
            continue

        meta_dict = {}
        for index, (item, route_name) in enumerate(valid_items, 1):
            cleaned = clean_title(route_name, strip_prefix=True)
            formatted_title = f"{index}. {cleaned}"
            meta_dict[route_name] = formatted_title
            
            if is_root:
                top_level_routes.append({"title": formatted_title, "href": f"/{route_name}"})
            
        if meta_dict:
            meta_path = Path(root) / "_meta.ts"
            
            if is_root:
                meta_dict = {
                    "index": {"title": "Home"},
                    **meta_dict
                }
                
            with open(meta_path, 'w', encoding='utf-8') as f:
                f.write("export default {\n")
                for key, value in meta_dict.items():
                    if isinstance(value, dict):
                        f.write(f'  "{key}": {json.dumps(value, indent=4).replace("}", "  }")},\n')
                    else:
                        safe_key = f'"{key}"' if not key.isidentifier() else key
                        safe_value = value.replace('"', '\\"')
                        f.write(f'  {safe_key}: "{safe_value}",\n')
                f.write("}\n")
            print(f"Generated {meta_path}")

    cards_mdx = "\n<Cards num={3}>\n"
    for r in top_level_routes:
        cards_mdx += f'  <Cards.Card title="{r["title"]}" href="{r["href"]}" arrow />\n'
    cards_mdx += "</Cards>\n"

    page_mdx_path = app_dir / "page.mdx"
    if page_mdx_path.exists():
        content = page_mdx_path.read_text(encoding='utf-8')
        new_content = re.sub(r'<Cards[\s\S]*?<\/Cards>', cards_mdx.strip(), content)
        page_mdx_path.write_text(new_content, encoding='utf-8')
        print("Updated page.mdx with the latest categories.")

if __name__ == '__main__':
    main()
