import re

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

new_records = [
    ('notre-msba', 'notre', '商业分析理学硕士（South Bend 在校 · STEM · 1 年 · pre-professional）', 'Master of Science in Business Analytics (MSBA, Residential)', 'MSBA', 'm', 1, '1 year / 3 semesters on-campus',
     'Early Action Sep 10, 2026 / R1 Oct 20, 2026 / R2 Jan 7, 2027 / R3 Mar 17, 2027', 'TOEFL / PTE / IELTS Academic (international, exact score 待补)', 'GMAT/GRE waived for GPA 3.5+ or Notre undergrad; min GPA 3.0; prereq 1 stats + 2 business courses; no work exp required (recent grads)',
     '~$60,000+/year (待补)', 'details/notre-msba.html'),
    ('notre-msf', 'notre', '金融理学硕士（Residential · STEM · 量化金融）', 'Master of Science in Finance (MSF, Residential)', 'MSF', 'm', 1, '~1 year accelerated on-campus',
     'Early Action Sep 10, 2026 / R1 Oct 20, 2026 / R2 Jan 7, 2027 / R3 Mar 17, 2027', 'TOEFL / PTE / IELTS Academic (international, exact score 待补)', 'GMAT/GRE waived for GPA 3.5+ or Notre undergrad; concentrations: Corporate Finance / Investments / Finance; no work exp required; quant science/eng background preferred',
     '~$60,000+/year (待补)', 'details/notre-msf.html'),
    ('ucsd-cse-ms', 'ucsd', '计算机科学与工程理学硕士（Thesis / Comprehensive 三 Plan · Fall only）', 'Master of Science in Computer Science and Engineering (CSE MS)', 'MS', 'm', 1, '2 years thesis / 1.5-2 years comprehensive, Fall admission',
     'December (Fall 2027, exact date 待补)', 'TOEFL iBT / IELTS (UCSD Grad Division standard)', 'GRE not required for Fall 2027; min GPA 3.0; 3 letters; BS in CS/CE/EE/Math preferred; 2 majors (CS/CE) x 3 plans (Thesis/Comprehensive Standard/Interdisciplinary)',
     '~$50,000+/year (待补)', 'details/ucsd-cse-ms.html'),
    ('uva-me-bme', 'uva', '生物工程工程硕士（M.E. · 32 credits · 课程型 · 无 thesis）', 'Master of Engineering in Biomedical Engineering (M.E. BME)', 'M.E.', 'm', 1, '~1-1.5 years / 32 credits coursework + design, Fall admission',
     'M.E. Fall Jul 31, 2026 (MS/PhD Dec 15, 2025)', 'TOEFL iBT 90-100 recommended / IELTS 7.0+', 'GRE waived for 2026 admission; typical GPA >3.5; prereq intro chem/biology/physics(2 sem calc-based)/math through ODE/programming; design project replaces thesis',
     '~$50,000+/year (待补)', 'details/uva-me-bme.html'),
    ('uchicago-msca', 'uchicago', '分析理学硕士（MScA · 12 courses + 2-quarter capstone · NBC Tower 在校）', 'Master of Science in Analytics (MScA)', 'MS', 'm', 1, '12-15 months full-time (3 courses/quarter), Autumn only',
     'Autumn rolling (final Jun 23, closes when cohort full)', 'TOEFL iBT 102 minimum / IELTS 7.0 (no sub-score)', 'GRE/GMAT not required; 12 courses = 7 core + 3 elective + 2 capstone; full-time in-person only visa-eligible; avg work exp 6mo-4yr',
     '~$65,000+/year (待补)', 'details/uchicago-msca.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)})')
