import json
from pathlib import Path
from collections import Counter
# Explicitly use utf-8 encoding
with open('.graphify_detect.json', 'r', encoding='utf-16') as f:
    detect = json.load(f)
all_files = [f for files in detect['files'].values() for f in files]
subdirs = Counter()
for f in all_files:
    try:
        # The paths in the JSON might be absolute, so we need to be careful
        p = Path(f)
        # Try to make it relative to the current working directory
        cwd = Path.cwd()
        if p.is_absolute() and str(p).startswith(str(cwd)):
            rel_p = p.relative_to(cwd)
            if len(rel_p.parts) > 0:
                subdirs[rel_p.parts[0]] += 1
            else:
                subdirs['root'] += 1
        else:
            # If it's already relative or from a different root
            if len(p.parts) > 0:
                subdirs[p.parts[0]] += 1
            else:
                subdirs['root'] += 1
    except Exception:
        subdirs['unknown'] += 1

for d, count in subdirs.most_common(10):
    print(f'  {d}: {count} files')
