import json, re

# Load existing PROGRAMS ids
html = open('index_main.html', encoding='utf-8').read()
existing_ids = set(re.findall(r'id:"([^"]+)"', html))
existing_urls = set(re.findall(r'url:"(details/[^"]+)"', html))
print(f"Existing IDs: {len(existing_ids)}, URLs: {len(existing_urls)}")

# Load todo_upgrade
todo = json.load(open('todo_upgrade.json', encoding='utf-8'))
print(f"\nTodo total: {len(todo)}")
print(f"Sample entry keys: {list(todo[0].keys()) if todo else 'empty'}")
print(f"Sample: {todo[0] if todo else 'none'}")

# Find entries not yet in PROGRAMS by matching s+en or det
# Fields: s/en/cn/deg/disc/col/url/det
pending = []
for item in todo:
    det = item.get('det', '')
    en = item.get('en', '')
    s = item.get('s', '')
    cn = item.get('cn', '')
    deg = item.get('deg', '')
    
    # Check if det filename matches existing url
    if det and det in existing_urls:
        continue
    
    # Also check if a url field matches
    url = item.get('url', '')
    if url and url in existing_urls:
        continue
    
    pending.append(item)

print(f"\nPending (not yet in PROGRAMS): {len(pending)}")

# Group by school
from collections import Counter
school_counts = Counter(p.get('s', '?') for p in pending)
print("\nPending by school:")
for s, c in school_counts.most_common(20):
    print(f"  {s}: {c}")

# Show first 20 pending entries
print("\n--- First 20 pending entries ---")
for i, p in enumerate(pending[:20]):
    print(f"{i+1}. [{p.get('s','?')}] {p.get('en','?')} | {p.get('cn','?')} | {p.get('deg','?')} | det={p.get('det','')}")
