import re

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

new_records = [
    ('uchicago-harris-msdap', 'uchicago', '数据 analytics 与政策理学硕士（MSDAP · 11 个月 · 夏季入学 · 加速）', 'Master of Science in Data Analytics and Policy (MSDAP, Harris School of Public Policy)', 'MS', 'm', 1, '11 months, summer start (June matriculation)',
     'Summer 入学（6 月入学，精确轮次待补）', 'TOEFL iBT / IELTS Academic（具体分数待补）', 'GRE required 或 waiver；strong quantitative background required；夏季 programming/statistics boot camp；3 推荐信',
     '约 USD 75,000+/11-month（精确待补）', 'details/uchicago-harris-msdap.html'),
    ('uchicago-harris-mscep', 'uchicago', '气候与能源政策理学硕士（MSCEP · 11 个月 · STEM · 2026 首届）', 'Master of Science in Climate and Energy Policy (MSCEP, Harris & Institute for Climate and Sustainable Growth)', 'MS', 'm', 1, '11 months, summer start (July matriculation)',
     'EA 2026/10/8 / R1 2026/12/3 / R2 2027/1/21 / R3 2027/6/17；国际生建议 R2 前', 'TOEFL iBT / IELTS Academic（具体分数待补）', 'GRE required 或 waiver；12 courses (1200 units) = 9 core + 3 elective；GPA≥2.7；STEM-designated；4 段短文书（每段≤250 词）；3 推荐信',
     '约 USD 75,000+/11-month（精确待补）', 'details/uchicago-harris-mscep.html'),
    ('emory-msba', 'emory', '商业分析理学硕士（MSBA · 10 个月 · STEM · Goizueta · 4 方向）', 'Master of Science in Business Analytics (MSBA, Goizueta Business School)', 'MSBA', 'm', 1, '10 months (July start, graduate May) 或 16 months (含 summer internship)',
     'Summer（7 月）入学（精确轮次待补）', 'TOEFL iBT / IELTS Academic（具体分数待补）', '无需工作经验；4 tracks: AI in Business / Business Analytics / Marketing Analytics / Supply Chain Analytics；Pre-fall math boot camp；Analytics Practicum 真实企业项目；STEM-designated',
     '约 USD 70,000+/10-month（精确待补）', 'details/emory-msba.html'),
    ('vandy-msbt', 'vanderbilt', '商业与技术理学硕士（MSBT · 1 年 · STEM · NYC 校区 · 2027 首届）', 'Master of Science in Business and Technology (MSBT, Owen & School of Engineering, NYC Campus)', 'MS', 'm', 1, '1 year (30 credit hours = 24 core + 6 capstone)',
     '2026/8/1 开放；Priority 2026/11/19；Final 2027/7/1；2027/8/23 开学', 'TOEFL iBT / IELTS Academic（具体分数待补）', 'GRE/GMAT not required；2 推荐信；Video Essay；SOP；NYC Chelsea 校区全日制 residential；Owen + Engineering + College of Connected Computing 合办',
     '约 USD 70,000+/年（精确待补）', 'details/vandy-msbt.html'),
    ('vandy-macc', 'vanderbilt', '会计硕士（MAcc · 12 个月 · 两个方向：Assurance / Valuation）', 'Master of Accountancy (MAcc, Owen Graduate School of Management) - Assurance & Valuation tracks', 'MAcc', 'm', 0, '12 months (mod system, 2 mods per semester)',
     'Fall 入学（8 月初开学，精确轮次待补）', 'TOEFL iBT / IELTS Academic（具体分数待补）', 'Valuation track 先修: Financial/Managerial Accounting, Economics, Finance (B+)；10-week paid Big 4 internship；CPA exam prep（Becker）；Valuation 加考 CFA Level 1；申请费 $100',
     '约 USD 70,000+/12-month（精确待补）', 'details/vandy-macc.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)})')
