import re
html = open('index_main.html', encoding='utf-8').read()
# Parse all PROGRAMS entries with their fields
pm = re.search(r'const PROGRAMS=\[([\s\S]*?)\n\];', html)
block = pm.group(1)
# Split by top-level {id:"..."
entries = re.split(r'(?=\{id:")', block)
parsed = []
for e in entries:
    e = e.strip().rstrip(',')
    if not e.startswith('{'): continue
    mid = re.search(r'\{id:"([^"]+)"', e)
    if not mid: continue
    pid = mid.group(1)
    s = re.search(r's:"([^"]*)"', e)
    n = re.search(r'n:"([^"]*)"', e)
    en = re.search(r'en:"([^"]*)"', e)
    deg = re.search(r'deg:"([^"]*)"', e)
    url = re.search(r'url:"([^"]*)"', e)
    parsed.append({
        'id': pid,
        's': s.group(1) if s else '',
        'n': n.group(1) if n else '',
        'en': en.group(1) if en else '',
        'deg': deg.group(1) if deg else '',
        'url': url.group(1) if url else '',
    })

# Find duplicates
from collections import defaultdict
by_id = defaultdict(list)
for p in parsed:
    by_id[p['id']].append(p)

dups = {k:v for k,v in by_id.items() if len(v) > 1}
print(f'Duplicate id groups: {len(dups)}')
print()
# Classify: are they truly identical (same url)? or conflicting (different url)?
true_dup = 0
conflict = 0
for pid, group in sorted(dups.items()):
    urls = set(g['url'] for g in group)
    ens = set(g['en'] for g in group)
    if len(urls) == 1 and len(ens) == 1:
        true_dup += 1
        tag = 'TRUE-DUP'
    else:
        conflict += 1
        tag = 'CONFLICT'
    print(f'[{tag}] {pid}  x{len(group)}')
    for g in group:
        print(f'    s={g["s"]}  n={g["n"]}  en={g["en"][:60]}  deg={g["deg"]}  url={g["url"]}')
print()
print(f'True duplicates (same url/en): {true_dup}')
print(f'Conflicts (different url/en): {conflict}')
