"""
Fix VD set: rebuild as a single proper array literal containing all
details/... URLs that appear anywhere between const VERIFIED_DETAILS=new Set(
and the closing );  -- regardless of historical malformed ], splits.
Also append the 5 new r165 urls.
"""
import re, json
h = open('index_main.html', encoding='utf-8').read()

# Find VD declaration start
vd_start = h.index('const VERIFIED_DETAILS=new Set(')
# Find the matching closing );  by walking paren depth after vd_start
i = vd_start + len('const VERIFIED_DETAILS=new Set(')
depth = 1  # we are inside Set(
while i < len(h) and depth > 0:
    c = h[i]
    if c == '(':
        depth += 1
    elif c == ')':
        depth -= 1
        if depth == 0:
            vd_end_paren = i
            break
    i += 1

# Extract everything between Set( and )
inner = h[vd_start + len('const VERIFIED_DETAILS=new Set('):vd_end_paren]
# Find all "details/..." strings in inner
urls = re.findall(r'"(details/[^"]+)"', inner)
# Add the 5 new r165 urls
new5 = [
    'details/ubc-msc-cs-thesis.html',
    'details/mcgill-msc-ece-thesis.html',
    'details/princeton-mse-mae.html',
    'details/uva-ms-systems-engineering.html',
    'details/mcgill-meng-ece-nonthesis.html',
]
for u in new5:
    if u not in urls:
        urls.append(u)
# Deduplicate preserving order
seen = set()
uniq = []
for u in urls:
    if u not in seen:
        seen.add(u)
        uniq.append(u)
print(f'After rebuild: {len(uniq)} unique urls (was {len(urls)} occurrences)')

# Rebuild as a single array literal
new_inner = '[' + ','.join(json.dumps(u, ensure_ascii=False) for u in uniq) + ']'
new_vd = 'const VERIFIED_DETAILS=new Set(' + new_inner + ')'
# Replace
h2 = h[:vd_start] + new_vd + h[vd_end_paren+1:]

with open('index_main.html', 'w', encoding='utf-8') as f:
    f.write(h2)
print(f'[DONE] index_main.html rewritten ({len(h2)} chars, was {len(h)})')
