# -*- coding: utf-8 -*-
import re, os
IDX = r"D:\work buddy\2026-11-05-14-15-52\index_main.html"
html = open(IDX, "r", encoding="utf-8").read()

# Fix: remove the incorrectly appended 4 entries (which use {"id":"..." format)
# and re-append them with correct {id:"..." format (no quotes on keys)

# First, remove the bad block. The bad block starts after the last good entry and ends before ]
# Bad pattern: ,\n,\n  {"id":"ucla-mfe"... through ..."url":"details/nyu-msqe.html"}
# We'll locate it by finding the bad start marker
bad_start_marker = ',\n,\n  {"id":"ucla-mfe"'
bad_end_marker = '"url":"details/nyu-msqe.html"}'
bi = html.find(bad_start_marker)
ei = html.find(bad_end_marker)
if bi >= 0 and ei >= 0:
    ei_end = ei + len(bad_end_marker)
    # Remove the bad block entirely (from after the last good entry's comma to before ])
    html = html[:bi] + "\n" + html[ei_end:]
    print("Removed bad block from offset", bi, "to", ei_end)
else:
    print("WARNING: bad block not found, bi=", bi, "ei=", ei)

# Now re-append with correct format: {id:"..." not {"id":"..."}
new_programs = [
    '{id:"ucla-mfe",s:"ucla",n:"\u91d1\u878d\u5de5\u7a0b\u7855\u58eb",en:"Master of Financial Engineering",deg:"M.F.E.",lvl:"m",stem:1,dur:"16 months (Aug-Dec, incl summer internship)",ddl:"R1 2026-11-30 / R2 2027-01-31",lang:"TOEFL iBT / IELTS (UCLA Graduate standard)",test:{cls:"GMAT/GRE required (admitted range, not minimum threshold)"},tui:"$100,719 (2026-27 total tuition)",url:"details/ucla-mfe.html"}',
    '{id:"ucla-biostat-ms",s:"ucla",n:"\u751f\u7269\u7edf\u8ba1\u5b66\u7855\u58eb",en:"MS in Biostatistics",deg:"MS",lvl:"m",stem:1,dur:"2 years (6 quarters; waiver to 1 year)",ddl:"2026-12-01",lang:"TOEFL iBT / IELTS (UCLA Graduate standard)",test:{cls:"GRE Optional"},tui:"TBD",url:"details/ucla-biostat-ms.html"}',
    '{id:"ucla-hpm-ms",s:"ucla",n:"\u5065\u5eb7\u653f\u7b56\u4e0e\u7ba1\u7406\u7855\u58eb",en:"MS in Health Policy and Management",deg:"MS",lvl:"m",stem:0,dur:"2 years (6 quarters; 1-year accelerated for prior doctorate)",ddl:"2026-12-01",lang:"TOEFL iBT / IELTS (UCLA Graduate standard)",test:{cls:"GRE Optional"},tui:"TBD",url:"details/ucla-hpm-ms.html"}',
    '{id:"nyu-msqe",s:"nyu",n:"\u5b9a\u91cf\u7ecf\u6d4e\u5b66\u7855\u58eb",en:"MS in Quantitative Economics",deg:"MS",lvl:"m",stem:1,dur:"10 months (Jul-May, 6 intensive modules)",ddl:"TBD",lang:"TOEFL iBT / IELTS (NYU GSAS standard)",test:{cls:"GRE required (Math-heavy, admitted range, not minimum threshold)"},tui:"~$1,999/credit x 33 credits (2021 rate TBD)",url:"details/nyu-msqe.html"}',
]

# Locate PROGRAMS array end via bracket-depth matching
m = re.search(r'(?:const|let|var)\s+PROGRAMS\s*=\s*\[', html)
assert m, "PROGRAMS array not found"
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
insert_pos = i - 1  # points at ]
# Find last non-whitespace before ]
j = insert_pos - 1
while j >= 0 and html[j] in ' \t\n\r': j -= 1
print("Last char before ]:", repr(html[j]))
# If last char is }, add comma; if it's already comma, don't add another
if html[j] == '}':
    prefix = ","
elif html[j] == ',':
    prefix = ""
else:
    prefix = ","
new_block = prefix + "\n  " + ",\n  ".join(new_programs)
html = html[:insert_pos] + new_block + "\n" + html[insert_pos:]

open(IDX, "w", encoding="utf-8").write(html)
print("OK: 4 PROGRAMS re-appended with correct format")
