import os

BASE = os.path.expanduser("~/Desktop/Alex Wiki")
cats = [
    ("motherlabs/compiler", "Motherlabs / Compiler"),
    ("motherlabs/architecture", "Motherlabs / Architecture"),
    ("motherlabs/strategy", "Motherlabs / Strategy"),
    ("ai-research/agents", "AI Research / Agents"),
    ("ai-research/models", "AI Research / Models"),
    ("ai-research/theory", "AI Research / Theory"),
    ("projects/tattooclaw", "Projects / TattooClaw"),
    ("projects/aigency", "Projects / AIgency"),
    ("projects/misc", "Projects / Misc"),
    ("personal/ideas", "Personal / Ideas"),
    ("personal/reflections", "Personal / Reflections"),
    ("personal/learning", "Personal / Learning"),
    ("uncategorized", "Uncategorized"),
]

total = 0
lines = ["# Alex Wiki -- Index\n", "Generated: 2026-04-03\n"]
summary = []

for cat, label in cats:
    d = os.path.join(BASE, cat)
    if not os.path.isdir(d):
        continue
    files = sorted([f for f in os.listdir(d) if f.endswith(".md")])
    if not files:
        summary.append(f"{label}: 0")
        continue
    total += len(files)
    summary.append(f"{label}: {len(files)}")
    lines.append(f"\n## {label} ({len(files)})\n")
    for f in files:
        title = f.replace(".md", "").replace("--", " / ").replace("-", " ")
        lines.append(f"- [{title}]({cat}/{f})")

lines.insert(2, f"Total files: {total}\n")

with open(os.path.join(BASE, "index.md"), "w") as fh:
    fh.write("\n".join(lines))

print("=== FINAL SUMMARY ===\n")
for s in summary:
    print(s)
print(f"\nTOTAL: {total}")
print(f"\nIndex: {os.path.join(BASE, 'index.md')}")
