import json, re
from pathlib import Path

root = Path(r"D:\work buddy\2026-11-05-14-15-52")

# Load existing PROGRAMS ids
html = (root / "index_main.html").read_text(encoding="utf-8")
m = re.search(r"const\s+PROGRAMS\s*=\s*\[", html)
start = m.end(); depth = 1; i = start
while i < len(html) and depth > 0:
    c = html[i]
    if c == "[": depth += 1
    elif c == "]": depth -= 1
    i += 1
block = html[start:i-1]
existing_ids = set(re.findall(r'\{id:"([^"]+)"', block))
print(f"Existing PROGRAMS ids: {len(existing_ids)}")

# Load todo_upgrade
todo = json.loads((root / "todo_upgrade.json").read_text(encoding="utf-8"))
print(f"Todo total: {len(todo)}")

# Priority order: CS/EE/Finance/Econ/BA/DS/Stats/ME/Bio/Management
# We want to find candidates NOT already in PROGRAMS, NOT in excluded list
excluded_keywords = [
    "online", "part-time", "part time", "executive", "master of business administration",
    "mba", "distance", "hybrid", "working professional", "mid-career", "part-time",
    "hst", "clinical", "residency",
]

# Group todo by field
candidates = []
for item in todo:
    if not isinstance(item, dict): continue
    title = (item.get("title") or item.get("n") or item.get("name") or "").lower()
    school = item.get("s") or item.get("school") or ""
    url = item.get("url") or item.get("detail_url") or ""
    pid = item.get("id") or ""
    # skip if already in PROGRAMS
    if pid and pid in existing_ids: continue
    # skip obvious excluded
    if any(k in title for k in excluded_keywords): continue
    candidates.append(item)

print(f"Remaining candidates after simple filter: {len(candidates)}")
# Print first 30
for c in candidates[:30]:
    print(f"  {c.get('id','?'):40s} s={c.get('s','?'):10s} n={c.get('n','?')[:60]}")
