import re

h = open('index_main.html', encoding='utf-8').read()

new_records = [
    ('illinois-msfe', 'illinois', '金融工程硕士（MSFE · 3 学期 · 工程+商院联办）', 'Master of Science in Financial Engineering (MSFE)', 'MSFE', 'm', 1, '3 semesters / 15 months (extendable to 18-24)',
     'Rolling (recommend Jan-Mar for Aug entry)', 'TOEFL iBT 103 / IELTS 7.5 / Duolingo 135', 'GRE/GMAT not required; min GPA 3.25 last 60 hrs; undergrad engineering/math/physics/CS/econ; prereq calculus/linear algebra/probability/stats/programming',
     '~$45,000+/year (待补)', 'details/illinois-msfe.html'),
    ('princeton-mpa', 'princeton', '公共事务硕士（2 年 · MPA · 4 方向）', 'Master in Public Affairs (MPA)', 'MPA', 'm', 0, '2 years full-time on-campus',
     'Dec 1, 2026 (fall entry)', 'TOEFL iBT / IELTS (Grad School standard)', 'GRE required; bachelor direct entry, avg prior work exp; 4 fields: IR/Intl Dev/Domestic/Econ & Public Policy; full funding',
     'Full tuition + fees (Princeton funding)', 'details/princeton-mpa.html'),
    ('emory-mph', 'emory', '罗林斯公共卫生硕士（4 学期 · MPH · CEPH）', 'Master of Public Health (MPH, Rollins)', 'MPH', 'm', 0, '4 semesters (~1.5-2 years) on-campus',
     'Jan 5 priority (scholarship)', 'TOEFL iBT / IELTS (SOPHAS)', 'GRE not required; min GPA 3.0 preferred; SOPHAS application; concentrations: Biostat/Epi/Global Health/HPM/BSHE/EHS',
     '~$55,000/year (待补)', 'details/emory-mph.html'),
    ('nu-mim', 'nu', '凯洛格商学院管理学硕士（10 个月 · MiM）', 'Master in Management (MiM, Kellogg)', 'MiM', 'm', 0, '10 months (Sep enrollment, graduate next June)',
     'R1 Oct 28, 2026 / R2 Jan 13, 2027 / R3 Apr 21 / R4 Jun 2', 'TOEFL/IELTS (non-English undergrad)', 'GMAT/GRE required (Northwestern undergrad waived); recent grads <2 yrs work exp; STEM/econ/liberal arts background',
     '~$80,000+ total (待补)', 'details/nu-mim.html'),
    ('brown-msds', 'brown', '数据科学理学硕士（在校版 · ScM）', 'Master of Science in Data Science (ScM)', 'ScM', 'm', 1, '12-24 months on-campus (fall default 21 months)',
     'Fall Feb 1 / Spring Nov 1 / Fifth-Year May 1', 'TOEFL iBT 105 preferred / IELTS', 'GRE not required; min GPA 3.0; prereq calculus/linear algebra/probability/stats/programming (Python/R); 9 credits incl capstone, no thesis',
     '~$60,000+/year (待补)', 'details/brown-msds.html'),
]

def fmt(rec):
    pid, s, n, en, deg, lvl, stem, dur, ddl, lang, test, tui, url = rec
    return (f'{{id:"{pid}",s:"{s}",n:"{n}",en:"{en}",deg:"{deg}",lvl:"{lvl}",stem:{stem},dur:"{dur}",ddl:"{ddl}",lang:"{lang}",test:{{cls:"{test}"}},tui:"{tui}",url:"{url}"}}')

new_block = ',\n' + ',\n'.join(fmt(r) for r in new_records)

m = re.search(r'const PROGRAMS\s*=\s*\[', h)
if not m:
    raise SystemExit('PROGRAMS start not found')
start = m.end()
depth = 1
i = start
in_str = False
str_ch = ''
while i < len(h) and depth > 0:
    c = h[i]
    if in_str:
        if c == '\\':
            i += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '[':
            depth += 1
        elif c == ']':
            depth -= 1
    i += 1
end_bracket = i - 1
new_h = h[:end_bracket] + new_block + '\n' + h[end_bracket:]

m2 = re.search(r'VERIFIED_DETAILS\s*=\s*new\s*Set\s*\(', new_h)
if not m2:
    raise SystemExit('VD Set start not found')
paren_start = m2.end()
depth = 1
j = paren_start
in_str = False
str_ch = ''
while j < len(new_h) and depth > 0:
    c = new_h[j]
    if in_str:
        if c == '\\':
            j += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '(':
            depth += 1
        elif c == ')':
            depth -= 1
    j += 1
paren_end = j - 1

arr_depth = 0
k = paren_start
in_str = False
str_ch = ''
arr_close = -1
while k < paren_end:
    c = new_h[k]
    if in_str:
        if c == '\\':
            k += 2
            continue
        if c == str_ch:
            in_str = False
    else:
        if c in ('"', "'"):
            in_str = True
            str_ch = c
        elif c == '[':
            arr_depth += 1
        elif c == ']':
            arr_depth -= 1
            if arr_depth == 0:
                arr_close = k
                break
    k += 1
if arr_close < 0:
    raise SystemExit('VD array close not found')

new_urls = [r[12] for r in new_records]
vd_insert = ',\n  "' + '",\n  "'.join(new_urls) + '"\n'
new_h = new_h[:arr_close] + vd_insert + new_h[arr_close:]

open('index_main.html', 'w', encoding='utf-8').write(new_h)
print(f'[OK] PROGRAMS +{len(new_records)}, VD +{len(new_urls)}')
print(f'File size: {len(new_h)} chars (was {len(h)})')
